Debian Codegraph @

tests/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 {
30 delete obj.release();
31 throw gs::IOReadFailure("In SerializableDerivedA::read: "
32 "inputs stream failure");
33 }
34 return obj.release();
35 }
36 else
37 {
38 // We are reading an object of some other class (normally,
39 // it must be derived from this one). Do this via the read
40 // function of the top-level base class.
41 SerializableBase* base = SerializableBase::read(id, in);
42 SerializableDerivedA* obj = dynamic_cast<SerializableDerivedA*>(base);
43
44 // The above dynamic_cast must always succeed unless this function
45 // was called with a completely inappropriate class id. Normally,
46 // such a call should never happen and should be considered a bug.
47 // Hence the assert below.
48 assert(obj);
49 return obj;
50 }
51 }