Debian Codegraph @

examples/storing_arrays.cc

raw
1 //
2 // The following program illustrates "geners" archive I/O for arrays
3 //
4 #include <cassert>
5
6 #include "geners/StringArchive.hh"
7 #include "geners/ArrayRecord.hh"
8 #include "geners/ArrayReference.hh"
9
10 using namespace gs;
11
12 int main(int, char**)
13 {
14 // The archive to use
15 StringArchive ar;
16
17 // The array which we want to store in the archive
18 int array[] = {1, 2, 3, 4, 5};
19
20 // To store arrays, we need to use a special record: "ArrayRecord".
21 // Note that the array size must be known to the user both when
22 // the array is stored in the archive and when it is retrieved.
23 // The array size itself is not stored in the archive since it is
24 // normally just a compile-time constant. Store the array size
25 // in a separate record or, better, use std::vector to store and
26 // retrieve collections whose size is not known in advance.
27 const unsigned arrLen = sizeof(array)/sizeof(array[0]);
28 ar << ArrayRecord(array, arrLen, "My array", "Top");
29
30 // Retrieve the array from the archive. Use "ArrayReference" to find
31 // the array in the archive.
32 int array2[arrLen] = {0};
33 ArrayReference<int>(ar, "My array", "Top").restore(0, array2, arrLen);
34
35 // Make sure that the retrieved and the original arrays are identical
36 for (unsigned i=0; i<arrLen; ++i)
37 assert(array2[i] == array[i]);
38
39 // We are done
40 return 0;
41 }