Debian Codegraph @

examples/ExtDerivedBIO.cc

raw
1 // This example works only if C++11 is supported
2 #include "geners/CPP11_config.hh"
3 #ifdef CPP11_STD_AVAILABLE
4
5 #include "ExtDerivedBIO.hh"
6 #include "geners/IOException.hh"
7
8 bool ExtDerivedBIO::write(std::ostream& of, const wrapped_base& base,
9 const bool dumpId) const
10 {
11 // If necessary, write out the class id
12 const bool status = dumpId ? wrappedClassId().write(of) : true;
13
14 // Write the object data out
15 if (status)
16 {
17 const wrapped_type& w = dynamic_cast<const wrapped_type&>(base);
18 gs::write_pod(of, w.getValue());
19 gs::write_pod(of, w.getAnother());
20 }
21
22 // Return "true" on success
23 return status && !of.fail();
24 }
25
26 ExtDerivedB* ExtDerivedBIO::read(const gs::ClassId& id, std::istream& in) const
27 {
28 // Validate the class id. You might want to implement
29 // class versioning here.
30 wrappedClassId().ensureSameId(id);
31
32 // Read in the object data
33 int i;
34 gs::read_pod(in, &i);
35 double d;
36 gs::read_pod(in, &d);
37
38 // Check that the stream is in a valid state
39 if (in.fail()) throw gs::IOReadFailure(
40 "In ExtDerivedBIO::read: input stream failure");
41
42 // Return new object
43 return new ExtDerivedB(i, d);
44 }
45
46 const gs::ClassId& ExtDerivedBIO::wrappedClassId()
47 {
48 static const gs::ClassId wrapId(gs::ClassId::makeId<wrapped_type>());
49 return wrapId;
50 }
51
52 #endif // CPP11_STD_AVAILABLE