Debian Codegraph @

examples/storing_stl_containers.cc

raw
1 //
2 // The following program illustrates "geners" archive I/O for STL
3 // containers. To serialize an STL container, include the corresponding
4 // "geners" header which ends in IO.hh. For example, to store lists,
5 // include "geners/listIO.hh", to store maps or multimaps include
6 // "geners/mapIO.hh", etc. Here is the full list of relevant standard
7 // headers together with the corresponding headers from the "geners"
8 // package
9 //
10 // Standard header Geners header Comment
11 // --------------- --------------- ----------------------------------
12 // <array> arrayIO.hh compiler must support C++11 or TR1
13 // <deque> dequeIO.hh
14 // <forward_list> forward_listIO.hh C++11 support required
15 // <list> listIO.hh
16 // <map> mapIO.hh
17 // <set> setIO.hh
18 // <tuple> tupleIO.hh C++11 support required
19 // <unordered_map> unordered_mapIO.hh C++11 support required
20 // <unordered_set> unordered_setIO.hh C++11 support required
21 // <valarray> valarrayIO.hh
22 // <vector> vectorIO.hh
23 //
24 // If a header requires C++11 support and your compiler does not provide
25 // it, you will not be able to use that header. This does not prevent you
26 // from using headers and containers that do not need C++11.
27 //
28 // Strings are an exception from this scheme -- their serialization
29 // facilities are defined in "geners/binaryIO.hh" and they are essentially
30 // treated as PODs.
31 //
32 #include <cassert>
33
34 // In this example, we will use vectors and maps
35 #include "geners/vectorIO.hh"
36 #include "geners/mapIO.hh"
37
38 #include "geners/StringArchive.hh"
39 #include "geners/Record.hh"
40 #include "geners/Reference.hh"
41
42 #include "SimpleSerializable2.hh"
43
44 using namespace gs;
45 using namespace std;
46
47 int main(int, char**)
48 {
49 // The archive to use
50 StringArchive ar;
51
52 // Make some objects that we want to store in the archive
53 vector<SimpleSerializable2> objects;
54 objects.push_back(SimpleSerializable2(10.));
55 objects.push_back(SimpleSerializable2(-12345.));
56 objects.push_back(SimpleSerializable2(42.));
57
58 map<string,int> roomNumbers;
59 roomNumbers["Andrew"] = 21;
60 roomNumbers["John"] = 33;
61
62 // And now store these objects
63 ar << Record(objects, "Some", "Objects")
64 << Record(roomNumbers, "Room Numbers", "My Department");
65
66 // Retrieve the objects and make sure that they are identical
67 // to the original ones. As usual, the restored objects can be
68 // placed either on the stack or on the heap.
69 vector<SimpleSerializable2> objects_2;
70 Reference<vector<SimpleSerializable2> >(ar, "Some", "Objects").restore(
71 0, &objects_2);
72 assert(objects == objects_2);
73
74 CPP11_auto_ptr<map<string,int> > roomNumbers_2 =
75 Reference<map<string,int> >(ar, "Room Numbers", "My Department").get(0);
76 assert(roomNumbers == *roomNumbers_2);
77
78 // We are done
79 return 0;
80 }