Jpp 19.3.0-rc.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::list from input.
109 *
110 * \param in reader
111 * \param object object
112 * \return reader
113 */
114 template<class JElement_t, class JAllocator_t>
116 {
117 int n = 0;
118
119 in >> n;
120
122
123 object.clear();
124
125 for (int i = 0; i != n; ++i) {
126
127 in >> element;
128
129 object.push_back(element);
130 }
131
132 return in;
133 }
134
135
136 /**
137 * Write template std::list to output.
138 *
139 * \param out writer
140 * \param object object
141 * \return writer
142 */
143 template<class JElement_t, class JAllocator_t>
145 {
146 int n = object.size();
147
148 out << n;
149
150 for (typename std::list<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
151 out << *i;
152 }
153
154 return out;
155 }
156
157
158 /**
159 * Read template std::set from input.
160 *
161 * \param in reader
162 * \param object object
163 * \return reader
164 */
165 template<class JElement_t, class JComparator_t, class JAllocator_t>
167 {
168 int n = 0;
169
170 in >> n;
171
173
174 object.clear();
175
176 for (int i = 0; i != n; ++i) {
177
178 in >> element;
179
180 object.insert(element);
181 }
182
183 return in;
184 }
185
186
187 /**
188 * Write template std::set to output.
189 *
190 * \param out writer
191 * \param object object
192 * \return writer
193 */
194 template<class JElement_t, class JComparator_t, class JAllocator_t>
196 {
197 int n = object.size();
198
199 out << n;
200
201 for (typename std::set<JElement_t, JComparator_t,JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
202 out << *i;
203 }
204
205 return out;
206 }
207
208
209 /**
210 * Read template std::multiset from input.
211 *
212 * \param in reader
213 * \param object object
214 * \return reader
215 */
216 template<class JElement_t, class JComparator_t, class JAllocator_t>
218 {
219 int n = 0;
220
221 in >> n;
222
224
225 object.clear();
226
227 for (int i = 0; i != n; ++i) {
228
229 in >> element;
230
231 object.insert(element);
232 }
233
234 return in;
235 }
236
237
238 /**
239 * Write template std::multiset to output.
240 *
241 * \param out writer
242 * \param object object
243 * \return writer
244 */
245 template<class JElement_t, class JComparator_t, class JAllocator_t>
247 {
248 int n = object.size();
249
250 out << n;
251
252 for (typename std::multiset<JElement_t, JComparator_t,JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
253 out << *i;
254 }
255
256 return out;
257 }
258
259
260 /**
261 * Read template std::pair from input.
262 *
263 * \param in reader
264 * \param object object
265 * \return reader
266 */
267 template<class JKey_t, class JValue_t>
269 {
270 in >> object.first;
271 in >> object.second;
272
273 return in;
274 }
275
276
277 /**
278 * Write template std::pair to output.
279 *
280 * \param out writer
281 * \param object object
282 * \return writer
283 */
284 template<class JKey_t, class JValue_t>
286 {
287 out << object.first;
288 out << object.second;
289
290 return out;
291 }
292
293
294 /**
295 * Read template std::map from input.
296 *
297 * \param in reader
298 * \param object object
299 * \return reader
300 */
301 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
303 {
304 int n = 0;
305
306 in >> n;
307
310
311 object.clear();
312
313 for (int i = 0; i != n; ++i) {
314
315 in >> key >> value;
316
317 object[key]=value;
318 }
319
320 return in;
321 }
322
323
324 /**
325 * Write template std::map to output.
326 *
327 * \param out writer
328 * \param object object
329 * \return writer
330 */
331 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
333 {
334 int n = object.size();
335
336 out << n;
337
338 for (typename std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
339 out << *i;
340 }
341
342 return out;
343 }
344
345
346 /**
347 * Read template std::multimap from input.
348 *
349 * \param in reader
350 * \param object object
351 * \return reader
352 */
353 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
355 {
356 int n = 0;
357
358 in >> n;
359
362
363 object.clear();
364
365 for (int i = 0; i != n; ++i) {
366
367 in >> key >> value;
368
369 object[key]=value;
370 }
371
372 return in;
373 }
374
375
376 /**
377 * Write template std::multimap to output.
378 *
379 * \param out writer
380 * \param object object
381 * \return writer
382 */
383 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
385 {
386 int n = object.size();
387
388 out << n;
389
390 for (typename std::multimap<JKey_t, JValue_t, JComparator_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
391 out << *i;
392 }
393
394 return out;
395 }
396}
397
398#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).