/* KD Tools - a set of useful widgets for TQt $Id: KDStream.h 387954 2005-02-10 07:49:40Z blackie $ */ /**************************************************************************** ** Copyright (C) 2001-2005 Klar�lvdalens Datakonsult AB. All rights reserved. ** ** This file is part of the KD Tools library. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** Licensees holding valid commercial KD Tools licenses may use this file in ** accordance with the KD Tools Commercial License Agreement provided with ** the Software. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** Contact info@klaralvdalens-datakonsult.se if any conditions of this ** licensing are not clear to you. ** **********************************************************************/ #ifndef KIPI_KDSTREAM #define KIPI_KDSTREAM // Forward declarations. class TQImage; class TQPixmap; class TQColor; class TQColorGroup; class TQPalette; class TQCursor; class TQDate; class TQDateTime; class TQTime; class TQFont; class TQPen; class TQPoint; class TQSize; class TQRect; class TQObject; class TQVariant; class TQBrush; class TQSizePolicy; class TQKeySequence; #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // utility functions. class KDStream; typedef KDStream & (*KDSTREAMFUNC)(KDStream &); KDStream& endl( KDStream& stream); KDStream& flush( KDStream& stream); class KDStream { public: KDStream( TQString* outputString = 0); ~KDStream(); void flush(); // Standard C++ types KDStream& operator<<( bool ); KDStream& operator<<( char ); KDStream& operator<<( float ); KDStream& operator<<( double ); KDStream& operator<<( short ); KDStream& operator<<( unsigned short ); KDStream& operator<<( int ); KDStream& operator<<( unsigned int ); KDStream& operator<<( long ); KDStream& operator<<( unsigned long ); KDStream& operator<<( const char* ); KDStream& operator<<( const void* ); // Data holding classes. KDStream& operator<<( const TQString& ); KDStream& operator<<( const TQCString& ); KDStream& operator<<( const TQChar& ); KDStream& operator<<( const TQColor& ); KDStream& operator<<( const TQColorGroup& ); KDStream& operator<<( const TQPalette& ); KDStream& operator<<( const TQCursor& ); KDStream& operator<<( const TQDate& ); KDStream& operator<<( const TQDateTime& ); KDStream& operator<<( const TQTime& ); KDStream& operator<<( const TQFont& ); KDStream& operator<<( const TQPen& ); KDStream& operator<<( const TQPoint& ); KDStream& operator<<( const TQSize& ); KDStream& operator<<( const TQRect& ); KDStream& operator<<( const TQBrush& ); KDStream& operator<<( const TQSizePolicy& ); KDStream& operator<<( const TQKeySequence& ); KDStream& operator<<( const TQPixmap& ); KDStream& operator<<( const TQImage& ); // misc KDStream& operator<<( KDSTREAMFUNC ); KDStream& operator<<( const TQVariant& ); KDStream& operator<<( const TQObject& ); KDStream& operator<<( const TQStrList& list ); protected: TQString TQColor2Str( const TQColor& col ); private: TQString _output; TQString* _out; }; // Helper functions for KDStream. // Defined as global functions to support // compilers without support for member templates // You should not need to call those yourself template void KDStream_valueListStream( KDStream& st, Iterator begin, Iterator end ) { st << "["; bool first = true; for ( Iterator it = begin; it != end; ++it ){ if ( first ) first = false; else st << ", "; st << *it; } st << "]"; } template void KDStream_ptrListStream( KDStream& st, Iterator it, bool doubleDeref ) { st << "["; bool first = true; for ( ; *it; ++ it) { if ( first ) first = false; else st << ", "; if ( doubleDeref ) st << *(*it); else { // TQStrList ought to be a value list rather than a ptr list, one less dereference is // necesary here, otherwise we will only stream out a char, rather than a char * st << *it; } } st << "]"; } template void KDStream_ptrDictStream( KDStream& st, Iterator it ) { st << "{"; bool first = true; for ( ; it; ++ it) { if ( first ) first = false; else st << ", "; st << (it.currentKey()) << ": " << *(it.current()) ; } st << "}"; } // Stream operators for containers // Defined as global functions to support // compilers without member templates template KDStream& operator<<( KDStream& st, const TQValueList& list ) { KDStream_valueListStream( st, list.begin(), list.end() ); return st; } template KDStream& operator<<( KDStream& st, const TQMemArray& array ) { KDStream_valueListStream( st, array.begin(), array.end() ); return st; } template KDStream& operator<<( KDStream& st, const TQPtrList& list ) { KDStream_ptrListStream ( st, TQPtrListIterator( list ), true ); return st; } template KDStream& operator<<( KDStream& st, const TQPair& pair ) { st << "(" << pair.first << "," << pair.second << ")"; return st; } template KDStream& operator<<( KDStream& st, const TQPtrVector& vector ) { TQPtrList list; vector.toList( &list ); KDStream_ptrListStream( st, TQPtrListIterator( list ), true ); return st; } template KDStream& operator<<( KDStream& st, const TQValueVector& vector ) { KDStream_valueListStream( st, vector.begin(), vector.end() ); return st; } template KDStream& operator<<( KDStream& st, const TQPtrStack& stack ) { // I need a copy to look at the individual elements. TQPtrStack copy(stack); /*}*/ st << "["; if ( stack.count() > 1 ) st << "top| "; st << " "; bool first = true; while ( !copy.isEmpty() ) { if (first) first = false; else st << ", "; st << *(copy.pop()); } st << " "; if ( stack.count() > 1 ) st << " |bottom"; st << "]"; return st; } // This function can unfortunately not be merged with the function for // Q(Ptr)Stack, as the Q(Ptr)Stack dereferences what it pops of the stack, // before streaming it: // Q(Ptr)Stack: *this << *(copy.pop()); // TQValueStack: *this << copy.pop() ; template KDStream& operator<<( KDStream& st, const TQValueStack& stack ) { // I need a copy to look at the individual elements. TQValueStack copy(stack); st << "["; if ( stack.count() > 1 ) st << "top| "; st << " "; bool first = true; while ( !copy.isEmpty() ) { if (first) first = false; else st << ", "; st << copy.pop(); } st << " "; if ( stack.count() > 1 ) st << " |bottom"; st << "]"; return st; } template KDStream& operator<<( KDStream& st, const TQAsciiDict& dict ) { KDStream_ptrDictStream( st, TQAsciiDictIterator( dict ) ); return st; } template KDStream& operator<<( KDStream& st, const TQIntDict& dict ) { KDStream_ptrDictStream( st, TQIntDictIterator( dict ) ); return st; } template KDStream& operator<<( KDStream& st, const TQPtrDict& dict ) { KDStream_ptrDictStream( st, TQPtrDictIterator( dict ) ); return st; } template KDStream& operator<<( KDStream& st, const TQDict& dict ) { KDStream_ptrDictStream( st, TQDictIterator( dict ) ); return st; } template KDStream& operator<<( KDStream& st, const TQAsciiCache& cache ) { KDStream_ptrDictStream( st, TQAsciiCacheIterator( cache ) ); return st; } template KDStream& operator<<( KDStream& st, const TQIntCache& cache ) { KDStream_ptrDictStream( st, TQIntCacheIterator( cache ) ); return st; } template KDStream& operator<<( KDStream& st, const TQCache& cache ) { KDStream_ptrDictStream( st, TQCacheIterator( cache ) ); return st; } #endif /* KDStream */