libKipi
KDStream.h
Go to the documentation of this file.
1 /* -*- Mode: C++ -*-
2  KD Tools - a set of useful widgets for Qt
3  $Id: KDStream.h 387954 2005-02-10 07:49:40Z blackie $
4 */
5 
6 /****************************************************************************
7 ** Copyright (C) 2001-2005 Klarälvdalens Datakonsult AB. All rights reserved.
8 **
9 ** This file is part of the KD Tools library.
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.GPL included in the
14 ** packaging of this file.
15 **
16 ** Licensees holding valid commercial KD Tools licenses may use this file in
17 ** accordance with the KD Tools Commercial License Agreement provided with
18 ** the Software.
19 **
20 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 **
23 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
24 ** licensing are not clear to you.
25 **
26 **********************************************************************/
27 
28 #ifndef KIPI_KDSTREAM
29 #define KIPI_KDSTREAM
30 
31 // Forward declarations.
32 class QImage;
33 class QPixmap;
34 class QColor;
35 class QColorGroup;
36 class QPalette;
37 class QCursor;
38 class QDate;
39 class QDateTime;
40 class QTime;
41 class QFont;
42 class QPen;
43 class QPoint;
44 class QSize;
45 class QRect;
46 class QObject;
47 class QVariant;
48 class QBrush;
49 class QSizePolicy;
50 class QKeySequence;
51 
52 #include <qstring.h>
53 #include <qvaluelist.h>
54 #include <qstrlist.h>
55 #include <qasciidict.h>
56 #include <qintdict.h>
57 #include <qptrdict.h>
58 #include <qdict.h>
59 #include <qvaluestack.h>
60 #include <qasciicache.h>
61 #include <qintcache.h>
62 #include <qcache.h>
63 
64 #if ( QT_VERSION < 300 )
65 #include <qlist.h>
66 #include <qstack.h>
67 #include <qqueue.h>
68 #include <qvector.h>
69 #endif
70 
71 #if ( QT_VERSION >= 300 )
72 #include <qptrlist.h>
73 #include <qptrstack.h>
74 #include <qptrqueue.h>
75 #include <qpair.h>
76 #include <qptrvector.h>
77 #include <qvaluevector.h>
78 #endif
79 
80 // utility functions.
81 class KDStream;
82 typedef KDStream & (*KDSTREAMFUNC)(KDStream &);
83 KDStream& endl( KDStream& stream);
84 KDStream& flush( KDStream& stream);
85 
86 class KDStream
87 {
88 public:
89  KDStream( QString* outputString = 0);
90  ~KDStream();
91  void flush();
92 
93  // Standard C++ types
94  KDStream& operator<<( bool );
95  KDStream& operator<<( char );
96  KDStream& operator<<( float );
97  KDStream& operator<<( double );
98  KDStream& operator<<( short );
99  KDStream& operator<<( unsigned short );
100  KDStream& operator<<( int );
101  KDStream& operator<<( unsigned int );
102  KDStream& operator<<( long );
103  KDStream& operator<<( unsigned long );
104  KDStream& operator<<( const char* );
105  KDStream& operator<<( const void* );
106 
107  // Data holding classes.
108  KDStream& operator<<( const QString& );
109  KDStream& operator<<( const QCString& );
110  KDStream& operator<<( const QChar& );
111 
112  KDStream& operator<<( const QColor& );
113  KDStream& operator<<( const QColorGroup& );
114  KDStream& operator<<( const QPalette& );
115  KDStream& operator<<( const QCursor& );
116 
117  KDStream& operator<<( const QDate& );
118  KDStream& operator<<( const QDateTime& );
119  KDStream& operator<<( const QTime& );
120 
121  KDStream& operator<<( const QFont& );
122  KDStream& operator<<( const QPen& );
123  KDStream& operator<<( const QPoint& );
124  KDStream& operator<<( const QSize& );
125  KDStream& operator<<( const QRect& );
126  KDStream& operator<<( const QBrush& );
127  KDStream& operator<<( const QSizePolicy& );
128  KDStream& operator<<( const QKeySequence& );
129  KDStream& operator<<( const QPixmap& );
130  KDStream& operator<<( const QImage& );
131 
132  // misc
134  KDStream& operator<<( const QVariant& );
135  KDStream& operator<<( const QObject& );
136  KDStream& operator<<( const QStrList& list );
137 
138 protected:
139  QString QColor2Str( const QColor& col );
140 
141 private:
142  QString _output;
143  QString* _out;
144 };
145 
146 
147 // Helper functions for KDStream.
148 // Defined as global functions to support
149 // compilers without support for member templates
150 // You should not need to call those yourself
151 template <class Iterator> void KDStream_valueListStream( KDStream& st, Iterator begin, Iterator end )
152 {
153  st << "[";
154  bool first = true;
155  for ( Iterator it = begin; it != end; ++it ){
156  if ( first )
157  first = false;
158  else
159  st << ", ";
160  st << *it;
161  }
162  st << "]";
163 }
164 
165 template<class Iterator> void KDStream_ptrListStream( KDStream& st, Iterator it, bool doubleDeref )
166 {
167  st << "[";
168  bool first = true;
169  for ( ; *it; ++ it) {
170  if ( first )
171  first = false;
172  else
173  st << ", ";
174 
175  if ( doubleDeref )
176  st << *(*it);
177  else {
178  // QStrList ought to be a value list rather than a ptr list, one less dereference is
179  // necesary here, otherwise we will only stream out a char, rather than a char *
180  st << *it;
181  }
182  }
183  st << "]";
184 }
185 
186 template<class Iterator> void KDStream_ptrDictStream( KDStream& st, Iterator it )
187 {
188  st << "{";
189  bool first = true;
190  for ( ; it; ++ it) {
191  if ( first )
192  first = false;
193  else
194  st << ", ";
195 
196  st << (it.currentKey()) << ": " << *(it.current()) ;
197  }
198  st << "}";
199 }
200 
201 // Stream operators for containers
202 // Defined as global functions to support
203 // compilers without member templates
204 
205 template<class T> KDStream& operator<<( KDStream& st, const QValueList<T>& list )
206 {
207  KDStream_valueListStream( st, list.begin(), list.end() );
208  return st;
209 }
210 
211 #if ( QT_VERSION < 300 )
212 template<class T> KDStream& operator<<( KDStream& st, const QList<T>& list )
213 {
214  KDStream_ptrListStream ( st, QListIterator<T>( list ) , true );
215  return st;
216 }
217 
218 template<class T> KDStream& operator<<( KDStream& st, const QArray<T>& array )
219 {
220  KDStream_valueListStream( st, array.begin(), array.end() );
221  return st;
222 }
223 
224 template<class T> KDStream& operator<<( KDStream& st, const QVector<T>& vector )
225 {
226  QList<T> list;
227  vector.toList( &list );
228 
229  KDStream_ptrListStream ( st, QListIterator<T>( list ), true );
230  return st;
231 }
232 #endif
233 #if ( QT_VERSION >= 300 )
234 template<class T> KDStream& operator<<( KDStream& st, const QMemArray<T>& array )
235 {
236  KDStream_valueListStream( st, array.begin(), array.end() );
237  return st;
238 }
239 
240 template<class T> KDStream& operator<<( KDStream& st, const QPtrList<T>& list )
241 {
242  KDStream_ptrListStream ( st, QPtrListIterator<T>( list ), true );
243  return st;
244 }
245 
246 template<class T1, class T2> KDStream& operator<<( KDStream& st, const QPair<T1,T2>& pair )
247 {
248  st << "(" << pair.first << "," << pair.second << ")";
249  return st;
250 }
251 
252 template<class T> KDStream& operator<<( KDStream& st, const QPtrVector<T>& vector )
253 {
254  QPtrList<T> list;
255  vector.toList( &list );
256 
257  KDStream_ptrListStream( st, QPtrListIterator<T>( list ), true );
258  return st;
259 }
260 
261 template<class T> KDStream& operator<<( KDStream& st, const QValueVector<T>& vector )
262 {
263  KDStream_valueListStream( st, vector.begin(), vector.end() );
264  return st;
265 }
266 #endif
267 
268 #if ( QT_VERSION < 300 )
269 template<class T> KDStream& operator<<( KDStream& st, const QStack<T>& stack )
270 {
271  // I need a copy to look at the individual elements.
272  QStack<T> copy(stack);
273 #else
274 template<class T> KDStream& operator<<( KDStream& st, const QPtrStack<T>& stack )
275 {
276 
277  // I need a copy to look at the individual elements.
278  QPtrStack<T> copy(stack);
279  /*}*/
280 #endif
281  st << "[";
282  if ( stack.count() > 1 )
283  st << "top| ";
284  st << " ";
285 
286  bool first = true;
287  while ( !copy.isEmpty() ) {
288  if (first)
289  first = false;
290  else
291  st << ", ";
292  st << *(copy.pop());
293  }
294 
295  st << " ";
296  if ( stack.count() > 1 )
297  st << " |bottom";
298  st << "]";
299  return st;
300 }
301 
302 // This function can unfortunately not be merged with the function for
303 // Q(Ptr)Stack, as the Q(Ptr)Stack dereferences what it pops of the stack,
304 // before streaming it:
305 // Q(Ptr)Stack: *this << *(copy.pop());
306 // QValueStack: *this << copy.pop() ;
307 template<class T> KDStream& operator<<( KDStream& st, const QValueStack<T>& stack )
308 {
309  // I need a copy to look at the individual elements.
310  QValueStack<T> copy(stack);
311  st << "[";
312  if ( stack.count() > 1 )
313  st << "top| ";
314  st << " ";
315 
316  bool first = true;
317  while ( !copy.isEmpty() ) {
318  if (first)
319  first = false;
320  else
321  st << ", ";
322  st << copy.pop();
323  }
324 
325  st << " ";
326  if ( stack.count() > 1 )
327  st << " |bottom";
328  st << "]";
329  return st;
330 }
331 
332 
333 template<class T> KDStream& operator<<( KDStream& st, const QAsciiDict<T>& dict )
334 {
335  KDStream_ptrDictStream( st, QAsciiDictIterator<T>( dict ) );
336  return st;
337 }
338 
339 template<class T> KDStream& operator<<( KDStream& st, const QIntDict<T>& dict )
340 {
341  KDStream_ptrDictStream( st, QIntDictIterator<T>( dict ) );
342  return st;
343 }
344 
345 template<class T> KDStream& operator<<( KDStream& st, const QPtrDict<T>& dict )
346 {
347  KDStream_ptrDictStream( st, QPtrDictIterator<T>( dict ) );
348  return st;
349 }
350 
351 template<class T> KDStream& operator<<( KDStream& st, const QDict<T>& dict )
352 {
353  KDStream_ptrDictStream( st, QDictIterator<T>( dict ) );
354  return st;
355 }
356 
357 template<class T> KDStream& operator<<( KDStream& st, const QAsciiCache<T>& cache )
358 {
359  KDStream_ptrDictStream( st, QAsciiCacheIterator<T>( cache ) );
360  return st;
361 }
362 
363 template<class T> KDStream& operator<<( KDStream& st, const QIntCache<T>& cache )
364 {
365  KDStream_ptrDictStream( st, QIntCacheIterator<T>( cache ) );
366  return st;
367 }
368 
369 template<class T> KDStream& operator<<( KDStream& st, const QCache<T>& cache )
370 {
371  KDStream_ptrDictStream( st, QCacheIterator<T>( cache ) );
372  return st;
373 }
374 
375 #endif /* KDStream */
void KDStream_valueListStream(KDStream &st, Iterator begin, Iterator end)
Definition: KDStream.h:151
void flush()
Definition: KDStream.cpp:100
Streaming operators for Qt classes.
Definition: KDStream.h:86
KDStream &(* KDSTREAMFUNC)(KDStream &)
Definition: KDStream.h:82
void KDStream_ptrListStream(KDStream &st, Iterator it, bool doubleDeref)
Definition: KDStream.h:165
void KDStream_ptrDictStream(KDStream &st, Iterator it)
Definition: KDStream.h:186
KDStream(QString *outputString=0)
Definition: KDStream.cpp:79
KDStream & flush(KDStream &stream)
Definition: KDStream.cpp:274
~KDStream()
Definition: KDStream.cpp:91
KDStream & operator<<(bool)
Definition: KDStream.cpp:117
KDStream & endl(KDStream &stream)
Definition: KDStream.cpp:264
QString QColor2Str(const QColor &col)
Definition: KDStream.cpp:495