Debian Codegraph @

examples/serializing_external_classes.cc

raw
1 //
2 // The following program illustrates usage of the archive I/O
3 // in the "geners" package with classes whose code can not be
4 // modified to play nicely with this I/O facility (e.g., they
5 // are members of external libraries owned by somebody else).
6 // To make the I/O of such classes meaningful, interfaces to such
7 // classes must expose sufficient amount of detail so that the
8 // complete object state can be captured and later restored.
9 //
10 // The technique illustrated here will not work for members of
11 // external inheritance hierarchies which can be addressed by
12 // pointer or reference to the base (slicing will occur).
13 //
14 #include <cmath>
15
16 // We will pretend that the following class belongs to some other library
17 namespace other {
18 class Point2d
19 {
20 public:
21 inline Point2d(double x, double y) : x_(x), y_(y) {}
22
23 inline double x() const {return x_;}
24 inline double y() const {return y_;}
25 inline double distanceToOrigin() const {return sqrt(x_*x_ + y_*y_);}
26
27 inline bool operator==(const Point2d& r) const
28 {return x_ == r.x_ && y_ == r.y_;}
29 inline bool operator!=(const Point2d& r) const
30 {return !(*this == r);}
31
32 private:
33 Point2d();
34 double x_;
35 double y_;
36 };
37 }
38
39 // The following header has to be included in order to wrap external
40 // classes or templates
41 #include "geners/GenericIO.hh"
42
43 // The next thing to do is to specialize the ClassIdSpecialization template
44 // so that a ClassId object can be associated with the class we want to
45 // serialize. The easiest way to do this is to use the "gs_specialize_class_id"
46 // macro (other similar macros exist which work for templates). The first
47 // argument of the macro is the class name (which will be stringized) and
48 // the second argument is the version number.
49 //
50 gs_specialize_class_id(other::Point2d, 1)
51
52 // The following macro tells the serialization system that the type is
53 // "external" for the I/O purposes so that the system will not try to
54 // apply its default serialization methods
55 //
56 gs_declare_type_external(other::Point2d)
57
58 // The most arcane part is to specialize the behavior of the two
59 // template classes at the heart of the serialization facility:
60 // gs::GenericWriter and gs::GenericReader.
61 //
62 namespace gs {
63 //
64 // This is how the specialization of GenericWriter should look like
65 //
66 template <class Stream, class State>
67 struct GenericWriter<Stream, State, other::Point2d,
68 Int2Type<IOTraits<int>::ISEXTERNAL> >
69 {
70 inline static bool process(const other::Point2d& point, Stream& os,
71 State*, const bool processClassId)
72 {
73 // If necessary, serialize the class id
74 static const ClassId current(ClassId::makeId<other::Point2d>());
75 const bool status = processClassId ? current.write(os) : true;
76
77 // Serialize object data if the class id was successfully
78 // written out
79 if (status)
80 {
81 write_pod(os, point.x());
82 write_pod(os, point.y());
83 }
84
85 // Return "true" on success, "false" on failure
86 return status && !os.fail();
87 }
88 };
89
90 // And this is the specialization of GenericReader
91 //
92 template <class Stream, class State>
93 struct GenericReader<Stream, State, other::Point2d,
94 Int2Type<IOTraits<int>::ISEXTERNAL> >
95 {
96 inline static bool readIntoPtr(other::Point2d*& ptr, Stream& is,
97 State* st, const bool processClassId)
98 {
99 // Make sure that the serialized class id is consistent with
100 // the current one
101 static const ClassId current(ClassId::makeId<other::Point2d>());
102 const ClassId& stored = processClassId ? ClassId(is,1) : st->back();
103 current.ensureSameId(stored);
104
105 // Deserialize object data. This part, of course, has to be
106 // consistent with the corresponding GenericWriter code and
107 // can depend on what stored.version() returns. If there can
108 // be several I/O versions of the serialized class, replace
109 // the "ensureSameId" call used above with "ensureSameName".
110 double x=0, y=0;
111 read_pod(is, &x);
112 read_pod(is, &y);
113 if (is.fail())
114 // Return "false" on failure
115 return false;
116
117 // Build the object from the stored data
118 if (ptr)
119 *ptr = other::Point2d(x, y);
120 else
121 ptr = new other::Point2d(x, y);
122 return true;
123 }
124
125 inline static bool process(other::Point2d& s, Stream& is,
126 State* st, const bool processClassId)
127 {
128 // Simply convert reading by reference into reading by pointer
129 other::Point2d* ps = &s;
130 return readIntoPtr(ps, is, st, processClassId);
131 }
132 };
133 }
134
135 //
136 // Now, we can use other::Point2d class with the archives
137 // just as if it had the read/write methods, as illustrated
138 // by the code below
139 //
140 #include "geners/StringArchive.hh"
141 #include "geners/Record.hh"
142 #include "geners/Reference.hh"
143
144 using namespace gs;
145 using namespace other;
146
147 int main(int, char**)
148 {
149 // The archive to use
150 StringArchive ar;
151
152 // The objects which we want to store in the archive
153 Point2d p1(0, 1);
154 Point2d p2(1, 2);
155
156 // Store the objects
157 ar << Record(p1, "First point", "Top")
158 << Record(p2, "Second point", "Top");
159
160 // Read the objects back and check that the retrived objects
161 // are identical to the original ones. First, restore an object
162 // on the stack...
163 Point2d p2_2(0, 0);
164 Reference<Point2d>(ar, "Second point", "Top").restore(0, &p2_2);
165 assert(p2 == p2_2);
166
167 // and now fetch another on the heap
168 CPP11_auto_ptr<Point2d> p1_2 = Reference<Point2d>(
169 ar, "First point", "Top").get(0);
170 assert(p1 == *p1_2);
171
172 // We are done
173 return 0;
174 }