Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JStreamToolkit.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JSTREAMTOOLKIT__
2 #define __JEEP__JSTREAMTOOLKIT__
3 
4 #include <istream>
5 #include <ostream>
6 #include <iterator>
7 
8 #include "JLang/JSTDTypes.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JEEP {}
16 namespace JPP { using namespace JEEP; }
17 
18 namespace JEEP {
19 
20  /**
21  * Stream input of object.
22  *
23  * \param in input stream
24  * \param object object
25  * \return input stream
26  */
27  template<class T>
28  inline std::istream& readObject(std::istream& in, T& object)
29  {
30  return in >> object;
31  }
32 
33 
34  /**
35  * Stream output of object.
36  *
37  * \param out output stream
38  * \param object object
39  * \return output stream
40  */
41  template<class T>
42  inline std::ostream& writeObject(std::ostream& out, const T& object)
43  {
44  return out << object;
45  }
46 
47 
48  /**
49  * Stream output of object.
50  *
51  * \param out output stream
52  * \param prefix prefix
53  * \param object object
54  * \param postfix postfix
55  * \return output stream
56  */
57  template<class T>
58  inline std::ostream& writeObject(std::ostream& out,
59  const char* prefix,
60  const T& object,
61  const char postfix)
62  {
63  return out << prefix << object << postfix;
64  }
65 
66 
67  /**
68  * Template specialisation of method readObject() for std::vector.
69  *
70  * \param in input stream
71  * \param object object
72  * \return input stream
73  */
74  template<class JElement_t, class JAllocator_t>
75  inline std::istream& readObject(std::istream& in, std::vector<JElement_t, JAllocator_t>& object)
76  {
77  for (JElement_t element; readObject(in, element); ) {
78  object.push_back(element);
79  }
80 
81  return in;
82  }
83 
84 
85  /**
86  * Template specialisation of method writeObject() for std::vector.
87  *
88  * \param out output stream
89  * \param object object
90  * \return output stream
91  */
92  template<class JElement_t, class JAllocator_t>
93  inline std::ostream& writeObject(std::ostream& out, const std::vector<JElement_t, JAllocator_t>& object)
94  {
95  for (typename std::vector<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
96  writeObject(out, ' ');
97  writeObject(out, *i);
98  }
99 
100  return out;
101  }
102 
103 
104  /**
105  * Template specialisation of method writeObject() for std::vector.
106  *
107  * \param out output stream
108  * \param prefix prefix
109  * \param object object
110  * \param postfix postfix
111  * \return output stream
112  */
113  template<class JElement_t, class JAllocator_t>
114  inline std::ostream& writeObject(std::ostream& out,
115  const char* prefix,
117  const char postfix)
118  {
119  for (typename std::vector<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
120  writeObject(out, prefix, *i, postfix);
121  }
122 
123  return out;
124  }
125 
126 
127  /**
128  * Template specialisation of method readObject() for std::list.
129  *
130  * \param in input stream
131  * \param object object
132  * \return input stream
133  */
134  template<class JElement_t, class JAllocator_t>
135  inline std::istream& readObject(std::istream& in, std::list<JElement_t, JAllocator_t>& object)
136  {
137  for (JElement_t element; readObject(in, element); ) {
138  object.push_back(element);
139  }
140 
141  return in;
142  }
143 
144 
145  /**
146  * Template specialisation of method writeObject() for std::list.
147  *
148  * \param out output stream
149  * \param object object
150  * \return output stream
151  */
152  template<class JElement_t, class JAllocator_t>
153  inline std::ostream& writeObject(std::ostream& out, const std::list<JElement_t, JAllocator_t>& object)
154  {
155  for (typename std::list<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
156  writeObject(out, ' ');
157  writeObject(out, *i);
158  }
159 
160  return out;
161  }
162 
163 
164  /**
165  * Template specialisation of method writeObject() for std::list.
166  *
167  * \param out output stream
168  * \param prefix prefix
169  * \param object object
170  * \param postfix postfix
171  * \return output stream
172  */
173  template<class JElement_t, class JAllocator_t>
174  inline std::ostream& writeObject(std::ostream& out,
175  const char* prefix,
177  const char postfix)
178  {
179  for (typename std::list<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
180  writeObject(out, prefix, *i, postfix);
181  }
182 
183  return out;
184  }
185 
186 
187  /**
188  * Template specialisation of method readObject() for std::set.
189  *
190  * \param in input stream
191  * \param object object
192  * \return input stream
193  */
194  template<class JElement_t, class JComparator_t, class JAllocator_t>
195  inline std::istream& readObject(std::istream& in, std::set<JElement_t, JComparator_t, JAllocator_t>& object)
196  {
197  for (JElement_t element; readObject(in, element); ) {
198 
199  const std::pair<typename std::set<JElement_t, JComparator_t, JAllocator_t>::iterator, bool> result = object.insert(element);
200 
201  if (!result.second) {
202  object.erase (result.first);
203  object.insert(element);
204  }
205  }
206 
207  return in;
208  }
209 
210 
211  /**
212  * Template specialisation of method writeObject() for std::set.
213  *
214  * \param out output stream
215  * \param object object
216  * \return output stream
217  */
218  template<class JElement_t, class JComparator_t, class JAllocator_t>
219  inline std::ostream& writeObject(std::ostream& out, const std::set<JElement_t, JComparator_t, JAllocator_t>& object)
220  {
221  for (typename std::set<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
222  writeObject(out, ' ');
223  writeObject(out, *i);
224  }
225 
226  return out;
227  }
228 
229 
230  /**
231  * Template specialisation of method writeObject() for std::set.
232  *
233  * \param out output stream
234  * \param prefix prefix
235  * \param object object
236  * \param postfix postfix
237  * \return output stream
238  */
239  template<class JElement_t, class JComparator_t, class JAllocator_t>
240  inline std::ostream& writeObject(std::ostream& out,
241  const char* prefix,
243  const char postfix)
244  {
245  for (typename std::set<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
246  writeObject(out, prefix, *i, postfix);
247  }
248 
249  return out;
250  }
251 
252 
253  /**
254  * Template specialisation of method readObject() for std::multiset.
255  *
256  * \param in input stream
257  * \param object object
258  * \return input stream
259  */
260  template<class JElement_t, class JComparator_t, class JAllocator_t>
261  inline std::istream& readObject(std::istream& in, std::multiset<JElement_t, JComparator_t, JAllocator_t>& object)
262  {
263  for (JElement_t element; readObject(in, element); ) {
264  object.insert(element);
265  }
266 
267  return in;
268  }
269 
270 
271  /**
272  * Template specialisation of method writeObject() for std::multiset.
273  *
274  * \param out output stream
275  * \param object object
276  * \return output stream
277  */
278  template<class JElement_t, class JComparator_t, class JAllocator_t>
279  inline std::ostream& writeObject(std::ostream& out, const std::multiset<JElement_t, JComparator_t, JAllocator_t>& object)
280  {
281  for (typename std::multiset<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
282  writeObject(out, ' ');
283  writeObject(out, *i);
284  }
285 
286  return out;
287  }
288 
289 
290  /**
291  * Template specialisation of method writeObject() for std::multiset.
292  *
293  * \param out output stream
294  * \param prefix prefix
295  * \param object object
296  * \param postfix postfix
297  * \return output stream
298  */
299  template<class JElement_t, class JComparator_t, class JAllocator_t>
300  inline std::ostream& writeObject(std::ostream& out,
301  const char* prefix,
303  const char postfix)
304  {
305  for (typename std::multiset<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
306  writeObject(out, prefix, *i, postfix);
307  }
308 
309  return out;
310  }
311 
312 
313  /**
314  * Template specialisation of method readObject() for std::pair.
315  *
316  * \param in input stream
317  * \param object object
318  * \return input stream
319  */
320  template<class JFirst_t, class JSecond_t>
321  inline std::istream& readObject(std::istream& in, std::pair<JFirst_t, JSecond_t>& object)
322  {
323  readObject(in, object.first);
324  readObject(in, object.second);
325 
326  return in;
327  }
328 
329 
330  /**
331  * Template specialisation of method writeObject() for std::pair.
332  *
333  * \param out output stream
334  * \param object object
335  * \return output stream
336  */
337  template<class JFirst_t, class JSecond_t>
338  inline std::ostream& writeObject(std::ostream& out, const std::pair<JFirst_t, JSecond_t>& object)
339  {
340  writeObject(out, object.first);
341  writeObject(out, ' ');
342  writeObject(out, object.second);
343 
344  return out;
345  }
346 
347 
348  /**
349  * Template specialisation of method writeObject() for std::pair.
350  *
351  * \param out output stream
352  * \param prefix prefix
353  * \param object object
354  * \param postfix postfix
355  * \return output stream
356  */
357  template<class JFirst_t, class JSecond_t>
358  inline std::ostream& writeObject(std::ostream& out,
359  const char* prefix,
360  const std::pair<JFirst_t, JSecond_t>& object,
361  const char postfix)
362  {
363  writeObject(out, prefix);
364  writeObject(out, object.first);
365  writeObject(out, ' ');
366  writeObject(out, object.second);
367  writeObject(out, postfix);
368 
369  return out;
370  }
371 
372 
373  /**
374  * Template specialisation of method readObject() for std::map.
375  *
376  * \param in input stream
377  * \param object object
378  * \return input stream
379  */
380  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
381  inline std::istream& readObject(std::istream& in, std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
382  {
383  for (std::pair<JKey_t, JValue_t> element; readObject(in, element); ) {
384 
386 
387  if (!result.second) {
388  result.first->second = element.second;
389  }
390  }
391 
392  return in;
393  }
394 
395 
396  /**
397  * Template specialisation of method writeObject() for std::map.
398  *
399  * \param out output stream
400  * \param object object
401  * \return output stream
402  */
403  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
404  inline std::ostream& writeObject(std::ostream& out, const std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
405  {
406  for (typename std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
407  writeObject(out, ' ');
408  writeObject(out, *i);
409  }
410 
411  return out;
412  }
413 
414 
415  /**
416  * Template specialisation of method writeObject() for std::map.
417  *
418  * \param out output stream
419  * \param prefix prefix
420  * \param object object
421  * \param postfix postfix
422  * \return output stream
423  */
424  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
425  inline std::ostream& writeObject(std::ostream& out,
426  const char* prefix,
428  const char postfix)
429  {
430  for (typename std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
431  writeObject(out, prefix, *i, postfix);
432  }
433 
434  return out;
435  }
436 
437 
438  /**
439  * Template specialisation of method readObject() for std::multimap.
440  *
441  * \param in input stream
442  * \param object object
443  * \return input stream
444  */
445  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
446  inline std::istream& readObject(std::istream& in, std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
447  {
448  for (std::pair<JKey_t, JValue_t> element; readObject(in, element); ) {
449  object.insert(element);
450  }
451 
452  return in;
453  }
454 
455 
456  /**
457  * Template specialisation of method writeObject() for std::multimap.
458  *
459  * \param out output stream
460  * \param object object
461  * \return output stream
462  */
463  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
464  inline std::ostream& writeObject(std::ostream& out, const std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
465  {
466  for (typename std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
467  writeObject(out, ' ');
468  writeObject(out, *i);
469  }
470 
471  return out;
472  }
473 
474 
475  /**
476  * Template specialisation of method writeObject() for std::multimap.
477  *
478  * \param out output stream
479  * \param prefix prefix
480  * \param object object
481  * \param postfix postfix
482  * \return output stream
483  */
484  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
485  inline std::ostream& writeObject(std::ostream& out,
486  const char* prefix,
488  const char postfix)
489  {
490  for (typename std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
491  writeObject(out, prefix, *i, postfix);
492  }
493 
494  return out;
495  }
496 
497 
498  /**
499  * Write array of objects.
500  *
501  * \param out output stream
502  * \param left left bracket
503  * \param right right bracket
504  * \param sep separator
505  * \param __begin begin of data
506  * \param __end end of data
507  * \return output stream
508  */
509  template<class T>
510  inline std::ostream& writeArray(std::ostream& out,
511  const char* left,
512  const char* right,
513  const char* sep,
514  T __begin,
515  T __end)
516  {
517  if (std::distance(__begin, __end) != 0) {
518 
519  out << left;
520 
521  T i = __begin;
522 
523  writeObject(out, *i);
524 
525  while (++i != __end) {
526 
527  out << sep;
528 
529  JEEP::writeObject(out, *i);
530  }
531 
532  out << right;
533  }
534 
535  return out;
536  }
537 }
538 #endif
std::istream & readObject(std::istream &in, T &object)
Stream input of object.
std::ostream & writeArray(std::ostream &out, const char *left, const char *right, const char *sep, T __begin, T __end)
Write array of objects.
Forward declarations of STD containers.
std::ostream & writeObject(std::ostream &out, const T &object)
Stream output of object.