| 1 |
// |
| 2 |
// Example simple template with a default constructor. |
| 3 |
// |
| 4 |
// The type upon which this template is parameterized |
| 5 |
// must itself have the default constructor, the copy |
| 6 |
// constructor, and the operator which compares objects |
| 7 |
// for equality. This type can not be a pointer type |
| 8 |
// (if T is a pointer, the code will not compile). |
| 9 |
// |
| 10 |
|
| 11 |
#ifndef GSEXAMPLES_SERIALIZABLETEMPLATE1_HH_ |
| 12 |
#define GSEXAMPLES_SERIALIZABLETEMPLATE1_HH_ |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
#include "geners/ClassId.hh" |
| 16 |
|
| 17 |
// In order to read/write arbitrary types, you will need to include |
| 18 |
// the following header file |
| 19 |
#include "geners/GenericIO.hh" |
| 20 |
|
| 21 |
template<typename T> |
| 22 |
class SerializableTemplate1 |
| 23 |
{ |
| 24 |
public: |
| 25 |
inline SerializableTemplate1() : datum_() {} |
| 26 |
inline SerializableTemplate1(const T& value) : datum_(value) {} |
| 27 |
|
| 28 |
inline const T& datum() const {return datum_;} |
| 29 |
|
| 30 |
// Operators which compare objects for equality are very useful |
| 31 |
// for I/O testing |
| 32 |
inline bool operator==(const SerializableTemplate1& r) const |
| 33 |
{return datum_ == r.datum_;} |
| 34 |
inline bool operator!=(const SerializableTemplate1& r) const |
| 35 |
{return !(*this == r);} |
| 36 |
|
| 37 |
// I/O methods needed for writing |
| 38 |
inline gs::ClassId classId() const {return gs::ClassId(*this);} |
| 39 |
inline bool write(std::ostream& of) const |
| 40 |
{ |
| 41 |
// The following function should be used to write out single |
| 42 |
// objects in a generic manner |
| 43 |
return gs::write_item(of, datum_); |
| 44 |
} |
| 45 |
|
| 46 |
// Generating class names for templates is a bit more involved |
| 47 |
// than for simple classes. Here we use the "template_class_name" |
| 48 |
// template function declared in the "geners/ClassId.hh" header. |
| 49 |
// The function "template_class_name" can take up to six template |
| 50 |
// parameters. |
| 51 |
static inline const char* classname() |
| 52 |
{ |
| 53 |
static const std::string myClassName( |
| 54 |
gs::template_class_name<T>("SerializableTemplate1")); |
| 55 |
return myClassName.c_str(); |
| 56 |
} |
| 57 |
static inline unsigned version() {return 1;} |
| 58 |
static inline void restore(const gs::ClassId& id, std::istream& in, |
| 59 |
SerializableTemplate1* ptr) |
| 60 |
{ |
| 61 |
// Here, we ensure that a correct class id argument was provided. |
| 62 |
// While a wrong class id argument should never be generated if |
| 63 |
// the item is simply read back from a "geners" archive, this check |
| 64 |
// might help to detect problems with other serialization scenarios. |
| 65 |
// |
| 66 |
static const gs::ClassId myClassId( |
| 67 |
gs::ClassId::makeId<SerializableTemplate1<T> >()); |
| 68 |
myClassId.ensureSameId(id); |
| 69 |
|
| 70 |
// Make sure that we have an object to work with |
| 71 |
assert(ptr); |
| 72 |
|
| 73 |
// The following function should be used to read back single |
| 74 |
// objects in a generic manner. It takes the input stream as |
| 75 |
// the first argument and the pointer to an object that will |
| 76 |
// be overwritten as the second argument. |
| 77 |
// |
| 78 |
gs::restore_item(in, &ptr->datum_); |
| 79 |
} |
| 80 |
|
| 81 |
private: |
| 82 |
T datum_; |
| 83 |
}; |
| 84 |
|
| 85 |
#endif // GSEXAMPLES_SERIALIZABLETEMPLATE1_HH_ |