Debian Codegraph @

examples/ExtDerivedAIO.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 "ExtDerivedAIO.hh"
6 #include "geners/IOException.hh"
7
8 bool ExtDerivedAIO::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 }
20
21 // Return "true" on success
22 return status && !of.fail();
23 }
24
25 ExtDerivedA* ExtDerivedAIO::read(const gs::ClassId& id, std::istream& in) const
26 {
27 // Validate the class id. You might want to implement
28 // class versioning here.
29 wrappedClassId().ensureSameId(id);
30
31 // Read in the object data
32 int i;
33 gs::read_pod(in, &i);
34
35 // Check that the stream is in a valid state
36 if (in.fail()) throw gs::IOReadFailure(
37 "In ExtDerivedAIO::read: input stream failure");
38
39 // Return new object
40 return new ExtDerivedA(i);
41 }
42
43 const gs::ClassId& ExtDerivedAIO::wrappedClassId()
44 {
45 static const gs::ClassId wrapId(gs::ClassId::makeId<wrapped_type>());
46 return wrapId;
47 }
48
49 #endif // CPP11_STD_AVAILABLE