| 1 |
// |
| 2 |
// Example simple stand-alone class with a default constructor |
| 3 |
// which plays nicely with "geners" serialization facilities. |
| 4 |
// |
| 5 |
// Static methods "classname" and "version" are used by "geners" |
| 6 |
// to construct the class identifier for serialization purposes. |
| 7 |
// Static method "restore" is used to read the object from an |
| 8 |
// istream. In addition, the class must implement member functions |
| 9 |
// "classId" and "write". "classId" is just a non-static wrapper |
| 10 |
// around "classname" and "version" which can always be implemented |
| 11 |
// as shown in this example. The purpose of the "write" method is, |
| 12 |
// naturally, to dump the object data into an ostream. In certain |
| 13 |
// situations, "classId" and "write" methods must be made virtual |
| 14 |
// in order to support polymorphic serialization. |
| 15 |
// |
| 16 |
// The "write" method must return "true" on success and "false" on |
| 17 |
// failure. The "restore" method must always succeed. If, for any |
| 18 |
// reason, "restore" is unable to reconstruct the object from |
| 19 |
// the istream, it must throw an exception which inherits from |
| 20 |
// std::exception. Several useful exception classes are declared |
| 21 |
// in the "geners/IOException.hh" header. |
| 22 |
// |
| 23 |
// The "restore" function overwrites an existing object which usually |
| 24 |
// lives on the stack. One can instead implement the "read" function |
| 25 |
// which makes a new object on the heap (see SimpleSerializable2.hh |
| 26 |
// header for such an example). As a rule of thumb, simple light-weight |
| 27 |
// classes without virtual methods and with default constructors |
| 28 |
// should implement "restore" for deserialization. In all other cases |
| 29 |
// implementing "read" is preferable. In particular, all classes which |
| 30 |
// are members of inheritance hierarchies and which can be accessed via |
| 31 |
// base class pointers must use virtual "classId" and "write" methods |
| 32 |
// and must implement "read" function for deserialization. An example |
| 33 |
// program called "inheritance_example.cc" gives more details about |
| 34 |
// serializing inheritance hierarchies. |
| 35 |
// |
| 36 |
|
| 37 |
#ifndef GSEXAMPLES_SIMPLESERIALIZABLE1_HH_ |
| 38 |
#define GSEXAMPLES_SIMPLESERIALIZABLE1_HH_ |
| 39 |
|
| 40 |
// The "geners/ClassId.hh" header contains the gs::ClassId declaration |
| 41 |
// (the class identifier). This header should be included whenever you |
| 42 |
// develop classes which use "geners" for serialization. |
| 43 |
// |
| 44 |
#include "geners/ClassId.hh" |
| 45 |
|
| 46 |
class SimpleSerializable1 |
| 47 |
{ |
| 48 |
public: |
| 49 |
inline SimpleSerializable1() : datum_(0) {} |
| 50 |
inline SimpleSerializable1(const int value) : datum_(value) {} |
| 51 |
|
| 52 |
inline int datum() const {return datum_;} |
| 53 |
|
| 54 |
// Operators which compare objects for equality are very useful |
| 55 |
// for I/O testing |
| 56 |
inline bool operator==(const SimpleSerializable1& r) const |
| 57 |
{return datum_ == r.datum_;} |
| 58 |
inline bool operator!=(const SimpleSerializable1& r) const |
| 59 |
{return !(*this == r);} |
| 60 |
|
| 61 |
// I/O methods needed for writing |
| 62 |
inline gs::ClassId classId() const {return gs::ClassId(*this);} |
| 63 |
bool write(std::ostream& of) const; |
| 64 |
|
| 65 |
// I/O methods needed for reading |
| 66 |
static inline const char* classname() {return "SimpleSerializable1";} |
| 67 |
static inline unsigned version() {return 1;} |
| 68 |
static void restore(const gs::ClassId& id, std::istream& in, |
| 69 |
SimpleSerializable1* ptr); |
| 70 |
private: |
| 71 |
// If necessary, the copy constructor and the assignment operator |
| 72 |
// can be disabled. Together with the default constructor, "write" |
| 73 |
// and "restore" methods give us everything we need for I/O. |
| 74 |
SimpleSerializable1(const SimpleSerializable1&); |
| 75 |
SimpleSerializable1& operator=(const SimpleSerializable1&); |
| 76 |
|
| 77 |
int datum_; |
| 78 |
}; |
| 79 |
|
| 80 |
#endif // GSEXAMPLES_SIMPLESERIALIZABLE1_HH_ |