| 1 |
// |
| 2 |
// The following program illustrates usage of the archive I/O |
| 3 |
// in the "geners" package with templates whose code can not be |
| 4 |
// modified to play nicely with this I/O facility (e.g., they |
| 5 |
// are members of external libraries owned by somebody else). |
| 6 |
// To make the I/O of such templates meaningful, interfaces to such |
| 7 |
// templates must expose sufficient amount of detail so that the |
| 8 |
// complete object state can be captured and later restored. |
| 9 |
// |
| 10 |
// The template which will be serialized is called "ExternalTemplate". |
| 11 |
// The header which provides serialization facilities for this template |
| 12 |
// is called "ExternalTemplateIO.hh". This header must be included here |
| 13 |
// in order for us to work with such a template. |
| 14 |
// |
| 15 |
|
| 16 |
#include "ExternalTemplateIO.hh" |
| 17 |
|
| 18 |
// We will use the class "SimpleSerializable2" as one of the |
| 19 |
// template parameters. |
| 20 |
#include "SimpleSerializable2.hh" |
| 21 |
|
| 22 |
// Include Geners headers for archive I/O |
| 23 |
#include "geners/StringArchive.hh" |
| 24 |
#include "geners/Record.hh" |
| 25 |
#include "geners/Reference.hh" |
| 26 |
|
| 27 |
using namespace gs; |
| 28 |
|
| 29 |
|
| 30 |
// An example class not known to "geners" |
| 31 |
class Unknown |
| 32 |
{ |
| 33 |
public: |
| 34 |
inline Unknown() {} |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
int main(int, char**) |
| 39 |
{ |
| 40 |
// The types we we want to store in the archive |
| 41 |
typedef ExternalTemplate<int, SimpleSerializable2> MyTemplate; |
| 42 |
typedef ExternalTemplate2<Unknown> MyTemplate2; |
| 43 |
|
| 44 |
// The archive to use |
| 45 |
StringArchive ar; |
| 46 |
|
| 47 |
// The objects which we want to store in the archive |
| 48 |
int i = 10; |
| 49 |
SimpleSerializable2 s2(3.0); |
| 50 |
MyTemplate object(i, s2); |
| 51 |
MyTemplate2 object2(5); |
| 52 |
|
| 53 |
// Store the objects |
| 54 |
ar << Record(object, "Example template", "") |
| 55 |
<< Record(object2, "Template with one template argument", ""); |
| 56 |
|
| 57 |
// Get the reference to this object in the archive |
| 58 |
Reference<MyTemplate> ref(ar, "Example template", ""); |
| 59 |
assert(ref.unique()); |
| 60 |
|
| 61 |
// Read the object back and check that the retrived object is |
| 62 |
// identical to the original one. First, restore it on the stack... |
| 63 |
MyTemplate readback(0, s2); |
| 64 |
assert(readback != object); |
| 65 |
ref.restore(0, &readback); |
| 66 |
assert(readback == object); |
| 67 |
|
| 68 |
// and now fetch it on the heap |
| 69 |
CPP11_auto_ptr<MyTemplate> pread = ref.get(0); |
| 70 |
assert(*pread == object); |
| 71 |
|
| 72 |
// Now, get "MyTemplate2" object |
| 73 |
Reference<MyTemplate2> ref2(ar, "Template with one template argument", ""); |
| 74 |
assert(ref2.unique()); |
| 75 |
MyTemplate2 readback2(3); |
| 76 |
assert(readback2 != object2); |
| 77 |
ref2.restore(0, &readback2); |
| 78 |
assert(readback2 == object2); |
| 79 |
|
| 80 |
// We are done |
| 81 |
return 0; |
| 82 |
} |