OpenDaVINCI  4.16.0
ProtoSerializer.h
Go to the documentation of this file.
1 
20 #ifndef OPENDAVINCI_CORE_SERIALIZATION_PROTOSERIALIZER_H_
21 #define OPENDAVINCI_CORE_SERIALIZATION_PROTOSERIALIZER_H_
22 
23 #include <sstream>
24 #include <string>
25 
28 
29 namespace odcore {
30  namespace serialization {
31 
32 class Serializable;
33 
34  using namespace std;
35 
40  class ProtoSerializer : public Serializer {
41  public:
42  // Protobuf type specification.
43  enum PROTOBUF_TYPE { VARINT = 0,
44  EIGHT_BYTES = 1,
45  LENGTH_DELIMITED = 2,
46  UNUSED_3 = 3, // START_GROUP_DEPRECATED
47  UNUSED_4 = 4, // END_GROUP_DEPRECATED
48  FOUR_BYTES = 5,
49  UNUSED_6 = 6,
50  UNUSED_7 = 7 };
51 
52  private:
59 
65  ProtoSerializer& operator=(const ProtoSerializer &);
66 
67  public:
69 
75  ProtoSerializer(ostream &o);
76 
77  virtual ~ProtoSerializer();
78 
79  virtual void getSerializedData(ostream &o);
80 
81  private:
89  uint8_t encodeVarInt(ostream &out, uint64_t value);
90 
97  uint8_t encodeZigZag8(int8_t value);
98 
105  uint16_t encodeZigZag16(int16_t value);
106 
113  uint32_t encodeZigZag32(int32_t value);
114 
121  uint64_t encodeZigZag64(int64_t value);
122 
123  public:
124  virtual uint32_t writeValue(ostream &o, const Serializable &v);
125  virtual uint32_t writeValue(ostream &o, const bool &v);
126  virtual uint32_t writeValue(ostream &o, const char &v);
127  virtual uint32_t writeValue(ostream &o, const unsigned char &v);
128  virtual uint32_t writeValue(ostream &o, const int8_t &v);
129  virtual uint32_t writeValue(ostream &o, const int16_t &v);
130  virtual uint32_t writeValue(ostream &o, const uint16_t &v);
131  virtual uint32_t writeValue(ostream &o, const int32_t &v);
132  virtual uint32_t writeValue(ostream &o, const uint32_t &v);
133  virtual uint32_t writeValue(ostream &o, const int64_t &v);
134  virtual uint32_t writeValue(ostream &o, const uint64_t &v);
135  virtual uint32_t writeValue(ostream &o, const float &v);
136  virtual uint32_t writeValue(ostream &o, const double &v);
137  virtual uint32_t writeValue(ostream &o, const string &v);
138  virtual uint32_t writeValue(ostream &o, const void *data, const uint32_t &size);
139 
140  public:
141  virtual void write(const uint32_t &id, const Serializable &s);
142  virtual void write(const uint32_t &id, const bool &b);
143  virtual void write(const uint32_t &id, const char &c);
144  virtual void write(const uint32_t &id, const unsigned char &uc);
145  virtual void write(const uint32_t &id, const int8_t &i);
146  virtual void write(const uint32_t &id, const int16_t &i);
147  virtual void write(const uint32_t &id, const uint16_t &ui);
148  virtual void write(const uint32_t &id, const int32_t &i);
149  virtual void write(const uint32_t &id, const uint32_t &ui);
150  virtual void write(const uint32_t &id, const int64_t &i);
151  virtual void write(const uint32_t &id, const uint64_t &ui);
152  virtual void write(const uint32_t &id, const float &f);
153  virtual void write(const uint32_t &id, const double &d);
154  virtual void write(const uint32_t &id, const string &s);
155  virtual void write(const uint32_t &id, const void *data, const uint32_t &size);
156 
157  public:
158  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const Serializable &s);
159  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const bool &b);
160  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const char &c);
161  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const unsigned char &uc);
162  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const int8_t &i);
163  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const int16_t &i);
164  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const uint16_t &ui);
165  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const int32_t &i);
166  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const uint32_t &ui);
167  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const int64_t &i);
168  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const uint64_t &ui);
169  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const float &f);
170  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const double &d);
171  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const string &s);
172  virtual void write(const uint32_t &id, const string &longName, const string &shortName, const void *data, const uint32_t &size);
173 
174  private:
182  template<typename T>
183  uint64_t writeKeyValue(const uint32_t &id, T &v) {
184  uint32_t bytesWritten = 0;
185  uint64_t key = getKey(id, ProtoSerializer::VARINT);
186  bytesWritten += encodeVarInt(m_buffer, key);
187  bytesWritten += writeValue(m_buffer, v);
188  return bytesWritten;
189  }
190 
198  uint64_t getKey(const uint32_t &fieldNumber, const uint8_t &protoType);
199 
200  private:
201  ostream *m_out; // We have a pointer here that we derive from a reference parameter in our non-standard constructor; thus, the other class is responsible for the lifecycle of the variable to which we point to.
202  stringstream m_buffer;
203  };
204 
205  }
206 } // odcore::serialization
207 
208 #endif /*OPENDAVINCI_CORE_SERIALIZATION_PROTOSERIALIZER_H_*/
Definition: ProtoSerializer.h:40
Definition: Serializer.h:38
PROTOBUF_TYPE
Definition: ProtoSerializer.h:43
Definition: ProtoSerializer.h:43
Definition: Serializable.h:59