Debian Codegraph @

examples/storing_tuples.cc

raw
1 //
2 // The following program illustrates "geners" archive I/O for C++11
3 // tuples. These containers rely heavily on the new language feature
4 // called "variadic templates". Tuple is the first truly heterogeneous
5 // container in C++. There were no such containers in the previous C++
6 // standard -- struct was the closest thing. Compared to similar containers
7 // in dynamically typed languages (python, Tcl, etc), C++ tuple is less
8 // capable and has a somewhat clumsier interface. However, it does offer
9 // a type-safe alternative to creating a struct when a number of
10 // heterogeneous items must be grouped together for various purposes.
11 //
12 // The "geners" package provides a number of facilities for reading and
13 // writing both single tuples and tuple collections. Collections support
14 // the data processing mode in which precisely one tuple is needed in
15 // order to perform some meaningful calculation. Think about spreadsheet
16 // processing one row at a time, or accessing a database table one entry
17 // at a time, or looking at any other independent entities such as particle
18 // physics events produced by accelerators. See example files
19 // "optimized_tuple_io.cc" and "tuple_collections.cc" for more details
20 // about these facilities.
21 //
22 // This particular example can be compiled only if C++11 support is enabled.
23 //
24 #include <cassert>
25 #include <iostream>
26 #include <cstdlib>
27
28 // The following header is needed to read/write tuples
29 #include "geners/tupleIO.hh"
30
31 #include "geners/vectorIO.hh"
32 #include "geners/StringArchive.hh"
33 #include "geners/Record.hh"
34 #include "geners/Reference.hh"
35
36 using namespace gs;
37 using namespace std;
38
39 #ifdef CPP11_STD_AVAILABLE
40
41 int main(int, char**)
42 {
43 // The archive to use
44 StringArchive ar;
45
46 // Pretend that we need the folowing variables to perform a certain
47 // calculation
48 int i = 15;
49 double d = 25.0;
50 std::shared_ptr<unsigned> pu(new unsigned(10U));
51
52 // Instead of combining all these variables into a struct, we can
53 // make a tuple which effectively acts like such a struct for I/O
54 // purposes. This saves quite a bit of typing because there is no
55 // need to implement all those I/O-related methods (classId, write,
56 // restore, etc).
57 //
58 // There is, however, a better way to do the same thing: see file
59 // "optimized_tuple_io.cc".
60 //
61 auto t = make_tuple(&i, &d, pu);
62 ar << Record(t, "tuple1", "top");
63
64 // Now, change the values of the variables and save them again via
65 // the tuple. Naturally, it is expected that in a real calculation
66 // this will be done in a cycle (over spreadsheet rows, etc).
67 i = 123;
68 d = 33.0;
69 *pu = 7U;
70 ar << Record(t, "tuple1", "top");
71
72 // Make sure that we can read the data back from the archive.
73 // Note the difference between bare pointer and shared pointer.
74 // When the tuple is read back, the pointee object is modified
75 // for the bare pointer, while the shared pointer is made to
76 // point to a new object reconstructed on the heap.
77 int i2(0);
78 double d2(0.0);
79 std::shared_ptr<unsigned> pu2(new unsigned(789U));
80 auto t2 = make_tuple(&i2, &d2, pu2);
81 Reference<decltype(t2)> ref(ar, "tuple1", "top");
82 assert(ref.size() == 2);
83
84 ref.restore(0, &t2);
85 assert(i2 == 15);
86 assert(d2 == 25.0);
87 assert(*std::get<2>(t2) == 10U);
88
89 // Note that at this point *pu2 is still 789U -- it was not modified
90 assert(*pu2 == 789U);
91
92 ref.restore(1, &t2);
93 assert(i2 == 123);
94 assert(d2 == 33.0);
95 assert(*std::get<2>(t2) == 7U);
96
97 // While tuples that contain data are probably less convenient to use
98 // than tuples that contain pointer-like objects, it is of course
99 // possible to serialize them as well. Here, for example, how one can
100 // store a vector of tuples.
101 auto t3 = std::tuple<bool, int, double>();
102 std::vector<decltype(t3)> vec;
103 for (unsigned count=0; count<10; ++count)
104 {
105 std::get<0>(t3) = rand()*1.0/RAND_MAX < 0.5;
106 std::get<1>(t3) = static_cast<int>(rand()*100.0/RAND_MAX);
107 std::get<2>(t3) = rand()*1.0/RAND_MAX;
108 vec.push_back(t3);
109 }
110 ar << Record(vec, "tuplevec", "top");
111
112 // Retrieve the vector of tuples and compare it with the original
113 std::vector<decltype(t3)> vec2;
114 Reference<decltype(vec2)>(ar, "tuplevec", "top").restore(0, &vec2);
115 assert(vec2 == vec);
116
117 // We are done
118 return 0;
119 }
120
121 #else // CPP11_STD_AVAILABLE
122
123 int main(int, char**)
124 {
125 cout << "To run this example, please recompile "
126 << "the package with C++11 features enabled" << endl;
127 return 0;
128 }
129
130 #endif // CPP11_STD_AVAILABLE