Debian Codegraph @

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