Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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
15namespace JEEP {}
16namespace JPP { using namespace JEEP; }
17
18namespace 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::pair.
69 *
70 * \param in input stream
71 * \param object object
72 * \return input stream
73 */
74 template<class JFirst_t, class JSecond_t>
75 inline std::istream& readObject(std::istream& in, std::pair<JFirst_t, JSecond_t>& object)
76 {
77 readObject(in, object.first);
78 readObject(in, object.second);
79
80 return in;
81 }
82
83
84 /**
85 * Template specialisation of method writeObject() for std::pair.
86 *
87 * \param out output stream
88 * \param object object
89 * \return output stream
90 */
91 template<class JFirst_t, class JSecond_t>
92 inline std::ostream& writeObject(std::ostream& out, const std::pair<JFirst_t, JSecond_t>& object)
93 {
94 writeObject(out, object.first);
95 writeObject(out, ' ');
96 writeObject(out, object.second);
97
98 return out;
99 }
100
101
102 /**
103 * Template specialisation of method writeObject() for std::pair.
104 *
105 * \param out output stream
106 * \param prefix prefix
107 * \param object object
108 * \param postfix postfix
109 * \return output stream
110 */
111 template<class JFirst_t, class JSecond_t>
112 inline std::ostream& writeObject(std::ostream& out,
113 const char* prefix,
114 const std::pair<JFirst_t, JSecond_t>& object,
115 const char postfix)
116 {
117 writeObject(out, prefix);
118 writeObject(out, object.first);
119 writeObject(out, ' ');
120 writeObject(out, object.second);
121 writeObject(out, postfix);
122
123 return out;
124 }
125
126
127 /**
128 * Template specialisation of method readObject() for std::vector.
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::vector<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::vector.
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::vector<JElement_t, JAllocator_t>& object)
154 {
155 for (typename std::vector<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::vector.
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::vector<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::list.
189 *
190 * \param in input stream
191 * \param object object
192 * \return input stream
193 */
194 template<class JElement_t, class JAllocator_t>
195 inline std::istream& readObject(std::istream& in, std::list<JElement_t, JAllocator_t>& object)
196 {
197 for (JElement_t element; readObject(in, element); ) {
198 object.push_back(element);
199 }
200
201 return in;
202 }
203
204
205 /**
206 * Template specialisation of method writeObject() for std::list.
207 *
208 * \param out output stream
209 * \param object object
210 * \return output stream
211 */
212 template<class JElement_t, class JAllocator_t>
213 inline std::ostream& writeObject(std::ostream& out, const std::list<JElement_t, JAllocator_t>& object)
214 {
215 for (typename std::list<JElement_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::list.
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 JAllocator_t>
234 inline std::ostream& writeObject(std::ostream& out,
235 const char* prefix,
237 const char postfix)
238 {
239 for (typename std::list<JElement_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::set.
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::set<JElement_t, JComparator_t, JAllocator_t>& object)
256 {
257 for (JElement_t element; readObject(in, element); ) {
258
260
261 if (!result.second) {
262 object.erase (result.first);
263 object.insert(element);
264 }
265 }
266
267 return in;
268 }
269
270
271 /**
272 * Template specialisation of method writeObject() for std::set.
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::set<JElement_t, JComparator_t, JAllocator_t>& object)
280 {
281 for (typename std::set<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::set.
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::set<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::multiset.
315 *
316 * \param in input stream
317 * \param object object
318 * \return input stream
319 */
320 template<class JElement_t, class JComparator_t, class JAllocator_t>
321 inline std::istream& readObject(std::istream& in, std::multiset<JElement_t, JComparator_t, JAllocator_t>& object)
322 {
323 for (JElement_t element; readObject(in, element); ) {
324 object.insert(element);
325 }
326
327 return in;
328 }
329
330
331 /**
332 * Template specialisation of method writeObject() for std::multiset.
333 *
334 * \param out output stream
335 * \param object object
336 * \return output stream
337 */
338 template<class JElement_t, class JComparator_t, class JAllocator_t>
339 inline std::ostream& writeObject(std::ostream& out, const std::multiset<JElement_t, JComparator_t, JAllocator_t>& object)
340 {
341 for (typename std::multiset<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
342 writeObject(out, ' ');
343 writeObject(out, *i);
344 }
345
346 return out;
347 }
348
349
350 /**
351 * Template specialisation of method writeObject() for std::multiset.
352 *
353 * \param out output stream
354 * \param prefix prefix
355 * \param object object
356 * \param postfix postfix
357 * \return output stream
358 */
359 template<class JElement_t, class JComparator_t, class JAllocator_t>
360 inline std::ostream& writeObject(std::ostream& out,
361 const char* prefix,
363 const char postfix)
364 {
365 for (typename std::multiset<JElement_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
366 writeObject(out, prefix, *i, postfix);
367 }
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 * Template specialisation of method readObject() for std::array.
500 *
501 * \param in input stream
502 * \param object object
503 * \return input stream
504 */
505 template<class JElement_t, std::size_t N>
506 inline std::istream& readObject(std::istream& in, std::array<JElement_t, N>& object)
507 {
508 for (size_t i = 0; i != N; ++i) {
509 readObject(in, object[i]);
510 }
511
512 return in;
513 }
514
515
516 /**
517 * Template specialisation of method writeObject() for std::array.
518 *
519 * \param out output stream
520 * \param object object
521 * \return output stream
522 */
523 template<class JElement_t, std::size_t N>
524 inline std::ostream& writeObject(std::ostream& out, const std::array<JElement_t, N>& object)
525 {
526 for (size_t i = 0; i != N; ++i) {
527 writeObject(out, ' ');
528 writeObject(out, object[i]);
529 }
530
531 return out;
532 }
533
534
535 /**
536 * Template specialisation of method writeObject() for std::array.
537 *
538 * \param out output stream
539 * \param prefix prefix
540 * \param object object
541 * \param postfix postfix
542 * \return output stream
543 */
544 template<class JElement_t, std::size_t N>
545 inline std::ostream& writeObject(std::ostream& out,
546 const char* prefix,
547 const std::array<JElement_t, N>& object,
548 const char postfix)
549 {
550 for (size_t i = 0; i != N; ++i) {
551 writeObject(out, prefix, object[i], postfix);
552 }
553
554 return out;
555 }
556
557
558 /**
559 * Write array of objects.
560 *
561 * \param out output stream
562 * \param left left bracket
563 * \param right right bracket
564 * \param sep separator
565 * \param __begin begin of data
566 * \param __end end of data
567 * \return output stream
568 */
569 template<class T>
570 inline std::ostream& writeArray(std::ostream& out,
571 const char* left,
572 const char* right,
573 const char* sep,
574 T __begin,
575 T __end)
576 {
577 if (std::distance(__begin, __end) != 0) {
578
579 out << left;
580
581 T i = __begin;
582
583 writeObject(out, *i);
584
585 while (++i != __end) {
586
587 out << sep;
588
589 writeObject(out, *i);
590 }
591
592 out << right;
593 }
594
595 return out;
596 }
597
598
599 /**
600 * Auxiliary data structure for streaming of STL containers.
601 *
602 * This manipulator transfers the following stream operation to method writeObject
603 * so that all STL containers can directly be printed.
604 */
605 struct JEEPZ
606 {
607 protected:
608 /**
609 * Auxiliary class for format STL containers.
610 */
611 struct JStream
612 {
613 /**
614 * Constructor.
615 *
616 * \param out output stream
617 */
618 JStream(std::ostream& out) :
619 out(out)
620 {}
621
622
623 /**
624 * Write value to output stream.
625 *
626 * \param value value
627 * \return this JSTDStreamer
628 */
629 template<class T>
630 std::ostream& operator<<(const T& value)
631 {
632 return writeObject(out, value);
633 }
634
635 private:
636 std::ostream& out;
637 };
638
639 public:
640 /**
641 * Default constructor.
642 */
644 {}
645
646
647 /**
648 * Format specifier.
649 *
650 * \param out output stream
651 * \param format format
652 * \return output stream
653 */
654 friend inline JStream operator<<(std::ostream& out, const JEEPZ& format)
655 {
656 return JStream(out);
657 }
658 };
659}
660
661#endif
Forward declarations of STD containers.
General puprpose classes and methods.
std::ostream & writeObject(std::ostream &out, const T &object)
Stream output of object.
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.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for format STL containers.
std::ostream & operator<<(const T &value)
Write value to output stream.
JStream(std::ostream &out)
Constructor.
Auxiliary data structure for streaming of STL containers.
friend JStream operator<<(std::ostream &out, const JEEPZ &format)
Format specifier.
JEEPZ()
Default constructor.