Debian Codegraph @

examples/SimpleSerializable1.cc

raw
1 #include <cassert>
2 #include "SimpleSerializable1.hh"
3
4 // The following header defines functions used to read/write built-in
5 // types as well as arrays, vectors, and strings. Always use these functions
6 // to implement the I/O for your own classes instead of using std::ostream
7 // facilities directly. This will ensure interface consistency and will
8 // simplify future updates.
9 #include "geners/binaryIO.hh"
10
11 // The following header defines several useful exception classes
12 #include "geners/IOException.hh"
13
14 using namespace gs;
15
16 bool SimpleSerializable1::write(std::ostream& of) const
17 {
18 write_pod(of, datum_);
19 return !of.fail();
20 }
21
22 void SimpleSerializable1::restore(const ClassId& id, std::istream& in,
23 SimpleSerializable1* ptr)
24 {
25 // Here, we ensure that a correct class id argument was provided.
26 // While a wrong class id argument should never be generated if
27 // the item is simply read back from a "geners" archive, this check
28 // might help to detect problems with other serialization scenarios.
29 //
30 static const ClassId myClassId(ClassId::makeId<SimpleSerializable1>());
31 myClassId.ensureSameId(id);
32
33 // Read back the object info
34 int i;
35 read_pod(in, &i);
36
37 // Check the status of the stream
38 if (in.fail()) throw IOReadFailure("In SimpleSerializable1::restore: "
39 "input stream failure");
40
41 // Overwrite the argument object. Ideally, we should be
42 // doing this only when we know for sure that all necessary
43 // information has been successfully read back.
44 assert(ptr);
45 ptr->datum_ = i;
46 }