Debian Codegraph @

examples/SerializableBase.hh

raw
1 // Example base class for a serializable inheritance hierarchy
2
3 #ifndef GSEXAMPLES_SERIALIZABLEBASE_HH_
4 #define GSEXAMPLES_SERIALIZABLEBASE_HH_
5
6 #include <iostream>
7 #include <typeinfo>
8
9 #include "geners/ClassId.hh"
10
11 class SerializableBase
12 {
13 public:
14 inline SerializableBase()
15 : number_(nextObjectNumber())
16 {
17 std::cout << "SerializableBase constructor for object # "
18 << number_ << std::endl;
19 }
20
21 inline virtual ~SerializableBase()
22 {
23 std::cout << "SerializableBase destructor for object # "
24 << number_ << std::endl;
25 }
26
27 // Comparison operators. Note that they are not virtual and should
28 // not be overriden by derived classes. These operators are very
29 // useful for I/O testing.
30 inline bool operator==(const SerializableBase& r) const
31 {return (typeid(*this) == typeid(r)) && this->isEqual(r);}
32 inline bool operator!=(const SerializableBase& r) const
33 {return !(*this == r);}
34
35 // I/O methods needed for writing. Must be overriden by derived classes.
36 virtual gs::ClassId classId() const = 0;
37 virtual bool write(std::ostream& of) const = 0;
38
39 // I/O methods needed for reading
40 static inline const char* classname() {return "SerializableBase";}
41 static inline unsigned version() {return 1;}
42 static SerializableBase* read(const gs::ClassId& id, std::istream& in);
43
44 protected:
45 // Method needed to compare objects for equality.
46 // Must be overriden by derived classes.
47 virtual bool isEqual(const SerializableBase&) const = 0;
48
49 private:
50 static unsigned long nextObjectNumber();
51 unsigned long number_;
52 };
53
54 #endif // GSEXAMPLES_SERIALIZABLEBASE_HH_