Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

KDStream.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002    KD Tools - a set of useful widgets for Qt
00003    $Id: KDStream.h,v 1.3 2004/08/24 16:01:36 blackie Exp $
00004 */
00005 
00006 /****************************************************************************
00007 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
00008 **
00009 ** This file is part of the KD Tools library.
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 as published by the Free Software
00013 ** Foundation and appearing in the file LICENSE.GPL included in the
00014 ** packaging of this file.
00015 **
00016 ** Licensees holding valid commercial KD Tools licenses may use this file in
00017 ** accordance with the KD Tools Commercial License Agreement provided with
00018 ** the Software.
00019 **
00020 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00021 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00022 **
00023 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
00024 **   information about KD Tools Commercial License Agreements.
00025 **
00026 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00027 ** licensing are not clear to you.
00028 **
00029 **********************************************************************/
00030 
00031 #ifndef KIPI_KDSTREAM
00032 #define KIPI_KDSTREAM
00033 
00034 // Forward declarations.
00035 class QImage;
00036 class QPixmap;
00037 class QColor;
00038 class QColorGroup;
00039 class QPalette;
00040 class QCursor;
00041 class QDate;
00042 class QDateTime;
00043 class QTime;
00044 class QFont;
00045 class QPen;
00046 class QPoint;
00047 class QSize;
00048 class QRect;
00049 class QObject;
00050 class QVariant;
00051 class QBrush;
00052 class QSizePolicy;
00053 class QKeySequence;
00054 
00055 #include <qstring.h>
00056 #include <qvaluelist.h>
00057 #include <qstrlist.h>
00058 #include <qasciidict.h>
00059 #include <qintdict.h>
00060 #include <qptrdict.h>
00061 #include <qdict.h>
00062 #include <qvaluestack.h>
00063 #include <qasciicache.h>
00064 #include <qintcache.h>
00065 #include <qcache.h>
00066 
00067 #if ( QT_VERSION < 300 )
00068 #include <qlist.h>
00069 #include <qstack.h>
00070 #include <qqueue.h>
00071 #include <qvector.h>
00072 #endif
00073 
00074 #if ( QT_VERSION >= 300 )
00075 #include <qptrlist.h>
00076 #include <qptrstack.h>
00077 #include <qptrqueue.h>
00078 #include <qpair.h>
00079 #include <qptrvector.h>
00080 #include <qvaluevector.h>
00081 #endif
00082 
00083 // utility functions.
00084 class KDStream;
00085 typedef KDStream & (*KDSTREAMFUNC)(KDStream &);
00086 KDStream& endl( KDStream& stream);
00087 KDStream& flush( KDStream& stream);
00088 
00089 class KDStream
00090 {
00091 public:
00092   KDStream( QString* outputString = 0);
00093   ~KDStream();
00094   void flush();
00095 
00096   // Standard C++ types
00097   KDStream& operator<<( bool );
00098   KDStream& operator<<( char );
00099   KDStream& operator<<( float );
00100   KDStream& operator<<( double );
00101   KDStream& operator<<( short );
00102   KDStream& operator<<( unsigned short );
00103   KDStream& operator<<( int );
00104   KDStream& operator<<( unsigned int );
00105   KDStream& operator<<( long );
00106   KDStream& operator<<( unsigned long );
00107   KDStream& operator<<( const char* );
00108   KDStream& operator<<( const void* );
00109 
00110   // Data holding classes.
00111   KDStream& operator<<( const QString& );
00112   KDStream& operator<<( const QCString& );
00113   KDStream& operator<<( const QChar& );
00114 
00115   KDStream& operator<<( const QColor& );
00116   KDStream& operator<<( const QColorGroup& );
00117   KDStream& operator<<( const QPalette& );
00118   KDStream& operator<<( const QCursor& );
00119 
00120   KDStream& operator<<( const QDate& );
00121   KDStream& operator<<( const QDateTime& );
00122   KDStream& operator<<( const QTime& );
00123 
00124   KDStream& operator<<( const QFont& );
00125   KDStream& operator<<( const QPen& );
00126   KDStream& operator<<( const QPoint& );
00127   KDStream& operator<<( const QSize& );
00128   KDStream& operator<<( const QRect& );
00129   KDStream& operator<<( const QBrush& );
00130   KDStream& operator<<( const QSizePolicy& );
00131   KDStream& operator<<( const QKeySequence& );
00132   KDStream& operator<<( const QPixmap& );
00133   KDStream& operator<<( const QImage& );
00134 
00135   // misc
00136   KDStream& operator<<( KDSTREAMFUNC );
00137   KDStream& operator<<( const QVariant& );
00138   KDStream& operator<<( const QObject& );
00139   KDStream& operator<<( const QStrList& list );
00140 
00141 protected:
00142   QString QColor2Str( const QColor& col );
00143 
00144 private:
00145   QString _output;
00146   QString* _out;
00147 };
00148 
00149 
00150 // Helper functions for KDStream.
00151 // Defined as global functions to support
00152 // compilers without support for member templates
00153 // You should not need to call those yourself
00154 template <class Iterator> void KDStream_valueListStream( KDStream& st, Iterator begin, Iterator end )
00155 {
00156   st << "[";
00157   bool first = true;
00158   for ( Iterator it = begin; it != end; ++it ){
00159     if ( first )
00160       first = false;
00161     else
00162       st << ", ";
00163     st << *it;
00164   }
00165   st << "]";
00166 }
00167 
00168 template<class Iterator> void KDStream_ptrListStream( KDStream& st, Iterator it, bool doubleDeref )
00169 {
00170   st << "[";
00171   bool first = true;
00172   for ( ; *it; ++ it) {
00173     if ( first )
00174       first = false;
00175     else
00176       st << ", ";
00177 
00178     if ( doubleDeref )
00179       st << *(*it);
00180     else {
00181       // QStrList ought to be a value list rather than a ptr list, one less dereference is
00182       // necesary here, otherwise we will only stream out a char, rather than a char *
00183       st << *it;
00184     }
00185   }
00186   st << "]";
00187 }
00188 
00189 template<class Iterator> void KDStream_ptrDictStream( KDStream& st, Iterator it )
00190 {
00191   st << "{";
00192   bool first = true;
00193   for ( ; it; ++ it) {
00194     if ( first )
00195       first = false;
00196     else
00197       st << ", ";
00198 
00199     st << (it.currentKey()) << ": " << *(it.current()) ;
00200   }
00201   st << "}";
00202 }
00203 
00204 // Stream operators for containers
00205 // Defined as global functions to support
00206 // compilers without member templates
00207 
00208 template<class T> KDStream& operator<<( KDStream& st, const QValueList<T>& list )
00209 {
00210   KDStream_valueListStream( st, list.begin(), list.end() );
00211   return st;
00212 }
00213 
00214 #if ( QT_VERSION < 300 )
00215 template<class T> KDStream& operator<<( KDStream& st, const QList<T>& list )
00216 {
00217   KDStream_ptrListStream ( st, QListIterator<T>( list ) , true );
00218   return st;
00219 }
00220 
00221 template<class T> KDStream& operator<<( KDStream& st, const QArray<T>& array )
00222 {
00223   KDStream_valueListStream( st, array.begin(), array.end() );
00224   return st;
00225 }
00226 
00227 template<class T> KDStream& operator<<( KDStream& st, const QVector<T>& vector )
00228 {
00229   QList<T> list;
00230   vector.toList( &list );
00231 
00232   KDStream_ptrListStream ( st, QListIterator<T>( list ), true );
00233   return st;
00234 }
00235 #endif
00236 #if ( QT_VERSION >= 300 )
00237 template<class T> KDStream& operator<<( KDStream& st, const QMemArray<T>& array )
00238 {
00239   KDStream_valueListStream( st, array.begin(), array.end() );
00240   return st;
00241 }
00242 
00243 template<class T> KDStream& operator<<( KDStream& st, const QPtrList<T>& list )
00244 {
00245   KDStream_ptrListStream ( st, QPtrListIterator<T>( list ), true );
00246   return st;
00247 }
00248 
00249 template<class T1, class T2> KDStream& operator<<( KDStream& st, const QPair<T1,T2>& pair )
00250 {
00251   st << "(" << pair.first << "," << pair.second << ")";
00252   return st;
00253 }
00254 
00255 template<class T> KDStream& operator<<( KDStream& st, const QPtrVector<T>& vector )
00256 {
00257   QPtrList<T> list;
00258   vector.toList( &list );
00259 
00260   KDStream_ptrListStream( st, QPtrListIterator<T>( list ), true );
00261   return st;
00262 }
00263 
00264 template<class T> KDStream& operator<<( KDStream& st, const QValueVector<T>& vector )
00265 {
00266   KDStream_valueListStream( st, vector.begin(), vector.end() );
00267   return st;
00268 }
00269 #endif
00270 
00271 #if ( QT_VERSION < 300 )
00272 template<class T> KDStream& operator<<( KDStream& st, const QStack<T>& stack )
00273 {
00274   // I need a copy to look at the individual elements.
00275   QStack<T> copy(stack);
00276 #else
00277 template<class T> KDStream& operator<<( KDStream& st, const QPtrStack<T>& stack )
00278 {
00279 
00280   // I need a copy to look at the individual elements.
00281   QPtrStack<T> copy(stack);
00282   /*}*/
00283 #endif
00284   st << "[";
00285   if ( stack.count() > 1 )
00286     st << "top| ";
00287   st << " ";
00288 
00289   bool first = true;
00290   while ( !copy.isEmpty() ) {
00291     if (first)
00292       first = false;
00293     else
00294       st << ", ";
00295     st << *(copy.pop());
00296   }
00297 
00298   st << " ";
00299   if (  stack.count() > 1 )
00300     st << " |bottom";
00301   st << "]";
00302   return st;
00303 }
00304 
00305 // This function can unfortunately not be merged with the function for
00306 // Q(Ptr)Stack, as the Q(Ptr)Stack dereferences what it pops of the stack,
00307 // before streaming it:
00308 // Q(Ptr)Stack: *this << *(copy.pop());
00309 // QValueStack: *this <<   copy.pop() ;
00310 template<class T> KDStream& operator<<( KDStream& st, const QValueStack<T>& stack )
00311 {
00312   // I need a copy to look at the individual elements.
00313   QValueStack<T> copy(stack);
00314   st << "[";
00315   if ( stack.count() > 1 )
00316     st << "top| ";
00317   st << " ";
00318 
00319   bool first = true;
00320   while ( !copy.isEmpty() ) {
00321     if (first)
00322       first = false;
00323     else
00324       st << ", ";
00325     st << copy.pop();
00326   }
00327 
00328   st << " ";
00329   if (  stack.count() > 1 )
00330     st << " |bottom";
00331   st << "]";
00332   return st;
00333 }
00334 
00335 
00336 template<class T> KDStream& operator<<( KDStream& st, const QAsciiDict<T>& dict )
00337 {
00338   KDStream_ptrDictStream( st, QAsciiDictIterator<T>( dict ) );
00339   return st;
00340 }
00341 
00342 template<class T> KDStream& operator<<( KDStream& st, const QIntDict<T>& dict )
00343 {
00344   KDStream_ptrDictStream( st, QIntDictIterator<T>( dict ) );
00345   return st;
00346 }
00347 
00348 template<class T> KDStream& operator<<( KDStream& st, const QPtrDict<T>& dict )
00349 {
00350   KDStream_ptrDictStream( st, QPtrDictIterator<T>( dict ) );
00351   return st;
00352 }
00353 
00354 template<class T> KDStream& operator<<( KDStream& st, const QDict<T>& dict )
00355 {
00356   KDStream_ptrDictStream( st, QDictIterator<T>( dict ) );
00357   return st;
00358 }
00359 
00360 template<class T> KDStream& operator<<( KDStream& st, const QAsciiCache<T>& cache )
00361 {
00362   KDStream_ptrDictStream( st, QAsciiCacheIterator<T>( cache ) );
00363   return st;
00364 }
00365 
00366 template<class T> KDStream& operator<<( KDStream& st, const QIntCache<T>& cache )
00367 {
00368   KDStream_ptrDictStream( st, QIntCacheIterator<T>( cache ) );
00369   return st;
00370 }
00371 
00372 template<class T> KDStream& operator<<( KDStream& st, const QCache<T>& cache )
00373 {
00374   KDStream_ptrDictStream( st, QCacheIterator<T>( cache ) );
00375   return st;
00376 }
00377 
00378 #endif /* KDStream */

Generated on Tue Sep 13 05:01:58 2005 for libKipi by  doxygen 1.4.4