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