-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinIO.hpp
437 lines (362 loc) · 12 KB
/
binIO.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/******************************************************************************
* GRIDGEN: Grid Generating Compiler
* By: Andy Stone (aistone@gmail.com)
* (C) Copyright 2011 Colorado State University
*****************************************************************************/
/** \addtogroup IO
* @{
*/
#ifndef BINIO_HPP_
#define BINIO_HPP_
#include "environment.hpp"
#include "iserializable.hpp"
#include <iostream>
#include <map>
#include <string>
/**
* Initialization function for the binio module.
*/
void initializeModule_binio();
/**
* @ingroup IO
*
* This class includes several methods for inputting and outputting primitive
* and serializable objects from and to input and output streams.
*/
class BinIO {
public:
// =======================
// - [Output primitives] -
// =======================
/** @name Output primitives */
///@{
/** Output an integer onto the stream. */
static void out(std::ostream &out, const int &val);
/** Output a double onto the stream. */
static void out(std::ostream &out, const double &val);
/** Output a boolean onto the stream. */
static void out(std::ostream &out, const bool &val);
/** Output a string onto the stream. */
static void out(std::ostream &out, const std::string &s);
/** Output a serializable object onto the stream. */
static void out(std::ostream &out, const ISerializable &obj);
/** Output a pointer to an environmental object. */
static void outIdent(std::ostream &out, const IEnvironmental &obj);
///@}
// =======================
// - [Output arrays] -
// =======================
/** @name Output arrays */
///@{
/** Output an array of primitives or objects. */
template <typename T>
static void out(std::ostream &out, T* A, int size);
/**
* Output an array of entities with a specialized output function
* per object.
*/
template <typename T>
static void out(std::ostream &out, T* A, int size,
void (*saveFunc)(std::ostream&, const T&));
///@}
// ==============================
// - [Output STL collections] -
// ==============================
/** @name Output STL collections */
///@{
/** Output a collection of primitives or objects. */
template <typename IteratorT>
static void out(std::ostream &out, IteratorT b, IteratorT e);
/**
* Output a collection of entities with a specialized output function
* per object.
*/
template <typename IteratorT, typename T>
static void out(std::ostream &out, IteratorT b, IteratorT e,
void (*saveFunc)(std::ostream&, const T&));
/**
* Output an STL vector. When outputting a vector that itself contains
* STL objects it is necessary to use this function in place
* of the iterators based output function.
*/
template <typename T>
static void out(std::ostream &out, const std::vector<T> &v);
/** Output a collection of pointers to environmental objects. */
template <typename IteratorT>
static void outIdents(std::ostream &out, IteratorT b, IteratorT e);
/**
* Output a map of pointers of environmental objects to some other
* entity.
**/
/** Output an environmental map. */
template<typename T>
static void outEnvMap(std::ostream &out,
const std::map<std::string, T*> &map);
///@}
// =======================
// - [Input primitives] -
// =======================
/** @name Input primitives */
///@{
/** Input an integer from the stream. */
static void in(std::istream &in, int &val);
/** Input a double onto the stream. */
static void in(std::istream &in, double &val);
/** Input a boolean onto the stream. */
static void in(std::istream &in, bool &val);
/** Input a string onto the stream. */
static void in(std::istream &in, std::string &s);
/** Input a serializable object onto the stream. */
static void in(std::istream &in, ISerializable &obj);
/**
* Input a pointer to an indentifiable object. The parameter getter
* should be a pointer to a function that can get a pointer to the
* object (for example: &Environment::getSubgrid).
**/
template <typename T>
static void inIdent(std::istream &in, T *ptr,
T (*getter)(const std::string&));
///@}
// =======================
// - [Input arrays] -
// =======================
/** @name Input arrays */
///@{
/** Input an array of primitives or objects. */
template <typename T>
static void in(std::istream &in, T** A);
/**
* Input an array of entities with a specialized input function
* per object.
*/
template <typename T>
static void in(std::istream &in, T** A,
void (*loadFunc)(std::istream&, T&));
/**
* Input an STL vector. When inputting a vector that itself contains
* STL objects it is necessary to use this function in place
* of the iterators based input functions.
*/
template <typename T>
static void in(std::istream &in, std::vector<T> &v);
///@}
// ==============================
// - [Input STL collections] -
// ==============================
/** @name Input STL collections */
///@{
/**
* Input a collection of primitives or objects. The parameter insert is
* an inserter iterator into the collection (for example STL's
* back_inserter). The parameter blank is a blank instance of the type
* of object to read in.
**/
template <typename InserterT, typename T>
static void in(std::istream &in,
InserterT insert,
T blank);
/**
* Input a collection of entites with a specialized input function.
*/
template <typename InserterT, typename T>
static void in(std::istream &in, InserterT inserter,
void (*loadFunc)(std::istream&, T&),
T blank);
/**
* Input a collection of pointers to identifiable objects. This
* function is passed an inserter iterator (for example
* STL's back_inserter) where the pointers will be placed. This
* function is also passed a getter function from the environment
* (for example &Environment::getSubgrid).
**/
template <typename InserterT, typename T>
static void inIdents(std::istream &in,
InserterT insert,
T (*getter)(const std::string&));
/** Input an environmental map. */
template<typename T>
static void inEnvMap(std::istream &in, std::map<std::string, T*> &map);
///@}
};
template <typename T>
void BinIO::out(std::ostream &out, T* A, int size) {
// output size of collection
BinIO::out(out, size);
// output each entity in the array individually
for(int i = 0; i < size; i++) {
BinIO::out(out, A[i]);
}
}
template <typename T>
void BinIO::out(std::ostream &out, T* A, int size,
void (*saveFunc)(std::ostream&, const T&))
{
// output size of collection
BinIO::out(out, size);
// output each entity in the array individually using the save function
for(int i = 0; i < size; i++) {
saveFunc(out, A[i]);
}
}
template <typename IteratorT>
void BinIO::out(std::ostream &out, IteratorT b, IteratorT e)
{
// output size of collection
int sz = distance(b, e);
BinIO::out(out, sz);
// output each entity in the collection individually
while(b != e) {
BinIO::out(out, (*b));
b++;
}
}
template <typename IteratorT, typename T>
void BinIO::out(std::ostream &out, IteratorT b, IteratorT e,
void (*saveFunc)(std::ostream&, const T&))
{
// output size of collection
int sz = distance(b, e);
BinIO::out(out, sz);
// output each entity in the collection individually using the
// save function
while(b != e) {
saveFunc(out, *b);
b++;
}
}
template <typename T>
void BinIO::out(std::ostream &out, const std::vector<T> &v) {
typename std::vector<T>::size_type sz;
int isz;
sz = v.size();
isz = sz;
BinIO::out(out, isz);
for(typename std::vector<T>::const_iterator i = v.begin();
i != v.end(); i++)
{
BinIO::out(out, *i);
}
}
template <typename IteratorT>
void BinIO::outIdents(std::ostream &out, IteratorT b, IteratorT e)
{
// output size of collection
int sz = distance(b, e);
BinIO::out(out, sz);
// output each identifier in the collection individually
while(b != e) {
BinIO::out(out, (*b)->getID());
b++;
}
}
template<typename T>
void BinIO::outEnvMap(std::ostream &out, const std::map<std::string, T*> &map) {
// Output size of the map
BinIO::out(out, (int)map.size());
// Output each (key, value) pair in the map
for(typename std::map<std::string, T*>::const_iterator i = map.begin();
i != map.end(); i++)
{
BinIO::out(out, i->first);
(i->second)->output(out);
}
}
template <typename T>
void BinIO::inIdent(std::istream &in, T *ptr,
T (*getter)(const std::string&))
{
std::string ident;
BinIO::in(in, ident);
*ptr = getter(ident);
}
template <typename T>
void BinIO::in(std::istream &in, T** A)
{
// input size of array
int sz;
BinIO::in(in, sz);
*A = new T [sz];
// input each entity in the collection individually
for(int i = 0; i < sz; i++) {
BinIO::in(in, (*A)[i]);
}
}
template <typename T>
void BinIO::in(std::istream &in, T** A,
void (*loadFunc)(std::istream&, T&))
{
// input size of array
int sz;
BinIO::in(in, sz);
*A = new T [sz];
// input each entity in the collection individually using the load
// function
for(int i = 0; i < sz; i++) {
loadFunc(in, (*A)[i]);
}
}
template <typename T>
void BinIO::in(std::istream &in, std::vector<T> &v)
{
int size;
BinIO::in(in, size);
v.resize(size);
for(int i = 0; i < size; i++) {
BinIO::in(in, v[i]);
}
}
template <typename InserterT, typename T>
void BinIO::in(std::istream &in, InserterT insert, T blank) {
// input size of collection
int sz;
BinIO::in(in, sz);
// output each entity in the collection individually
for(int i = 0; i < sz; i++) {
BinIO::in(in, blank);
*insert = blank;
}
}
template <typename InserterT, typename T>
void BinIO::in(std::istream &in,
InserterT inserter,
void (*loadFunc)(std::istream&, T&),
T blank)
{
// input size of collection
int sz;
BinIO::in(in, sz);
// input each entity in the collection individually using loadFunc
for(int i = 0; i < sz; i++) {
T tmp;
loadFunc(in, tmp);
*inserter = tmp;
}
}
template <typename InserterT, typename T>
void BinIO::inIdents(std::istream &in, InserterT insert,
T (*getter)(const std::string&))
{
std::string ident;
int size;
BinIO::in(in, size);
for(int i = 0; i < size; i++) {
BinIO::in(in, ident);
*insert = getter(ident);
}
}
template<typename T>
void BinIO::inEnvMap(std::istream &in, std::map<std::string, T*> &map)
{
int size;
std::string ident;
T *value;
// Input size of map
BinIO::in(in, size);
// Read in each element of the map
for(int i = 0; i < size; i++) {
BinIO::in(in, ident);
Environment::newObj(ident, &value);
BinIO::in(in, *value);
}
}
#endif