Debian Codegraph @

tests/SerializableDerivedB.cc

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