F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Serializable.hpp
Go to the documentation of this file.
1 #ifndef SERIALIZABLE_HPP
2 #define SERIALIZABLE_HPP
3 
4 #ifdef BUILD_UT
5 #include <iostream>
6 #endif
7 
8 #include <FpConfig.hpp>
9 #include "Fw/Deprecate.hpp"
10 
11 namespace Fw {
12 
13 class StringBase;
14 typedef enum {
23 class SerializeBufferBase;
24 
25 class Serializable {
26  public:
27  // Size type for backwards compatibility
29 
30  public:
31  virtual SerializeStatus serialize(SerializeBufferBase& buffer) const = 0;
33 #if FW_SERIALIZABLE_TO_STRING || FW_ENABLE_TEXT_LOGGING || BUILD_UT
34  virtual void toString(StringBase& text) const;
35 #endif
36 
37 #ifdef BUILD_UT
38  friend std::ostream& operator<<(std::ostream& os, const Serializable& val);
39 #endif
40 
41  protected:
42  Serializable();
43  virtual ~Serializable();
44 };
45 
47  public:
48  enum t {
51  };
52 };
53 
55  protected:
57 
58  public:
59  virtual ~SerializeBufferBase();
60 
61  // Serialization for built-in types
62 
65 
66 #if FW_HAS_16_BIT == 1
67  SerializeStatus serialize(U16 val);
68  SerializeStatus serialize(I16 val);
69 #endif
70 #if FW_HAS_32_BIT == 1
71  SerializeStatus serialize(U32 val);
72  SerializeStatus serialize(I32 val);
73 #endif
74 #if FW_HAS_64_BIT == 1
76  SerializeStatus serialize(I64 val);
77 #endif
79 #if FW_HAS_F64
80  SerializeStatus serialize(F64 val);
81 #endif
82  SerializeStatus serialize(bool val);
83 
85  const void* val);
86 
88  SerializeStatus serialize(const U8* buff, NATIVE_UINT_TYPE length, bool noLength);
90  SerializeStatus serialize(const U8* buff, NATIVE_UINT_TYPE length);
91 
101  SerializeStatus serialize(const U8* buff, FwSizeType length, Serialization::t mode);
102 
104 
106 
108 
109  // Deserialization for built-in types
110 
113 
114 #if FW_HAS_16_BIT == 1
115  SerializeStatus deserialize(U16& val);
116  SerializeStatus deserialize(I16& val);
117 #endif
118 
119 #if FW_HAS_32_BIT == 1
120  SerializeStatus deserialize(U32& val);
121  SerializeStatus deserialize(I32& val);
122 #endif
123 #if FW_HAS_64_BIT == 1
125  SerializeStatus deserialize(I64& val);
126 #endif
128 #if FW_HAS_F64
129  SerializeStatus deserialize(F64& val);
130 #endif
131  SerializeStatus deserialize(bool& val);
132 
133  SerializeStatus deserialize(void*& val);
134 
136  SerializeStatus deserialize(U8* buff, NATIVE_UINT_TYPE& length, bool noLength);
137 
152 
154 
156 
158 
159  void resetSer();
160  void resetDeser();
161 
164 
166  FwSizeType numBytesToSkip);
168  FwSizeType numBytesToSkip);
172  virtual U8* getBuffAddr() = 0;
173  virtual const U8* getBuffAddr() const = 0;
174  const U8* getBuffAddrLeft() const;
175  U8* getBuffAddrSer();
177  SerializeStatus setBuff(const U8* src, Serializable::SizeType length);
180  SerializeBufferBase& dest,
181  Serializable::SizeType size);
182  // Will increment deserialization pointer
184  SerializeBufferBase& dest,
185  Serializable::SizeType size);
186  // Will increment deserialization pointer
187 
188 #ifdef BUILD_UT
189  bool operator==(const SerializeBufferBase& other) const;
190  friend std::ostream& operator<<(std::ostream& os, const SerializeBufferBase& buff);
191 #endif
192 
193  PROTECTED:
195 
196  PRIVATE:
197  // Copy constructor can be used only by the implementation
199 
200  void copyFrom(const SerializeBufferBase& src);
201  Serializable::SizeType m_serLoc;
202  Serializable::SizeType m_deserLoc;
203 };
204 
205 // Helper classes for building buffers with external storage
206 
209  public:
215  void setExtBuffer(U8* buffPtr, Serializable::SizeType size);
216  void clear();
218 
219  // pure virtual functions
221  U8* getBuffAddr();
222  const U8* getBuffAddr() const;
223 
226 
227  PROTECTED:
228  // data members
231 };
232 
238  public:
240  : ExternalSerializeBuffer(buffPtr, size) {}
245  (void)SerializeBufferBase::operator=(src);
246  return *this;
247  }
248 };
249 
256  public:
258  : ExternalSerializeBuffer(buffPtr, size) {}
264  // Ward against self-assignment
265  if (this != &src) {
266  this->setExtBuffer(src.m_buff, src.m_buffSize);
267  }
268  return *this;
269  }
270 };
271 
272 } // namespace Fw
273 #endif
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:29
float F32
32-bit floating point
Definition: BasicTypes.h:49
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:56
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
C++-compatible configuration header for fprime configuration.
External serialize buffer with no copy semantics.
U8 * getBuffAddr()
gets buffer address for data filling
ExternalSerializeBuffer(const ExternalSerializeBuffer &src)=delete
deleted copy constructor
Serializable::SizeType getBuffCapacity() const
returns capacity, not current size, of buffer
U8 * m_buff
pointer to external buffer
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
void clear()
clear external buffer
ExternalSerializeBuffer()
default constructor
ExternalSerializeBuffer & operator=(const SerializeBufferBase &src)=delete
deleted copy assignment operator
Serializable::SizeType m_buffSize
size of external buffer
ExternalSerializeBufferWithDataCopy(const SerializeBufferBase &src)=delete
ExternalSerializeBufferWithDataCopy(U8 *buffPtr, Serializable::SizeType size)
ExternalSerializeBufferWithDataCopy & operator=(SerializeBufferBase &src)
ExternalSerializeBufferWithMemberCopy(const ExternalSerializeBufferWithMemberCopy &src)
ExternalSerializeBufferWithMemberCopy(U8 *buffPtr, Serializable::SizeType size)
ExternalSerializeBufferWithMemberCopy & operator=(const ExternalSerializeBufferWithMemberCopy &src)
forward declaration
NATIVE_UINT_TYPE SizeType
Serializable()
Default constructor.
virtual ~Serializable()
destructor
virtual SerializeStatus deserialize(SerializeBufferBase &buffer)=0
deserialize to contents
virtual SerializeStatus serialize(SerializeBufferBase &buffer) const =0
serialize contents
@ OMIT_LENGTH
Omit length from serialization.
@ INCLUDE_LENGTH
Include length as first token in serialization.
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
SerializeStatus deserializeSize(FwSizeType &size)
deserialize a size value
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
void resetDeser()
reset deserialization to beginning
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
Serializable::SizeType getBuffLeft() const
returns how much deserialization buffer is left
const U8 * getBuffAddrLeft() const
gets address of remaining non-deserialized data.
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus serializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for serialization.
SerializeStatus moveSerToOffset(FwSizeType offset)
Moves serialization to the specified offset.
SerializeBufferBase()
default constructor
Serializable::SizeType getBuffLength() const
returns current buffer size
SerializeBufferBase & operator=(const SerializeBufferBase &src)
copy assignment operator
SerializeStatus setBuff(const U8 *src, Serializable::SizeType length)
sets buffer contents and size
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
virtual ~SerializeBufferBase()
destructor
SerializeStatus deserializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for deserialization.
SerializeStatus copyRaw(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
virtual const U8 * getBuffAddr() const =0
gets buffer address for data reading, const version
SerializeStatus copyRawOffset(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
virtual Serializable::SizeType getBuffCapacity() const =0
returns capacity, not current size, of buffer
SerializeStatus serializeSize(const FwSizeType size)
serialize a size value
SerializeStatus
forward declaration for string
@ FW_DESERIALIZE_FORMAT_ERROR
Deserialization data had incorrect values (unexpected data types)
@ FW_DESERIALIZE_BUFFER_EMPTY
Deserialization buffer was empty when trying to read more data.
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_SERIALIZE_FORMAT_ERROR
Data was the wrong format (e.g. wrong packet type)
@ FW_DESERIALIZE_TYPE_MISMATCH
Deserialized type ID didn't match.
@ FW_DESERIALIZE_SIZE_MISMATCH
Data was left in the buffer, but not enough to deserialize.
@ FW_SERIALIZE_NO_ROOM_LEFT
No room left in the buffer to serialize data.
#define U64(C)
Definition: sha.h:176