Jpp 21.0.1
the software that should make you happy
Loading...
Searching...
No Matches
JSTDIO.hh
Go to the documentation of this file.
1#ifndef __JIO__JSTDIO__
2#define __JIO__JSTDIO__
3
4#include <string>
5
6#include "JLang/JSTDTypes.hh"
8
9
10/**
11 * \file
12 * STD extensions for binary I/O.
13 * \author mdejong
14 */
15namespace JIO {}
16namespace JPP { using namespace JIO; }
17
18namespace JIO {
19
20 /**
21 * Read template std::string from input.
22 *
23 * \param in reader
24 * \param object object
25 * \return reader
26 */
27 inline JReader& operator>>(JReader& in, std::string& object)
28 {
29 unsigned int n = 0;
30
31 object.clear();
32
33 in.read((char*) &n, sizeof(unsigned int));
34
35 for (char c; n != 0; --n, object += c) {
36 in.read(&c, sizeof(char));
37 }
38
39 return in;
40 }
41
42
43 /**
44 * Write template std::string to output.
45 *
46 * \param out writer
47 * \param object object
48 * \return writer
49 */
50 inline JWriter& operator<<(JWriter& out, const std::string& object)
51 {
52 unsigned int n = object.size();
53
54 out.write((const char*) &n, sizeof(unsigned int));
55 out.write((const char*) object.c_str(), n);
56
57 return out;
58 }
59
60
61 /**
62 * Read template std::vector from input.
63 *
64 * \param in reader
65 * \param object object
66 * \return reader
67 */
68 template<class JElement_t, class JAllocator_t>
70 {
71 int n = 0;
72
73 in >> n;
74
75 object.resize(n);
76
77 for (typename std::vector<JElement_t, JAllocator_t>::iterator i = object.begin(); i != object.end(); ++i) {
78 in >> *i;
79 }
80
81 return in;
82 }
83
84
85 /**
86 * Write template std::vector to output.
87 *
88 * \param out writer
89 * \param object object
90 * \return writer
91 */
92 template<class JElement_t, class JAllocator_t>
94 {
95 int n = object.size();
96
97 out << n;
98
99 for (typename std::vector<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
100 out << *i;
101 }
102
103 return out;
104 }
105
106
107 /**
108 * Read template std::array from input.
109 *
110 * \param in reader
111 * \param object object
112 * \return reader
113 */
114 template<class JElement_t, size_t N>
116 {
117 for (typename std::array<JElement_t, N>::iterator i = object.begin(); i != object.end(); ++i) {
118 in >> *i;
119 }
120
121 return in;
122 }
123
124
125 /**
126 * Write template std::array to output.
127 *
128 * \param out writer
129 * \param object object
130 * \return writer
131 */
132 template<class JElement_t, size_t N>
134 {
135 for (typename std::array<JElement_t, N>::const_iterator i = object.begin(); i != object.end(); ++i) {
136 out << *i;
137 }
138
139 return out;
140 }
141
142
143 /**
144 * Read template std::list from input.
145 *
146 * \param in reader
147 * \param object object
148 * \return reader
149 */
150 template<class JElement_t, class JAllocator_t>
152 {
153 int n = 0;
154
155 in >> n;
156
158
159 object.clear();
160
161 for (int i = 0; i != n; ++i) {
162
163 in >> element;
164
165 object.push_back(element);
166 }
167
168 return in;
169 }
170
171
172 /**
173 * Write template std::list to output.
174 *
175 * \param out writer
176 * \param object object
177 * \return writer
178 */
179 template<class JElement_t, class JAllocator_t>
181 {
182 int n = object.size();
183
184 out << n;
185
186 for (typename std::list<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
187 out << *i;
188 }
189
190 return out;
191 }
192
193
194 /**
195 * Read template std::set from input.
196 *
197 * \param in reader
198 * \param object object
199 * \return reader
200 */
201 template<class JElement_t, class JComparator_t, class JAllocator_t>
203 {
204 int n = 0;
205
206 in >> n;
207
209
210 object.clear();
211
212 for (int i = 0; i != n; ++i) {
213
214 in >> element;
215
216 object.insert(element);
217 }
218
219 return in;
220 }
221
222
223 /**
224 * Write template std::set to output.
225 *
226 * \param out writer
227 * \param object object
228 * \return writer
229 */
230 template<class JElement_t, class JComparator_t, class JAllocator_t>
232 {
233 int n = object.size();
234
235 out << n;
236
237 for (typename std::set<JElement_t, JComparator_t,JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
238 out << *i;
239 }
240
241 return out;
242 }
243
244
245 /**
246 * Read template std::multiset from input.
247 *
248 * \param in reader
249 * \param object object
250 * \return reader
251 */
252 template<class JElement_t, class JComparator_t, class JAllocator_t>
254 {
255 int n = 0;
256
257 in >> n;
258
260
261 object.clear();
262
263 for (int i = 0; i != n; ++i) {
264
265 in >> element;
266
267 object.insert(element);
268 }
269
270 return in;
271 }
272
273
274 /**
275 * Write template std::multiset to output.
276 *
277 * \param out writer
278 * \param object object
279 * \return writer
280 */
281 template<class JElement_t, class JComparator_t, class JAllocator_t>
283 {
284 int n = object.size();
285
286 out << n;
287
288 for (typename std::multiset<JElement_t, JComparator_t,JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
289 out << *i;
290 }
291
292 return out;
293 }
294
295
296 /**
297 * Read template std::pair from input.
298 *
299 * \param in reader
300 * \param object object
301 * \return reader
302 */
303 template<class JKey_t, class JValue_t>
305 {
306 in >> object.first;
307 in >> object.second;
308
309 return in;
310 }
311
312
313 /**
314 * Write template std::pair to output.
315 *
316 * \param out writer
317 * \param object object
318 * \return writer
319 */
320 template<class JKey_t, class JValue_t>
322 {
323 out << object.first;
324 out << object.second;
325
326 return out;
327 }
328
329
330 /**
331 * Read template std::map from input.
332 *
333 * \param in reader
334 * \param object object
335 * \return reader
336 */
337 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
339 {
340 int n = 0;
341
342 in >> n;
343
346
347 object.clear();
348
349 for (int i = 0; i != n; ++i) {
350
351 in >> key >> value;
352
353 object[key]=value;
354 }
355
356 return in;
357 }
358
359
360 /**
361 * Write template std::map to output.
362 *
363 * \param out writer
364 * \param object object
365 * \return writer
366 */
367 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
369 {
370 int n = object.size();
371
372 out << n;
373
374 for (typename std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
375 out << *i;
376 }
377
378 return out;
379 }
380
381
382 /**
383 * Read template std::multimap from input.
384 *
385 * \param in reader
386 * \param object object
387 * \return reader
388 */
389 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
391 {
392 int n = 0;
393
394 in >> n;
395
398
399 object.clear();
400
401 for (int i = 0; i != n; ++i) {
402
403 in >> key >> value;
404
405 object[key]=value;
406 }
407
408 return in;
409 }
410
411
412 /**
413 * Write template std::multimap to output.
414 *
415 * \param out writer
416 * \param object object
417 * \return writer
418 */
419 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
421 {
422 int n = object.size();
423
424 out << n;
425
426 for (typename std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
427 out << *i;
428 }
429
430 return out;
431 }
432}
433
434#endif
Forward declarations of STD containers.
Interface for binary input.
Interface for binary output.
virtual int read(char *buffer, const int length)=0
Read byte array.
virtual int write(const char *buffer, const int length)=0
Write byte array.
Auxiliary classes and methods for binary I/O.
JReader & operator>>(JReader &in, std::string &object)
Read template std::string from input.
Definition JSTDIO.hh:27
JWriter & operator<<(JWriter &out, const std::string &object)
Write template std::string to output.
Definition JSTDIO.hh:50
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).