Debian Codegraph @

examples/SerializableDerivedA.cc

raw
1 #include <cassert>
2
3 #include "SerializableDerivedA.hh"
4
5 #include "geners/binaryIO.hh"
6 #include "geners/CPP11_auto_ptr.hh"
7 #include "geners/IOException.hh"
8
9 bool SerializableDerivedA::write(std::ostream& of) const
10 {
11 gs::write_pod(of, a_);
12 return !of.fail();
13 }
14
15 SerializableDerivedA* SerializableDerivedA::read(const gs::ClassId& id,
16 std::istream& in)
17 {
18 // Class id for the current version of this class
19 static const gs::ClassId myId(gs::ClassId::makeId<SerializableDerivedA>());
20
21 if (id.name() == myId.name())
22 {
23 // We are reading an object of this particular class
24 myId.ensureSameVersion(id);
25
26 CPP11_auto_ptr<SerializableDerivedA> obj(new SerializableDerivedA(0));
27 gs::read_pod(in, &obj->a_);
28 if (in.fail())
29 throw gs::IOReadFailure("In SerializableDerivedA::read: "
30 "input stream failure");
31 return obj.release();
32 }
33 else
34 {
35 // We are reading an object of some other class (normally,
36 // it must be derived from this one). Do this via the read
37 // function of the top-level base class.
38 SerializableBase* base = SerializableBase::read(id, in);
39 SerializableDerivedA* obj = dynamic_cast<SerializableDerivedA*>(base);
40
41 // The above dynamic_cast must always succeed unless this function
42 // was called with a completely inappropriate class id. Normally,
43 // such a call should never happen and should be considered a bug.
44 // Hence the assert below.
45 assert(obj);
46 return obj;
47 }
48 }