Debian Codegraph @

examples/optimized_tuple_io.cc

raw
1 //
2 // The "geners" package provides a wrapper template for std::tuple called
3 // "VarPack". VarPack implements "write" and "restore" methods, and it is
4 // optimized for overwriting the same stack-based object many times. The
5 // serialized representation of VarPack is identical to std::tuple, so
6 // VarPack uses class name "std::tuple" for I/O purposes. std::tuple objects
7 // stored in "geners" archives can also be retrieved as VarPacks, and
8 // stored VarPack objects can also be retrieved as std::tuples.
9 //
10 // Do not store collections of VarPacks, this is not what they are intended
11 // for. While it is possible to use std::vector<VarPack<...> >, its I/O will
12 // be less efficient than that of std::vector<std::tuple<...> >.
13 //
14 // By default, VarPack checks the class ids of its template parameters on
15 // the first use only. This means that default VarPack retrieval mechanism
16 // is inappropriate for use with multiple archives (or one combined archive)
17 // which were written using different I/O versions of a class used as
18 // a VarPack template parameter. If you have to work with such archives,
19 // call "checkTypeEveryTime(true)" method of the VarPack object immediately
20 // after the object is created. This will slow down the VarPack I/O to
21 // about the speed of std::tuple I/O, but the class versioning will work
22 // properly.
23 //
24 // Both std::tuple and VarPack are often convenient to use as containers of
25 // pointers. There is one problem, however. Pointers are "loud" objects
26 // for I/O purposes -- when the serialization system encounters a pointer
27 // (bare, shared_ptr, or unique_ptr), it normally emits the class id of
28 // the pointee into the data stream. This is because the most common use
29 // of pointers for I/O purposes consists in storing a container of pointers
30 // to a base class in some kind of an inheritance hierarchy. AFAIK, there
31 // is no way to check at the compile time whether the "classId" method of
32 // a class is virtual or not, so the class id is written anyway, even in
33 // case there is no inheritance hierarchy to take care of. To tell the
34 // system that it is not necessary to write out the class id of the pointee,
35 // a special "silent" pointer type, IOPtr, should be used.
36 //
37 // This example illustrates usage of VarPack and IOPtr. It can be compiled
38 // only if C++11 support is enabled.
39 //
40 #include <cassert>
41 #include <iostream>
42
43 #include "geners/VarPack.hh"
44 #include "geners/IOPtr.hh"
45
46 #include "geners/StringArchive.hh"
47 #include "geners/Record.hh"
48 #include "geners/Reference.hh"
49
50 using namespace gs;
51 using namespace std;
52
53 #ifdef CPP11_STD_AVAILABLE
54
55 int main(int, char**)
56 {
57 // The archive to use
58 StringArchive ar;
59
60 // Pretend that we need the folowing variables to perform a certain
61 // calculation
62 int i = 15;
63 double d = 25.0;
64 float f = 3.f;
65
66 // Make a tuple (VarPack) which effectively acts as a struct for I/O
67 // purposes. Use "silent" pointers.
68 //
69 auto t = make_VarPack(
70 make_IOPtr(i),
71 make_IOPtr(d),
72 make_IOPtr(f));
73 ar << Record(t, "pack1", "top");
74
75 // Now, change the values of the variables and save them again via
76 // the tuple. Naturally, it is expected that in a real calculation
77 // this will be done in a cycle (over spreadsheet rows, etc).
78 i = 123;
79 d = 33.0;
80 f = 7.f;
81 ar << Record(t, "pack1", "top");
82
83 // Make sure that we can read the data back from the archive
84 int i2(0);
85 double d2(0.0);
86 float f2(0.f);
87 auto t2 = make_VarPack(
88 make_IOPtr(i2),
89 make_IOPtr(d2),
90 make_IOPtr(f2));
91 Reference<decltype(t2)> ref(ar, "pack1", "top");
92 assert(ref.size() == 2);
93
94 ref.restore(0, &t2);
95 assert(i2 == 15);
96 assert(d2 == 25.0);
97 assert(f2 == 3.f);
98
99 ref.restore(1, &t2);
100 assert(i2 == 123);
101 assert(d2 == 33.0);
102 assert(f2 == 7.f);
103
104 // We are done
105 return 0;
106 }
107
108 #else // CPP11_STD_AVAILABLE
109
110 int main(int, char**)
111 {
112 cout << "To run this example, please recompile "
113 << "the package with C++11 features enabled" << endl;
114 return 0;
115 }
116
117 #endif // CPP11_STD_AVAILABLE