PVData C++  8.0.2
serialize.h
1 /* serialize.h */
2 /*
3  * Copyright information and license terms for this software can be
4  * found in the file LICENSE that is included with the distribution
5  */
6 /**
7  * @author mrk
8  */
9 #ifndef SERIALIZE_H
10 #define SERIALIZE_H
11 
12 #include <epicsTypes.h>
13 
14 #include <pv/byteBuffer.h>
15 #include <pv/sharedPtr.h>
16 
17 #include <shareLib.h>
18 
19 namespace epics { namespace pvData {
20 
21  class SerializableControl;
22  class DeserializableControl;
23  class Serializable;
24  class BitSetSerializable;
25  class SerializableArray;
26  class BitSet;
27  class Field;
28 
29  /**
30  * @brief Callback class for serialization.
31  *
32  * This must be provided by code that calls serialize.
33  */
34  class epicsShareClass SerializableControl {
35  public:
36  /**
37  * Destructor.
38  */
39  virtual ~SerializableControl(){}
40  /**
41  * Done with this buffer. Flush it.
42  */
43  virtual void flushSerializeBuffer() =0;
44  /**
45  * Make sure buffer has at least size bytes remaining.
46  * If not flush existing buffer and provide a new one.
47  * @param size The number of bytes.
48  */
49  virtual void ensureBuffer(std::size_t size) =0;
50  /**
51  * Add pad bytes to buffer.
52  * @param alignment alignment required.
53  */
54  virtual void alignBuffer(std::size_t alignment) =0;
55  /**
56  * Method for serializing primitive array data.
57  * Hook for supplying custom serialization implementation.
58  * The serialization implementation need not be provided.
59  * Returns true if method performs serialization, false otherwise.
60  * This should only be used for arrays of primitive types,
61  * i. e. boolean, byte,..., double.
62  * It cannot be called for string, structure, or union arrays.
63  * @param existingBuffer the existing buffer from the caller.
64  * @param toSerialize location of data to be put into buffer.
65  * @param elementCount number of elements.
66  * @param elementSize element size.
67  * @returns true if serialization performed, else false.
68  */
69  virtual bool directSerialize(
71  const char* toSerialize,
73  std::size_t elementSize) = 0;
74  /**
75  * serialize via cache
76  * @param field instance to be serialized
77  * @param buffer buffer to be serialized to
78  */
79  virtual void cachedSerialize(
80  std::tr1::shared_ptr<const Field> const & field,
81  ByteBuffer* buffer) = 0;
82  };
83 
84  /**
85  * @brief Callback class for deserialization.
86  *
87  * This must be provided by code that calls deserialize.
88  */
89  class epicsShareClass DeserializableControl {
90  public:
91  /**
92  * Destructor.
93  */
94  virtual ~DeserializableControl(){}
95  /**
96  * Helper method.
97  * Ensures specified size of bytes, provides it if necessary.
98  * @param size The number of bytes.
99  */
100  virtual void ensureData(std::size_t size) =0;
101  /**
102  * Align buffer.
103  * Note that this takes care only current buffer alignment.
104  * If streaming protocol is used,
105  * care must be taken that entire stream is aligned.
106  * @param alignment size in bytes, must be power of two.
107  */
108  virtual void alignData(std::size_t alignment) =0;
109  /**
110  * Method for deserializing array data.
111  * Hook for supplying custom deserialization implementation.
112  * The deserialization implementation need not be provided.
113  * Returns true if method performs deserialization, false otherwise.
114  * This should only be used for arrays of primitive types.
115  * i.e. boolean, byte,..., double.
116  * It cannot be called for string, structure, or union arrays.
117  * @param existingBuffer the existing buffer from the caller.
118  * @param deserializeTo location of data.
119  * @param elementCount number of elements.
120  * @param elementSize element size.
121  * @returns true if deserialization performed, else false.
122  */
123  virtual bool directDeserialize(
125  char* deserializeTo,
127  std::size_t elementSize) = 0;
128  /**
129  * deserialize via cache
130  * @param buffer buffer to be deserialized from
131  */
132  virtual std::tr1::shared_ptr<const Field> cachedDeserialize(
133  ByteBuffer* buffer) = 0;
134  };
135 
136  /**
137  * @brief Base class for serialization.
138  *
139  */
140  class epicsShareClass Serializable {
141  public:
142  /**
143  * Destructor.
144  */
145  virtual ~Serializable(){}
146  /**
147  * Serialize field into given buffer.
148  * @param buffer serialization buffer.
149  * @param flusher flush interface.
150  */
151  virtual void serialize(ByteBuffer *buffer,
152  SerializableControl *flusher) const = 0;
153  /**
154  * Deserialize buffer.
155  * @param buffer serialization buffer.
156  * @param flusher deserialization control.
157  */
158  virtual void deserialize(ByteBuffer *buffer,
160  };
161 
162  /**
163  * @brief Push serialize and append to the provided byte vector.
164  * No caching is done. Only complete serialization.
165  *
166  * @param S A Serializable object
167  * @param byteOrder Byte order to write (EPICS_ENDIAN_LITTLE or EPICS_ENDIAN_BIG)
168  * @param out The output vector. Results are appended
169  */
170  void epicsShareFunc serializeToVector(const Serializable *S,
171  int byteOrder,
172  std::vector<epicsUInt8>& out);
173 
174  /**
175  * @brief deserializeFromBuffer Deserialize into S from provided vector
176  * @param S A Serializeable object. The current contents will be replaced
177  * @param in The input buffer (byte order of this buffer is used)
178  * @throws std::logic_error if input buffer is too small. State of S is then undefined.
179  */
180  void epicsShareFunc deserializeFromBuffer(Serializable *S,
181  ByteBuffer& in);
182 
183  /**
184  * @brief deserializeFromBuffer Deserialize into S from provided vector
185  * @param S A Serializeable object. The current contents will be replaced
186  * @param byteOrder Byte order to write (EPICS_ENDIAN_LITTLE or EPICS_ENDIAN_BIG)
187  * @param in The input vector
188  * @throws std::logic_error if input buffer is too small. State of S is then undefined.
189  */
190  inline void deserializeFromVector(Serializable *S,
191  int byteOrder,
192  const std::vector<epicsUInt8>& in)
193  {
194  ByteBuffer B((char*)&in[0], in.size(), byteOrder); // we promise not the modify 'in'
195  deserializeFromBuffer(S, B);
196  }
197 
198  /**
199  * @brief Class for serializing bitSets.
200  *
201  */
202  class epicsShareClass BitSetSerializable {
203  public:
204  /**
205  * Destructor.
206  */
207  virtual ~BitSetSerializable(){}
208  /**
209  * Serialize field into given buffer.
210  * @param buffer serialization buffer.
211  * @param flusher flush interface.
212  * @param bitSet The bitSet to serialize.
213  */
214  virtual void serialize(ByteBuffer *buffer,
216  /**
217  * Deserialize buffer.
218  * @param buffer serialization buffer.
219  * @param flusher deserialization control.
220  * @param bitSet The bitSet to deserialize.
221  */
222  virtual void deserialize(ByteBuffer *buffer,
224  };
225 
226 
227  /**
228  * @brief Class for serializing arrays.
229  *
230  */
231  class epicsShareClass SerializableArray : public virtual Serializable {
232  public:
233  /**
234  * Destructor.
235  */
236  virtual ~SerializableArray(){}
237  using Serializable::serialize;
238  /**
239  * Serialize field into given buffer.
240  * @param buffer serialization buffer.
241  * @param flusher flush interface.
242  * @param offset offset in elements.
243  * @param count number of elements
244  */
245  virtual void serialize(
248  std::size_t offset,
249  std::size_t count) const = 0;
250  };
251 
252 }}
253 #endif /* SERIALIZE_H */
Callback class for serialization.
Definition: serialize.h:34
void deserializeFromVector(Serializable *S, int byteOrder, const std::vector< epicsUInt8 > &in)
deserializeFromBuffer Deserialize into S from provided vector
Definition: serialize.h:190
virtual void serialize(ByteBuffer *buffer, SerializableControl *flusher, std::size_t offset, std::size_t count) const =0
Callback class for deserialization.
Definition: serialize.h:89
Class for serializing arrays.
Definition: serialize.h:231
Class for serializing bitSets.
Definition: serialize.h:202
Base class for serialization.
Definition: serialize.h:140