Debian Codegraph @

tests/SerializableBase.hh

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