Debian Codegraph @

examples/storing_user_templates.cc

raw
1 //
2 // The following program illustrates usage of the archive I/O in
3 // the "geners" package with user-developed template classes.
4 // Please read the comments in header files "SerializableTemplate1.hh"
5 // and "SerializableTemplate2.hh" for more details.
6 //
7 #include <cassert>
8
9 #include "geners/StringArchive.hh"
10 #include "geners/Record.hh"
11 #include "geners/Reference.hh"
12
13 #include "SimpleSerializable2.hh"
14 #include "SerializableTemplate1.hh"
15 #include "SerializableTemplate2.hh"
16
17 using namespace gs;
18
19 int main(int, char**)
20 {
21 // The archive to use
22 StringArchive ar;
23
24 // The objects which we want to store in the archive
25 const SerializableTemplate1<int> t1(123);
26 const SerializableTemplate2<SimpleSerializable2> t2(SimpleSerializable2(3.0));
27
28 // Store the objects in the archive
29 ar << Record(t1, "First object", "Top")
30 << Record(t2, "Second object", "Top");
31
32 // Read the objects back and make sure that the archived copies and
33 // the originals are the same
34 SerializableTemplate1<int> t1_2;
35 Reference< SerializableTemplate1<int> >(
36 ar, "First object", "Top").restore(0, &t1_2);
37 assert(t1 == t1_2);
38
39 CPP11_auto_ptr<SerializableTemplate2<SimpleSerializable2> > t2_2 =
40 Reference<SerializableTemplate2<SimpleSerializable2> >(
41 ar, "Second object", "Top").get(0);
42 assert(t2 == *t2_2);
43
44 // We are done
45 return 0;
46 }