| 1 |
// |
| 2 |
// The following program illustrates the usage of "geners" package |
| 3 |
// serialization mechanisms with an inheritance hierarchy. The following |
| 4 |
// classes are used: SerializableBase, SerializableDerivedA (derived from |
| 5 |
// SerializableBase), SerializableDerivedB (derived from SerializableBase), |
| 6 |
// SerializableDerivedC (derived from SerializableDerivedB). |
| 7 |
// |
| 8 |
// SerializableBase (files SerializableBase.hh, SerializableBase.cc) |
| 9 |
// is a top-level base class for a serializable hierarchy. Its "read" |
| 10 |
// method (implemented inside SerializableBase.cc) is capable of reading |
| 11 |
// all classes derived from SerializableBase. This is what happens inside |
| 12 |
// that method: |
| 13 |
// |
| 14 |
// 1. An instance of a reader factory is created (or recalled) as a Meyers |
| 15 |
// singleton. This means that a reader factory should be implemented |
| 16 |
// as a companion to the top-level base class. See files |
| 17 |
// SerializableBaseReader.hh, SerializableBaseReader.cc. |
| 18 |
// |
| 19 |
// 2. The factory "read" method chooses the "read" function of a concrete |
| 20 |
// derived class using the class identifier. |
| 21 |
// |
| 22 |
// 3. The proper "read" function is called, object info is read from the |
| 23 |
// input stream and the object is created on the heap. |
| 24 |
// |
| 25 |
// The reader factory for SerializableBase descendants is defined inside |
| 26 |
// the "SerializableBaseReader.hh" file. Naturally, the classes read by |
| 27 |
// a particular factory must be registered with that factory. This |
| 28 |
// registration can be done in two different ways: |
| 29 |
// |
| 30 |
// a) In the factory constructor, as in the file SerializableBaseReader.cc. |
| 31 |
// It is convenient to do so for all derived classes in a hierarchy |
| 32 |
// distributed with some library, etc. |
| 33 |
// |
| 34 |
// b) Anywhere else in the code but before any I/O related to that |
| 35 |
// hierarchy is performed. See the line of code in this file which |
| 36 |
// looks like |
| 37 |
// |
| 38 |
// StaticSerializableBaseReader::registerClass<SerializableDerivedC>(); |
| 39 |
// |
| 40 |
// This must be done, for example, if an application implements new |
| 41 |
// classes which inherit from a base class defined in an external |
| 42 |
// library, and the constructor code for the associated reader factory |
| 43 |
// can not be modified. |
| 44 |
// |
| 45 |
#include "geners/vectorIO.hh" |
| 46 |
#include "geners/IOException.hh" |
| 47 |
#include "geners/StringArchive.hh" |
| 48 |
#include "geners/Record.hh" |
| 49 |
#include "geners/Reference.hh" |
| 50 |
|
| 51 |
#include "SerializableBaseReader.hh" |
| 52 |
#include "SerializableDerivedA.hh" |
| 53 |
#include "SerializableDerivedB.hh" |
| 54 |
|
| 55 |
// Create another class in the SerializableBase inheritance hierarchy |
| 56 |
// |
| 57 |
class SerializableDerivedC : public SerializableDerivedB |
| 58 |
{ |
| 59 |
public: |
| 60 |
inline SerializableDerivedC(const unsigned u, const double d) |
| 61 |
: SerializableDerivedB(d), u_(u) |
| 62 |
{ |
| 63 |
std::cout << "SerializableDerivedC constructor" << std::endl; |
| 64 |
} |
| 65 |
|
| 66 |
inline ~SerializableDerivedC() |
| 67 |
{ |
| 68 |
std::cout << "SerializableDerivedC destructor" << std::endl; |
| 69 |
} |
| 70 |
|
| 71 |
inline unsigned u() const {return u_;} |
| 72 |
|
| 73 |
inline gs::ClassId classId() const {return gs::ClassId(*this);} |
| 74 |
inline bool write(std::ostream& of) const |
| 75 |
{ |
| 76 |
// If writing out the complete object information at this point |
| 77 |
// would result in too much code duplication between the code |
| 78 |
// used here and the "write" method of the base class, it is |
| 79 |
// instead often possible to invoke |
| 80 |
// |
| 81 |
// SerializableDerivedB::classId().write(of); |
| 82 |
// SerializableDerivedB::write(of); |
| 83 |
// |
| 84 |
// This, of course, assumes that the "write" function of the base |
| 85 |
// is not abstract. However, this technique has several drawbacks. |
| 86 |
// The main drawback is that the implementation of the base class |
| 87 |
// "write" method may change with time. This means that we are |
| 88 |
// no longer in control of the exact format, so we must call the |
| 89 |
// "read" function of the base class in the "read" method of this |
| 90 |
// class. This leads to an extra copy of the base class contents |
| 91 |
// which must be later deleted. Another drawback is that the |
| 92 |
// class id of the base class must be written out for each object |
| 93 |
// of this class if proper evolution of the version numbers is |
| 94 |
// to be supported. |
| 95 |
// |
| 96 |
gs::write_pod(of, b()); |
| 97 |
gs::write_pod(of, u_); |
| 98 |
return !of.fail(); |
| 99 |
} |
| 100 |
|
| 101 |
static inline const char* classname() {return "SerializableDerivedC";} |
| 102 |
static inline unsigned version() {return 1;} |
| 103 |
static inline SerializableDerivedC* read(const gs::ClassId& id, |
| 104 |
std::istream& in) |
| 105 |
{ |
| 106 |
// We do not expect any other class to be derived from |
| 107 |
// this one (note that the destructor is not virtual). |
| 108 |
// Because of this, we only need to handle the case |
| 109 |
// of correct class id. |
| 110 |
static const gs::ClassId myId(gs::ClassId::makeId<SerializableDerivedC>()); |
| 111 |
myId.ensureSameId(id); |
| 112 |
|
| 113 |
double b(0.0); |
| 114 |
gs::read_pod(in, &b); |
| 115 |
unsigned u; |
| 116 |
gs::read_pod(in, &u); |
| 117 |
if (in.fail()) |
| 118 |
throw gs::IOReadFailure("In SerializableDerivedC::read: " |
| 119 |
"input stream failure"); |
| 120 |
return new SerializableDerivedC(u, b); |
| 121 |
} |
| 122 |
|
| 123 |
protected: |
| 124 |
virtual bool isEqual(const SerializableBase& otherBase) const |
| 125 |
{ |
| 126 |
// Note the call of "isEqual" method of the base class. |
| 127 |
// This should be done if that method is not pure virtual. |
| 128 |
const SerializableDerivedC& r = |
| 129 |
static_cast<const SerializableDerivedC&>(otherBase); |
| 130 |
return SerializableDerivedB::isEqual(otherBase) && u_ == r.u_; |
| 131 |
} |
| 132 |
|
| 133 |
private: |
| 134 |
unsigned u_; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
int main(int, char**) |
| 139 |
{ |
| 140 |
typedef CPP11_shared_ptr<SerializableBase> BasePtr; |
| 141 |
typedef CPP11_shared_ptr<SerializableDerivedB> DerivedBPtr; |
| 142 |
|
| 143 |
// Register the new class with the reader factory |
| 144 |
StaticSerializableBaseReader::registerClass<SerializableDerivedC>(); |
| 145 |
|
| 146 |
// Create a heterogeneous collection of objects using pointers |
| 147 |
// to the top-level base |
| 148 |
std::vector<BasePtr> coll1; |
| 149 |
coll1.push_back(BasePtr(new SerializableDerivedA(1))); |
| 150 |
coll1.push_back(BasePtr(new SerializableDerivedB(2.0))); |
| 151 |
coll1.push_back(BasePtr(new SerializableDerivedC(3U, 4.0))); |
| 152 |
|
| 153 |
// Write out this collection |
| 154 |
std::stringstream s; |
| 155 |
gs::write_item(s, coll1); |
| 156 |
|
| 157 |
// Read it back |
| 158 |
std::vector<BasePtr> coll2; |
| 159 |
gs::restore_item(s, &coll2); |
| 160 |
|
| 161 |
// Make sure the read back items are identical to the original ones |
| 162 |
{ |
| 163 |
const unsigned len = coll1.size(); |
| 164 |
assert(len == coll2.size()); |
| 165 |
for (unsigned i=0; i<len; ++i) |
| 166 |
assert(*coll1[i] == *coll2[i]); |
| 167 |
} |
| 168 |
|
| 169 |
// Create and write a heterogeneous collection of objects using |
| 170 |
// pointers to a mid-level base |
| 171 |
std::vector<DerivedBPtr> coll3; |
| 172 |
coll3.push_back(DerivedBPtr(new SerializableDerivedB(5.0))); |
| 173 |
coll3.push_back(DerivedBPtr(new SerializableDerivedC(6U, 7.0))); |
| 174 |
gs::write_item(s, coll3); |
| 175 |
|
| 176 |
// Read it back |
| 177 |
std::vector<DerivedBPtr> coll4; |
| 178 |
gs::restore_item(s, &coll4); |
| 179 |
|
| 180 |
// Make sure the read back items are identical to the original ones |
| 181 |
{ |
| 182 |
const unsigned len = coll3.size(); |
| 183 |
assert(len == coll4.size()); |
| 184 |
for (unsigned i=0; i<len; ++i) |
| 185 |
assert(*coll3[i] == *coll4[i]); |
| 186 |
} |
| 187 |
|
| 188 |
// Archive I/O example which illustrates the use of a base class |
| 189 |
// pointer/reference to store and retrieve the item |
| 190 |
gs::StringArchive ar; |
| 191 |
const SerializableDerivedB item(7.777777); |
| 192 |
|
| 193 |
// Write out the item using a reference to the base. Note explicit |
| 194 |
// dynamic cast to the base reference -- this reference must be used |
| 195 |
// if in the future we want to retrieve the item using a pointer to |
| 196 |
// the base class. This is because the class id stored in the archive |
| 197 |
// is always item's actual class id (this is what the "read" function |
| 198 |
// gets) while the class id in the catalog (which defines the item |
| 199 |
// type for all item search purposes) is the class id generated for |
| 200 |
// the reference used to store the item. |
| 201 |
// |
| 202 |
ar << gs::Record(dynamic_cast<const SerializableBase&>(item), "item", ""); |
| 203 |
|
| 204 |
// Get the item back from the archive using a pointer to the base |
| 205 |
CPP11_auto_ptr<SerializableBase> p1 = |
| 206 |
gs::Reference<SerializableBase>(ar, "item", "").get(0); |
| 207 |
|
| 208 |
// Make sure the retrieved item is the same as the original one |
| 209 |
assert(item == *p1); |
| 210 |
|
| 211 |
// We are done |
| 212 |
return 0; |
| 213 |
} |