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  object.insert(element);
199  }
200 
201  return in;
202  }
203 
204 
205  /**
206  * Template specialisation of method writeObject() for std::set.
207  *
208  * \param out output stream
209  * \param object object
210  * \return output stream
211  */
212  template<class JElement_t, class JComparator_t, class JAllocator_t>
213  inline std::ostream& writeObject(std::ostream& out, const std::set<JElement_t, JComparator_t, JAllocator_t>& object)
214  {
215  for (typename std::set<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
216  writeObject(out, ' ');
217  writeObject(out, *i);
218  }
219 
220  return out;
221  }
222 
223 
224  /**
225  * Template specialisation of method writeObject() for std::set.
226  *
227  * \param out output stream
228  * \param prefix prefix
229  * \param object object
230  * \param postfix postfix
231  * \return output stream
232  */
233  template<class JElement_t, class JComparator_t, class JAllocator_t>
234  inline std::ostream& writeObject(std::ostream& out,
235  const char* prefix,
237  const char postfix)
238  {
239  for (typename std::set<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
240  writeObject(out, prefix, *i, postfix);
241  }
242 
243  return out;
244  }
245 
246 
247  /**
248  * Template specialisation of method readObject() for std::multiset.
249  *
250  * \param in input stream
251  * \param object object
252  * \return input stream
253  */
254  template<class JElement_t, class JComparator_t, class JAllocator_t>
255  inline std::istream& readObject(std::istream& in, std::multiset<JElement_t, JComparator_t, JAllocator_t>& object)
256  {
257  for (JElement_t element; readObject(in, element); ) {
258  object.insert(element);
259  }
260 
261  return in;
262  }
263 
264 
265  /**
266  * Template specialisation of method writeObject() for std::multiset.
267  *
268  * \param out output stream
269  * \param object object
270  * \return output stream
271  */
272  template<class JElement_t, class JComparator_t, class JAllocator_t>
273  inline std::ostream& writeObject(std::ostream& out, const std::multiset<JElement_t, JComparator_t, JAllocator_t>& object)
274  {
275  for (typename std::multiset<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
276  writeObject(out, ' ');
277  writeObject(out, *i);
278  }
279 
280  return out;
281  }
282 
283 
284  /**
285  * Template specialisation of method writeObject() for std::multiset.
286  *
287  * \param out output stream
288  * \param prefix prefix
289  * \param object object
290  * \param postfix postfix
291  * \return output stream
292  */
293  template<class JElement_t, class JComparator_t, class JAllocator_t>
294  inline std::ostream& writeObject(std::ostream& out,
295  const char* prefix,
297  const char postfix)
298  {
299  for (typename std::multiset<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
300  writeObject(out, prefix, *i, postfix);
301  }
302 
303  return out;
304  }
305 
306 
307  /**
308  * Template specialisation of method readObject() for std::pair.
309  *
310  * \param in input stream
311  * \param object object
312  * \return input stream
313  */
314  template<class JFirst_t, class JSecond_t>
315  inline std::istream& readObject(std::istream& in, std::pair<JFirst_t, JSecond_t>& object)
316  {
317  readObject(in, object.first);
318  readObject(in, object.second);
319 
320  return in;
321  }
322 
323 
324  /**
325  * Template specialisation of method writeObject() for std::pair.
326  *
327  * \param out output stream
328  * \param object object
329  * \return output stream
330  */
331  template<class JFirst_t, class JSecond_t>
332  inline std::ostream& writeObject(std::ostream& out, const std::pair<JFirst_t, JSecond_t>& object)
333  {
334  writeObject(out, object.first);
335  writeObject(out, ' ');
336  writeObject(out, object.second);
337 
338  return out;
339  }
340 
341 
342  /**
343  * Template specialisation of method writeObject() for std::pair.
344  *
345  * \param out output stream
346  * \param prefix prefix
347  * \param object object
348  * \param postfix postfix
349  * \return output stream
350  */
351  template<class JFirst_t, class JSecond_t>
352  inline std::ostream& writeObject(std::ostream& out,
353  const char* prefix,
354  const std::pair<JFirst_t, JSecond_t>& object,
355  const char postfix)
356  {
357  writeObject(out, prefix);
358  writeObject(out, object.first);
359  writeObject(out, ' ');
360  writeObject(out, object.second);
361  writeObject(out, postfix);
362 
363  return out;
364  }
365 
366 
367  /**
368  * Template specialisation of method readObject() for std::map.
369  *
370  * \param in input stream
371  * \param object object
372  * \return input stream
373  */
374  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
375  inline std::istream& readObject(std::istream& in, std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
376  {
377  for (std::pair<JKey_t, JValue_t> element; readObject(in, element); ) {
378  object.insert(element);
379  }
380 
381  return in;
382  }
383 
384 
385  /**
386  * Template specialisation of method writeObject() for std::map.
387  *
388  * \param out output stream
389  * \param object object
390  * \return output stream
391  */
392  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
393  inline std::ostream& writeObject(std::ostream& out, const std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
394  {
395  for (typename std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
396  writeObject(out, ' ');
397  writeObject(out, *i);
398  }
399 
400  return out;
401  }
402 
403 
404  /**
405  * Template specialisation of method writeObject() for std::map.
406  *
407  * \param out output stream
408  * \param prefix prefix
409  * \param object object
410  * \param postfix postfix
411  * \return output stream
412  */
413  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
414  inline std::ostream& writeObject(std::ostream& out,
415  const char* prefix,
417  const char postfix)
418  {
419  for (typename std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
420  writeObject(out, prefix, *i, postfix);
421  }
422 
423  return out;
424  }
425 
426 
427  /**
428  * Template specialisation of method readObject() for std::multimap.
429  *
430  * \param in input stream
431  * \param object object
432  * \return input stream
433  */
434  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
435  inline std::istream& readObject(std::istream& in, std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
436  {
437  for (std::pair<JKey_t, JValue_t> element; readObject(in, element); ) {
438  object.insert(element);
439  }
440 
441  return in;
442  }
443 
444 
445  /**
446  * Template specialisation of method writeObject() for std::multimap.
447  *
448  * \param out output stream
449  * \param object object
450  * \return output stream
451  */
452  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
453  inline std::ostream& writeObject(std::ostream& out, const std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>& object)
454  {
455  for (typename std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
456  writeObject(out, ' ');
457  writeObject(out, *i);
458  }
459 
460  return out;
461  }
462 
463 
464  /**
465  * Template specialisation of method writeObject() for std::multimap.
466  *
467  * \param out output stream
468  * \param prefix prefix
469  * \param object object
470  * \param postfix postfix
471  * \return output stream
472  */
473  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
474  inline std::ostream& writeObject(std::ostream& out,
475  const char* prefix,
477  const char postfix)
478  {
479  for (typename std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
480  writeObject(out, prefix, *i, postfix);
481  }
482 
483  return out;
484  }
485 
486 
487  /**
488  * Write array of objects.
489  *
490  * \param out output stream
491  * \param left left bracket
492  * \param right right bracket
493  * \param sep separator
494  * \param __begin begin of data
495  * \param __end end of data
496  * \return output stream
497  */
498  template<class T>
499  inline std::ostream& writeArray(std::ostream& out,
500  const char* left,
501  const char* right,
502  const char* sep,
503  T __begin,
504  T __end)
505  {
506  if (std::distance(__begin, __end) != 0) {
507 
508  out << left;
509 
510  T i = __begin;
511 
512  writeObject(out, *i);
513 
514  while (++i != __end) {
515 
516  out << sep;
517 
518  JEEP::writeObject(out, *i);
519  }
520 
521  out << right;
522  }
523 
524  return out;
525  }
526 }
527 #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.