CFEL - ASG Software Suite  2.5.0
CASS
serializable.hpp
Go to the documentation of this file.
1 //Copyright (C) 2011, 2015 Lutz Foucar
2 
3 /**
4  * @file cass/serializable.hpp file contains base class all serializable classes
5  *
6  * @author Lutz Foucar
7  */
8 
9 #ifndef _SERIALIZABLE_HPP_
10 #define _SERIALIZABLE_HPP_
11 
12 #include <stdint.h>
13 #include <sstream>
14 
15 #include "serializer.hpp"
16 
17 namespace cass
18 {
19 //forward declaration
20 class SerializerBackend;
21 
22 /** Serializable.
23  *
24  * pure virtual class that all serializable classes should inherit from.
25  * This makes sure that all classes that should be serializable
26  *
27  * @author Lutz Foucar
28  */
30 {
31 public:
32  /** constructor initializing the version*/
33  explicit Serializable(uint16_t version)
34  : _version(version)
35  {}
36 
37  /** virtual destructor to avoid warning with gcc 4.1.2 */
38  virtual ~Serializable(){}
39 
40  /** pure virtual function that needs to be defined by the derived class.
41  *
42  * will serialize an object to the Serializer class
43  */
44  virtual void serialize(SerializerBackend&)const=0;
45 
46  /** pure virtual function that needs to be defined by the derived class.
47  *
48  * will deserialize an object from the Serializer class
49  */
50  virtual bool deserialize(SerializerBackend&)=0;
51 
52  /** retrieve the version of the serializer */
53  uint16_t ver()const {return _version;}
54 
55  /** write the version to the stream
56  *
57  * @param out the stream to write the version to
58  */
59  virtual void writeVersion(SerializerBackend &out)const
60  {
61  out.add(_version);
62  }
63 
64  /** check the version
65  *
66  * asserts that the version of this class corresponsed to the one read
67  * from the stream
68  *
69  * @param in the stream to read the version from
70  */
71  virtual void checkVersion(SerializerBackend& in)const
72  {
73  uint16_t version(in.retrieve<uint16_t>());
74  if(version != _version)
75  {
76  std::stringstream ss;
77  ss << "Version mismatch in '" << typeid(*this).name();
78  ss << "': '" << version << "' != '" << _version << "'";
79  throw std::runtime_error(ss.str());
80  }
81  }
82 
83 protected:
84  /** the version for de/serializing*/
85  uint16_t _version;
86 };
87 }
88 #endif
virtual void writeVersion(SerializerBackend &out) const
write the version to the stream
virtual bool deserialize(SerializerBackend &)=0
pure virtual function that needs to be defined by the derived class.
file contains classes for serializing objects
uint16_t _version
the version for de/serializing
Serializable(uint16_t version)
constructor initializing the version
virtual void serialize(SerializerBackend &) const =0
pure virtual function that needs to be defined by the derived class.
uint16_t ver() const
retrieve the version of the serializer
virtual void checkVersion(SerializerBackend &in) const
check the version
virtual ~Serializable()
virtual destructor to avoid warning with gcc 4.1.2
Type retrieve()
read arbitrary value from stream
Definition: serializer.hpp:169
Serializable.
void add(const Type &value)
add arbitrary value to the stream
Definition: serializer.hpp:158