Debian Codegraph @

examples/external_inheritance_example.cc

raw
1 //
2 // The following program illustrates the usage of "geners" package
3 // serialization mechanisms with an inheritance hierarchy over which
4 // you have no control -- in particular, the classes in this hierarchy
5 // do not have virtual "classId" and "write" methods. Naturally, it is
6 // assumed that these classes allow for complete examination of their
7 // state.
8 //
9 // In this case, the implemented serialization mechanism relies on
10 // the parallel hierarchy of I/O classes which wrap the "functional"
11 // classes and provide both read and write methods. The I/O classes
12 // are placed into an I/O factory which automatically chooses proper
13 // wrapper class for each functional class. The factory implementation
14 // assumes that your compiler supports C++11.
15 //
16 // The following classes are used in this example: ExtBase (pure virtual
17 // base class), ExtDerivedA (derived from ExtBase), and ExtDerivedB
18 // (derived from ExtDerivedA).
19 //
20 // The I/O factory for this hierarchy is implemented inside files
21 // ExtBaseIO.hh and ExtBaseIO.cc. Note that I/O wrappers for all
22 // concrete classes in the inheritance hierarchy are registered with
23 // this factory inside its constructor (see ExtBaseIO.cc).
24 //
25 // The wrappers for concrete classes are implemented in files
26 // ExtDerivedAIO.{hh,cc} and ExtDerivedBIO.{hh,cc}.
27 //
28 #include <memory>
29 #include <iostream>
30
31 #include "geners/CPP11_config.hh"
32 #ifdef CPP11_STD_AVAILABLE
33
34 #include "ExtDerivedAIO.hh"
35 #include "ExtDerivedBIO.hh"
36
37 #include "geners/vectorIO.hh"
38 #include "geners/StringArchive.hh"
39 #include "geners/Record.hh"
40 #include "geners/Reference.hh"
41
42 using namespace gs;
43 using namespace std;
44
45 int main(int, char**)
46 {
47 // The archive
48 gs::StringArchive ar;
49
50 // Objects we want to serialize
51 const ExtDerivedA a1(5);
52 const ExtDerivedA a2(8);
53 const ExtDerivedB b1(6, 7.0);
54 const ExtDerivedB b2(9, 10.0);
55 const ExtDerivedB b3(11, 12.0);
56
57 std::vector<std::shared_ptr<ExtBase> > v;
58 v.push_back(std::shared_ptr<ExtBase>(new ExtDerivedA(15)));
59 v.push_back(std::shared_ptr<ExtBase>(new ExtDerivedB(16, 17.0)));
60
61 // Write out the items
62 ar << gs::Record(a1, "saved A", "")
63 << gs::Record(dynamic_cast<const ExtBase&>(a2), "A saved by base ref", "")
64 << gs::Record(b1, "saved B", "")
65 << gs::Record(dynamic_cast<const ExtBase&>(b2), "B saved by base ref", "")
66 << gs::Record(dynamic_cast<const ExtDerivedA&>(b3),
67 "B saved by intermediate base ref", "")
68 << gs::Record(v, "vector of heterogeneous pointers", "");
69
70 // Retrieve the items and compare with originals
71 gs::Reference<ExtDerivedA> ra1(ar, "saved A", "");
72 assert(*ra1.get(0) == a1);
73
74 // Objects of concrete classes saved by their own references
75 // can also be retrieved on the stack (of course, under the
76 // condition that the class has the assignment operator). Objects
77 // saved using base references can only be retrieved on the heap.
78 ExtDerivedA tmpa(2323);
79 ra1.restore(0, &tmpa);
80 assert(tmpa == a1);
81
82 gs::Reference<ExtBase> ra2(ar, "A saved by base ref", "");
83 assert(dynamic_cast<ExtDerivedA&>(*ra2.get(0)) == a2);
84
85 gs::Reference<ExtDerivedB> rb1(ar, "saved B", "");
86 assert(*rb1.get(0) == b1);
87
88 ExtDerivedB tmpb(678, 5.0);
89 rb1.restore(0, &tmpb);
90 assert(tmpb == b1);
91
92 gs::Reference<ExtBase> rb2(ar, "B saved by base ref", "");
93 assert(dynamic_cast<ExtDerivedB&>(*rb2.get(0)) == b2);
94
95 gs::Reference<ExtDerivedA> rb3(ar, "B saved by intermediate base ref", "");
96 assert(dynamic_cast<ExtDerivedB&>(*rb3.get(0)) == b3);
97
98 std::vector<std::shared_ptr<ExtBase> > v2;
99 gs::Reference<decltype(v2)> r(ar, "vector of heterogeneous pointers", "");
100 r.restore(0, &v2);
101 assert(dynamic_cast<ExtDerivedA&>(*v[0])==dynamic_cast<ExtDerivedA&>(*v2[0]));
102 assert(dynamic_cast<ExtDerivedB&>(*v[1])==dynamic_cast<ExtDerivedB&>(*v2[1]));
103
104 return 0;
105 }
106
107 #else // CPP11_STD_AVAILABLE
108
109 int main(int, char**)
110 {
111 std::cout << "To run this example, please recompile "
112 << "the package with C++11 features enabled" << std::endl;
113 return 0;
114 }
115
116 #endif // CPP11_STD_AVAILABLE