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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
//
// Top-level API for serializing and deserializing arbitrary classes
//
#ifndef GENERS_GENERICIO_HH_
#define GENERS_GENERICIO_HH_
#include <memory>
#include "Alignment/Geners/interface/IOPointeeType.hh"
#include "Alignment/Geners/interface/binaryIO.hh"
#include "Alignment/Geners/interface/ArrayAdaptor.hh"
#include "Alignment/Geners/interface/ClearIfPointer.hh"
#include "Alignment/Geners/interface/StrippedType.hh"
namespace gs {
/**
// Generic top-level function which can be used to write out
// almost anything. Intended mainly for use inside "write"
// methods of user-developed classes and templates. Returns
// "true" if the argument item is successfully written out.
*/
template <class Stream, class Item>
inline bool write_item(Stream &os, const Item &item, const bool writeClassId = true) {
char *ps = nullptr;
return process_const_item<GenericWriter>(item, os, ps, writeClassId);
}
/**
// A function for overwriting existing objects (which usually live
// on the stack). This function actually skips couple levels of
// indirection which would be generated by a call to "process_item".
*/
template <class Stream, class Item>
inline void restore_item(Stream &is, Item *item, const bool readClassId = true) {
typedef std::vector<ClassId> State;
assert(item);
State state;
const bool status = GenericReader<Stream, State, Item *, Int2Type<IOTraits<int>::ISPOINTER>>::process(
item, is, &state, readClassId);
if (is.fail())
throw IOReadFailure("In gs::restore_item: input stream failure");
if (!status)
throw IOInvalidData("In gs::restore_item: invalid input stream data");
}
/**
// Function for returning objects on the heap. This function
// requires explicit specification of its first template
// parameter, the type of the item to read. This function
// either succeeds or throws an exception which inherits
// from std::exception.
*/
template <class Item, class Stream>
inline std::unique_ptr<Item> read_item(Stream &is, const bool readClassId = true) {
typedef std::vector<ClassId> State;
Item *item = nullptr;
State state;
const bool status = GenericReader<Stream, State, Item *, Int2Type<IOTraits<int>::ISNULLPOINTER>>::process(
item, is, &state, readClassId);
std::unique_ptr<Item> ptr(item);
if (is.fail())
throw IOReadFailure("In gs::read_item: input stream failure");
if (!status || item == nullptr)
throw IOInvalidData("In gs::read_item: invalid input stream data");
return ptr;
}
/**
// Generic top-level function for writing arrays. Note that
// the length of the array is not written out and that the
// length must be known in advance in the scope from which
// the companion function, "read_array", is called. "true"
// is returned upon success, "false" on failure.
*/
template <class Stream, class Item>
inline bool write_array(Stream &os, Item *items, const std::size_t length) {
char *ps = nullptr;
return process_const_item<GenericWriter>(ArrayAdaptor<Item>(items, length), os, ps, false);
}
/**
// Function for deserializing arrays. The length of the array
// must be known in the scope from which this function is invoked.
*/
template <class Stream, class Item>
inline void read_array(Stream &is, Item *items, const std::size_t length) {
typedef std::vector<ClassId> State;
State state;
ArrayAdaptor<Item> adap(items, length);
const bool st = process_item<GenericReader>(adap, is, &state, false);
if (is.fail())
throw IOReadFailure("In gs::read_array: input stream failure");
if (!st)
throw IOInvalidData("In gs::read_array: invalid input stream data");
}
} // namespace gs
namespace gs {
template <class Stream, class State, class Item, class Stage>
struct GenericWriter2 : public GenericWriter<Stream, State, Item, Stage> {};
template <class Stream, class State, class Item, class Stage>
struct GenericReader2 : public GenericReader<Stream, State, Item, Stage> {};
// The reader and writer templates should be specialized
// (that is, their "process" function should be defined) using
// the following processing stage types from "ProcessItem.hh":
//
// Int2Type<IOTraits<int>::ISPOD> (+readIntoPtr)
// InContainerHeader
// InContainerFooter
// InContainerSize
// InPODArray
// Int2Type<IOTraits<int>::ISWRITABLE>
// Int2Type<IOTraits<int>::ISPOINTER>
// Int2Type<IOTraits<int>::ISSHAREDPTR>
// Int2Type<IOTraits<int>::ISPAIR> (+readIntoPtr)
// Int2Type<IOTraits<int>::ISSTRING> (+readIntoPtr)
//
// In addition, the reader should be specialized for the following
// types:
//
// InContainerCycle (process only)
// Int2Type<IOTraits<int>::ISSTDCONTAINER> (readIntoPtr only)
// Int2Type<IOTraits<int>::ISHEAPREADABLE> (readIntoPtr only)
// Int2Type<IOTraits<int>::ISPLACEREADABLE> (readIntoPtr only)
//
// The resulting code is essentially one big compile-time state
// machine with two main switching hubs: "process_item" function
// from "ProcessItem.hh" and "process" function in GenericReader
// template specialized for bare pointers.
//
//===================================================================
//
// Processing of a POD
//
//===================================================================
template <class Stream, class State, class T>
struct GenericWriter<Stream, State, T, Int2Type<IOTraits<int>::ISPOD>> {
inline static bool process(const T &s, Stream &os, State *, const bool processClassId) {
static const ClassId current(ClassId::makeId<T>());
const bool status = processClassId ? current.write(os) : true;
if (status)
write_pod(os, s);
return status && !os.fail();
}
};
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISPOD>> {
inline static bool readIntoPtr(T *&ptr, Stream &str, State *, const bool processClassId) {
std::unique_ptr<T> myptr;
if (ptr == nullptr)
myptr = std::unique_ptr<T>(new T());
if (processClassId) {
static const ClassId current(ClassId::makeId<T>());
ClassId id(str, 1);
current.ensureSameName(id);
}
read_pod(str, ptr ? ptr : myptr.get());
if (str.fail())
return false;
if (ptr == nullptr)
ptr = myptr.release();
return true;
}
inline static bool process(T &s, Stream &os, State *st, const bool processClassId) {
T *ps = &s;
return readIntoPtr(ps, os, st, processClassId);
}
};
//===================================================================
//
// Processing of a container header
//
//===================================================================
template <class Stream, class State, class Container>
struct GenericWriter<Stream, State, Container, InContainerHeader> {
inline static bool process(const Container &, Stream &os, State *, const bool processClassId) {
typedef typename Container::value_type T;
static const ClassId current(ClassId::makeId<Container>());
bool status = processClassId ? current.write(os) : true;
// Maybe we do not have to write out the container class id,
// but we do have to write out the item class id -- unless the
// container is just an array of pods. Otherwise we might not
// be able to read the container items back.
if (status && !(IOTraits<T>::IsPOD && IOTraits<Container>::IsContiguous)) {
static const ClassId itemId(ClassId::makeId<T>());
status = itemId.write(os);
}
return status;
}
};
template <class Stream, class State, class Container>
struct GenericReader<Stream, State, Container, InContainerHeader> {
inline static bool process(Container &a, Stream &is, State *state, const bool processClassId) {
typedef typename Container::value_type T;
if (processClassId) {
static const ClassId current(ClassId::makeId<Container>());
ClassId id(is, 1);
current.ensureSameName(id);
}
a.clear();
if (!(IOTraits<T>::IsPOD && IOTraits<Container>::IsContiguous)) {
ClassId id(is, 1);
// Remember the class id of the contained items.
// We need to do this even if the id is invalid because
// the id will be popped back when the "InContainerFooter"
// stage is processed.
state->push_back(id);
}
return true;
}
};
//===================================================================
//
// Processing of a container footer
//
//===================================================================
template <class Stream, class State, class Container>
struct GenericWriter<Stream, State, Container, InContainerFooter> {
inline static bool process(const Container &, Stream &, State *, bool) { return true; }
};
template <class Stream, class State, class Container>
struct GenericReader<Stream, State, Container, InContainerFooter> {
inline static bool process(Container &, Stream &, State *state, bool) {
typedef typename Container::value_type T;
if (!(IOTraits<T>::IsPOD && IOTraits<Container>::IsContiguous))
state->pop_back();
return true;
}
};
//===================================================================
//
// Processing of container size
//
//===================================================================
template <class Stream, class State, class Container>
struct GenericWriter<Stream, State, Container, InContainerSize> {
inline static bool process(const std::size_t &sz, Stream &os, State *, bool /* processClassId */) {
write_pod(os, sz);
return !os.fail();
}
};
template <class Stream, class State, class Container>
struct GenericReader<Stream, State, Container, InContainerSize> {
inline static bool process(std::size_t &sz, Stream &is, State *, bool /* processClassId */) {
read_pod(is, &sz);
return !is.fail();
}
};
//===================================================================
//
// Processing of data in contiguous POD containers
//
//===================================================================
template <class Stream, class State, class ArrayLike>
struct GenericWriter<Stream, State, ArrayLike, InPODArray> {
inline static bool process(const ArrayLike &a, Stream &os, State *, bool) {
const std::size_t len = a.size();
write_pod(os, len);
if (len)
write_pod_array(os, &a[0], len);
return !os.fail();
}
};
template <class Stream, class State, class ArrayLike>
struct GenericReader<Stream, State, ArrayLike, InPODArray> {
inline static bool process(ArrayLike &a, Stream &s, State *, bool) {
std::size_t len = 0;
read_pod(s, &len);
if (s.fail())
return false;
a.resize(len);
if (!len)
return true;
read_pod_array(s, &a[0], len);
return !s.fail();
}
};
//===================================================================
//
// Processing of "writable" objects
//
//===================================================================
template <class Stream, class State, class T>
struct GenericWriter<Stream, State, T, Int2Type<IOTraits<int>::ISWRITABLE>> {
inline static bool process(const T &s, Stream &os, State *, const bool processClassId) {
return (processClassId ? s.classId().write(os) : true) && s.write(os) && !os.fail();
}
};
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISWRITABLE>> {
inline static bool process(T &s, Stream &is, State *st, const bool processClassId) {
typedef IOTraits<T> M;
T *ps = &s;
return GenericReader<Stream, State, T, Int2Type<M::Signature &(M::ISPLACEREADABLE | M::ISHEAPREADABLE)>>::
readIntoPtr(ps, is, st, processClassId);
}
};
//===================================================================
//
// Processing of bare pointers.
//
// The writer simply dereferences the pointer.
//
// In the reader, we want to read stuff into the pointee object,
// or want to create an item on the heap if the pointer value is 0.
//
//===================================================================
template <class Stream, class State, class Ptr>
struct GenericWriter<Stream, State, Ptr, Int2Type<IOTraits<int>::ISPOINTER>> {
inline static bool process(const Ptr &ptr, Stream &os, State *s, const bool processClassId) {
// Can't have pointers to pointers. This is a design
// decision which simplifies things considerably.
typedef typename IOPointeeType<Ptr>::type Pointee;
typedef IOTraits<Pointee> M;
static_assert((M::Signature & (M::ISPOINTER | M::ISSHAREDPTR)) == 0, "can not write pointers to pointers");
// Can't have NULL pointers either. But this
// can be checked at run time only.
assert(ptr);
return process_const_item<GenericWriter2>(*ptr, os, s, processClassId);
}
};
template <class Stream, class State, class Ptr>
struct GenericReader<Stream, State, Ptr, Int2Type<IOTraits<int>::ISPOINTER>> {
inline static bool process(Ptr &ptr, Stream &str, State *s, const bool processClassId) {
// We need to figure out the type of the pointee
// and make a swich depending on that type.
// Note that the pointee itself can not be a pointer.
typedef typename IOPointeeType<Ptr>::type Pointee;
typedef IOTraits<Pointee> M;
static_assert((M::Signature & (M::ISPOINTER | M::ISSHAREDPTR)) == 0, "can not read pointers to pointers");
return GenericReader<
Stream,
State,
Pointee,
Int2Type<M::Signature &(M::ISPOD | M::ISSTDCONTAINER | M::ISHEAPREADABLE | M::ISPLACEREADABLE | M::ISPAIR |
M::ISTUPLE | M::ISEXTERNAL | M::ISSTRING)>>::readIntoPtr(ptr, str, s, processClassId);
}
};
template <class Stream, class State, class Ptr>
struct GenericReader<Stream, State, Ptr, Int2Type<IOTraits<int>::ISNULLPOINTER>> {
inline static bool process(Ptr &ptr, Stream &str, State *s, const bool processClassId) {
// We need to figure out the type of the pointee
// and make a swich depending on that type.
// Note that the pointee itself can not be a pointer.
typedef typename IOPointeeType<Ptr>::type Pointee;
typedef IOTraits<Pointee> M;
static_assert((M::Signature & (M::ISNULLPOINTER | M::ISSHAREDPTR)) == 0, "can not read pointers to pointers");
return GenericReader<
Stream,
State,
Pointee,
Int2Type<M::Signature &(M::ISPOD | M::ISSTDCONTAINER | M::ISPUREHEAPREADABLE | M::ISPLACEREADABLE | M::ISPAIR |
M::ISTUPLE | M::ISEXTERNAL | M::ISSTRING)>>::readIntoPtr(ptr, str, s, processClassId);
}
};
//===================================================================
//
// Processing of shared pointers -- similar logic to pointers.
// For the reader, handling of the shared pointer is reduced
// to handling of a normal pointer with 0 value.
//
//===================================================================
template <class Stream, class State, class Ptr>
struct GenericWriter<Stream, State, Ptr, Int2Type<IOTraits<int>::ISSHAREDPTR>> {
inline static bool process(const Ptr &ptr, Stream &os, State *s, const bool processClassId) {
typedef typename Ptr::element_type Pointee;
typedef IOTraits<Pointee> M;
static_assert((M::Signature & (M::ISPOINTER | M::ISSHAREDPTR)) == 0, "can not write pointers to pointers");
assert(ptr.get());
return process_const_item<GenericWriter2>(*ptr, os, s, processClassId);
}
};
template <class Stream, class State, class ShPtr>
struct GenericReader<Stream, State, ShPtr, Int2Type<IOTraits<int>::ISSHAREDPTR>> {
inline static bool process(ShPtr &a, Stream &str, State *s, const bool processClassId) {
typedef typename ShPtr::element_type Pointee;
typedef IOTraits<Pointee> M;
static_assert((M::Signature & (M::ISPOINTER | M::ISSHAREDPTR)) == 0, "can not read pointers to pointers");
Pointee *ptr = 0;
const bool status = GenericReader<Stream, State, Pointee *, Int2Type<IOTraits<int>::ISNULLPOINTER>>::process(
ptr, str, s, processClassId);
if (status) {
assert(ptr);
a = std::shared_ptr<Pointee>(ptr);
return true;
} else {
delete ptr;
return false;
}
}
};
//===================================================================
//
// Processing of std::pair
//
//===================================================================
template <class Stream, class State, class T>
struct GenericWriter<Stream, State, T, Int2Type<IOTraits<int>::ISPAIR>> {
inline static bool process(const T &s, Stream &os, State *st, const bool processClassId) {
// Here is a little problem: in this scope "GenericWriter"
// means GenericWriter<Stream, State, T,
// Int2Type<IOTraits<int>::ISPAIR> >
// However, we want to use the whole template, unspecialized.
// This is why "GenericWriter2" is introduced: a copy of
// "GenericWriter" via public inheritance.
static const ClassId current(ClassId::makeId<T>());
return (processClassId ? current.write(os) : true) &&
process_const_item<GenericWriter2>(s.first, os, st, false) &&
process_const_item<GenericWriter2>(s.second, os, st, false);
}
};
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISPAIR>> {
inline static bool readIntoPtr(T *&ptr, Stream &str, State *s, const bool processClassId) {
std::unique_ptr<T> myptr;
if (ptr == 0) {
myptr = std::unique_ptr<T>(new T());
clearIfPointer(myptr.get()->first);
clearIfPointer(myptr.get()->second);
}
std::vector<std::vector<ClassId>> itemIds;
if (processClassId) {
static const ClassId current(ClassId::makeId<T>());
ClassId pairId(str, 1);
current.ensureSameName(pairId);
pairId.templateParameters(&itemIds);
assert(itemIds.size() == 2U);
} else {
assert(!s->empty());
s->back().templateParameters(&itemIds);
if (itemIds.size() != 2U) {
std::string err(
"In gs::GenericReader::readIntoPtr: "
"bad class id for std::pair on the "
"class id stack: ");
err += s->back().id();
throw IOInvalidData(err);
}
}
if (!(process_item<GenericReader2>((ptr ? ptr : myptr.get())->first, str, &itemIds[0], false) &&
process_item<GenericReader2>((ptr ? ptr : myptr.get())->second, str, &itemIds[1], false)))
return false;
if (ptr == 0)
ptr = myptr.release();
return true;
}
inline static bool process(T &s, Stream &os, State *st, const bool processClassId) {
T *ps = &s;
return readIntoPtr(ps, os, st, processClassId);
}
};
//===================================================================
//
// Processing of std::string
//
//===================================================================
template <class Stream, class State>
struct GenericWriter<Stream, State, std::string, Int2Type<IOTraits<int>::ISSTRING>> {
inline static bool process(const std::string &s, Stream &os, State *, const bool processClassId) {
static const ClassId current(ClassId::makeId<std::string>());
const bool status = processClassId ? current.write(os) : true;
if (status)
write_string<char>(os, s);
return status && !os.fail();
}
};
template <class Stream, class State>
struct GenericReader<Stream, State, std::string, Int2Type<IOTraits<int>::ISSTRING>> {
inline static bool readIntoPtr(std::string *&ptr, Stream &is, State *, const bool processClassId) {
std::unique_ptr<std::string> myptr;
if (ptr == nullptr)
myptr = std::make_unique<std::string>();
if (processClassId) {
static const ClassId current(ClassId::makeId<std::string>());
ClassId id(is, 1);
current.ensureSameName(id);
}
read_string<char>(is, ptr ? ptr : myptr.get());
if (is.fail())
return false;
if (ptr == nullptr)
ptr = myptr.release();
return true;
}
inline static bool process(std::string &s, Stream &is, State *st, const bool processClassId) {
std::string *ptr = &s;
return readIntoPtr(ptr, is, st, processClassId);
}
};
//===================================================================
//
// Processing of container readout
//
//===================================================================
template <class Stream, class State, class Container>
struct GenericReader<Stream, State, Container, InContainerCycle> {
private:
typedef typename Container::value_type item_type;
typedef IOTraits<item_type> M;
// Item is a simple pointer
inline static bool process2(Container &obj, Stream &is, State *st, const std::size_t itemN, Int2Type<1>) {
item_type ptr = 0;
const bool status =
GenericReader<Stream, State, item_type, Int2Type<IOTraits<int>::ISNULLPOINTER>>::process(ptr, is, st, true);
if (status) {
assert(ptr);
InsertContainerItem<Container>::insert(obj, ptr, itemN);
} else
delete ptr;
return status;
}
// Item is a shared pointer
inline static bool process2(Container &obj, Stream &is, State *st, const std::size_t itemN, Int2Type<2>) {
typedef typename item_type::element_type Pointee;
Pointee *ptr = 0;
const bool status =
GenericReader<Stream, State, Pointee *, Int2Type<IOTraits<int>::ISNULLPOINTER>>::process(ptr, is, st, true);
if (status) {
assert(ptr);
std::shared_ptr<Pointee> sptr(ptr);
InsertContainerItem<Container>::insert(obj, sptr, itemN);
} else
delete ptr;
return status;
}
// Item is heap-readable
inline static bool process2(Container &obj, Stream &is, State *st, const std::size_t itemN, Int2Type<3>) {
// No class id -- this is a member of a container
assert(!st->empty());
item_type *ptr = item_type::read(st->back(), is);
if (ptr) {
InsertContainerItem<Container>::insert(obj, *ptr, itemN);
delete ptr;
}
return ptr;
}
// Item is not a pointer and not heap-readable.
// Assume that it has a default constructor.
inline static bool process2(Container &obj, Stream &is, State *st, const std::size_t itemN, Int2Type<4>) {
typedef typename StrippedType<item_type>::type NCType;
NCType item;
NCType *pitem = &item;
bool status =
GenericReader<Stream, State, NCType *, Int2Type<IOTraits<int>::ISPOINTER>>::process(pitem, is, st, false);
if (status)
InsertContainerItem<Container>::insert(obj, item, itemN);
return status;
}
public:
inline static bool process(Container &obj, Stream &is, State *st, const std::size_t itemN) {
// By default, we will assume that container starts empty.
// Here, we need to produce a new item. There are 3 options:
// 1) make it on the stack, insert a copy into the container
// 2) make it on the heap, insert a copy, delete original
// 3) the container contains pointers to begin with, so
// we make it on the stack and add a pointer to the container
return process2(obj,
is,
st,
itemN,
Int2Type<M::IsPointer * 1 + M::IsSharedPtr * 2 + M::IsHeapReadable * 3 +
!(M::IsPointer || M::IsSharedPtr || M::IsHeapReadable) * 4>());
}
};
//===================================================================
//
// Reading things when a pointer is given
//
//===================================================================
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISSTDCONTAINER>> {
inline static bool readIntoPtr(T *&ptr, Stream &str, State *s, const bool processClassId) {
if (ptr)
return process_item<GenericReader2>(*ptr, str, s, processClassId);
else {
std::unique_ptr<T> myptr(new T());
if (!process_item<GenericReader2>(*myptr, str, s, processClassId))
return false;
ptr = myptr.release();
return true;
}
}
};
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISHEAPREADABLE>> {
inline static bool readIntoPtr(T *&ptr, Stream &str, State *s, const bool processClassId) {
T *readback = 0;
if (processClassId) {
ClassId id(str, 1);
readback = T::read(id, str);
} else {
assert(!s->empty());
readback = T::read(s->back(), str);
}
if (readback) {
if (ptr) {
try {
// We will assume here that the "read"
// operation takes precedence over constness
*const_cast<typename StrippedType<T>::type *>(ptr) = *readback;
} catch (...) {
delete readback;
throw;
}
delete readback;
} else
ptr = readback;
}
return readback;
}
};
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISPUREHEAPREADABLE>> {
inline static bool readIntoPtr(T *&ptr, Stream &str, State *s, const bool processClassId) {
T *readback = nullptr;
if (processClassId) {
ClassId id(str, 1);
readback = T::read(id, str);
} else {
assert(!s->empty());
readback = T::read(s->back(), str);
}
if (readback) {
assert(!ptr);
ptr = readback;
}
return readback;
}
};
template <class Stream, class State, class T>
struct GenericReader<Stream, State, T, Int2Type<IOTraits<int>::ISPLACEREADABLE>> {
inline static bool readIntoPtr(T *&ptr, Stream &str, State *s, const bool processClassId) {
std::unique_ptr<T> myptr;
if (ptr == 0)
myptr = std::unique_ptr<T>(new T());
if (processClassId) {
ClassId id(str, 1);
T::restore(id, str, ptr ? ptr : myptr.get());
} else {
assert(!s->empty());
T::restore(s->back(), str, ptr ? ptr : myptr.get());
}
if (ptr == 0)
ptr = myptr.release();
return ptr;
}
};
} // namespace gs
#endif // GENERS_GENERICIO_HH_
|