Debian Codegraph @

examples/ExternalTemplateIO.hh

raw
1 #ifndef GSEXAMPLES_EXTERNALTEMPLATEIO_HH_
2 #define GSEXAMPLES_EXTERNALTEMPLATEIO_HH_
3
4 // Header file for the template we want to wrap
5 #include "ExternalTemplate.hh"
6
7 // The following header has to be included in order to wrap external
8 // classes or templates
9 #include "geners/GenericIO.hh"
10 #include "geners/IOException.hh"
11
12 // Specialize the ClassIdSpecialization template so that a ClassId object
13 // can be associated with the template we want to serialize. The easiest
14 // way to do this is to use the "gs_specialize_template_id_TT" macro.
15 // The first argument of the macro is the version number. The second
16 // argument tells how many template parameters affect the serialized
17 // representation (it is assumed that the parameters that do affect
18 // serialized representation come earlier in the parameter sequence).
19 // For example, if the second template parameter was not affecting the
20 // representation (e.g., as the allocator class for std::vector) then
21 // the second macro argument would be 1. The number of "T"s in _TT should
22 // correspond to the total number of template parameters (up to six).
23 //
24 gs_specialize_template_id_TT(ExternalTemplate, 1, 2)
25 gs_specialize_template_id_T(ExternalTemplate2, 1, 0)
26
27 // The following macro tells the serialization system that the template
28 // is "external" for the I/O purposes so that the system will not try to
29 // apply its default serialization methods
30 //
31 gs_declare_template_external_TT(ExternalTemplate)
32 gs_declare_template_external_T(ExternalTemplate2)
33
34 // The most arcane part is to specialize the behavior of the two
35 // template classes at the heart of the serialization facility:
36 // gs::GenericWriter and gs::GenericReader.
37 //
38 namespace gs {
39 // Serialization code for class ExternalTemplate
40 //
41 template <class Stream, class State, class A, class B>
42 struct GenericWriter<Stream, State, ExternalTemplate<A,B>,
43 Int2Type<IOTraits<int>::ISEXTERNAL> >
44 {
45 inline static bool process(const ExternalTemplate<A,B>& s, Stream& os,
46 State*, const bool processClassId)
47 {
48 // If necessary, serialize the class id
49 static const ClassId current(ClassId::makeId<ExternalTemplate<A,B> >());
50 const bool status = processClassId ? current.write(os) : true;
51
52 // Serialize object data if the class id was successfully
53 // written out
54 if (status)
55 {
56 write_item(os, s.getA());
57 write_item(os, s.getB());
58 }
59
60 // Return "true" on success, "false" on failure
61 return status && !os.fail();
62 }
63 };
64
65 template <class Stream, class State, class A, class B>
66 struct GenericReader<Stream, State, ExternalTemplate<A,B>,
67 Int2Type<IOTraits<int>::ISEXTERNAL> >
68 {
69 inline static bool readIntoPtr(ExternalTemplate<A,B>*& ptr, Stream& is,
70 State* st, const bool processClassId)
71 {
72 // Make sure that the serialized class id is consistent with
73 // the current one
74 static const ClassId current(ClassId::makeId<ExternalTemplate<A,B> >());
75 const ClassId& stored = processClassId ? ClassId(is,1) : st->back();
76
77 // Check that the name is consistent. Do not check for the
78 // consistency of the complete id because we want to be able
79 // to read different versions of classes A and B.
80 current.ensureSameName(stored);
81
82 // The following code does not assume that A or B have
83 // default constructors
84 CPP11_auto_ptr<A> pa = read_item<A>(is);
85 CPP11_auto_ptr<B> pb = read_item<B>(is);
86 if (ptr == 0)
87 ptr = new ExternalTemplate<A,B>(*pa, *pb);
88 else
89 *ptr = ExternalTemplate<A,B>(*pa, *pb);
90 return true;
91 }
92
93 inline static bool process(ExternalTemplate<A,B>& s, Stream& is,
94 State* st, const bool processClassId)
95 {
96 // Simply convert reading by reference into reading by pointer
97 ExternalTemplate<A,B>* ps = &s;
98 return readIntoPtr(ps, is, st, processClassId);
99 }
100 };
101
102
103 // Serialization code for class ExternalTemplate2.
104 // Note that, in this case, A does not affect serialized
105 // representation of "ExternalTemplate2".
106 //
107 template <class Stream, class State, class A>
108 struct GenericWriter<Stream, State, ExternalTemplate2<A>,
109 Int2Type<IOTraits<int>::ISEXTERNAL> >
110 {
111 inline static bool process(const ExternalTemplate2<A>& s, Stream& os,
112 State*, const bool processClassId)
113 {
114 // If necessary, serialize the class id
115 static const ClassId current(ClassId::makeId<ExternalTemplate2<A> >());
116 const bool status = processClassId ? current.write(os) : true;
117 write_pod(os, s.getInt());
118
119 // Return "true" on success, "false" on failure
120 return status && !os.fail();
121 }
122 };
123
124 template <class Stream, class State, class A>
125 struct GenericReader<Stream, State, ExternalTemplate2<A>,
126 Int2Type<IOTraits<int>::ISEXTERNAL> >
127 {
128 inline static bool readIntoPtr(ExternalTemplate2<A>*& ptr, Stream& is,
129 State* st, const bool processClassId)
130 {
131 // Make sure that the serialized class id is consistent with
132 // the current one
133 static const ClassId current(ClassId::makeId<ExternalTemplate2<A> >());
134 const ClassId& stored = processClassId ? ClassId(is,1) : st->back();
135 current.ensureSameId(stored);
136
137 // The following code does not assume that A or B have
138 // default constructors
139 int i;
140 read_pod(is, &i);
141 if (is.fail()) throw IOReadFailure("Input stream failure "
142 "while reading ExternalTemplate2");
143 if (ptr == 0)
144 ptr = new ExternalTemplate2<A>(i);
145 else
146 *ptr = ExternalTemplate2<A>(i);
147 return true;
148 }
149
150 inline static bool process(ExternalTemplate2<A>& s, Stream& is,
151 State* st, const bool processClassId)
152 {
153 // Simply convert reading by reference into reading by pointer
154 ExternalTemplate2<A>* ps = &s;
155 return readIntoPtr(ps, is, st, processClassId);
156 }
157 };
158 }
159
160 #endif // GSEXAMPLES_EXTERNALTEMPLATEIO_HH_