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 
10 namespace Fw {
11 
12  class StringBase;
13  typedef enum {
22  class SerializeBufferBase;
23 
24  class Serializable {
25  public:
26  virtual SerializeStatus serialize(SerializeBufferBase& buffer) const = 0;
28 #if FW_SERIALIZABLE_TO_STRING || FW_ENABLE_TEXT_LOGGING || BUILD_UT
29  virtual void toString(StringBase& text) const;
30 #endif
31 
32 #ifdef BUILD_UT
33  friend std::ostream& operator<<(std::ostream& os, const Serializable& val);
34 #endif
35 
36  protected:
37  Serializable();
38  virtual ~Serializable();
39  };
40 
42  public:
43 
45 
46  virtual ~SerializeBufferBase();
47 
48  // Serialization for built-in types
49 
52 
53 #if FW_HAS_16_BIT==1
54  SerializeStatus serialize(U16 val);
55  SerializeStatus serialize(I16 val);
56 #endif
57 #if FW_HAS_32_BIT==1
58  SerializeStatus serialize(U32 val);
59  SerializeStatus serialize(I32 val);
60 #endif
61 #if FW_HAS_64_BIT==1
63  SerializeStatus serialize(I64 val);
64 #endif
66 #if FW_HAS_F64
67  SerializeStatus serialize(F64 val);
68 #endif
69  SerializeStatus serialize(bool val);
70 
71  SerializeStatus serialize(const void* val);
72 
73  SerializeStatus serialize(const U8* buff, NATIVE_UINT_TYPE length, bool noLength = false);
74 
76 
78 
79  // Deserialization for built-in types
80 
83 
84 #if FW_HAS_16_BIT==1
85  SerializeStatus deserialize(U16 &val);
86  SerializeStatus deserialize(I16 &val);
87 #endif
88 
89 #if FW_HAS_32_BIT==1
90  SerializeStatus deserialize(U32 &val);
91  SerializeStatus deserialize(I32 &val);
92 #endif
93 #if FW_HAS_64_BIT==1
95  SerializeStatus deserialize(I64 &val);
96 #endif
98 #if FW_HAS_F64
99  SerializeStatus deserialize(F64 &val);
100 #endif
101  SerializeStatus deserialize(bool &val);
102 
103  SerializeStatus deserialize(void*& val);
104 
105  // length should be set to max, returned value is actual size stored. If noLength
106  // is true, use the length variable as the actual number of bytes to deserialize
107  SerializeStatus deserialize(U8* buff, NATIVE_UINT_TYPE& length, bool noLength = false);
108  // serialize/deserialize Serializable
109 
110 
112 
114 
115  void resetSer();
116  void resetDeser();
117 
120 
121  SerializeStatus serializeSkip(FwSizeType numBytesToSkip);
122  SerializeStatus deserializeSkip(FwSizeType numBytesToSkip);
123  virtual NATIVE_UINT_TYPE getBuffCapacity() const = 0;
125  NATIVE_UINT_TYPE getBuffLeft() const;
126  virtual U8* getBuffAddr() = 0;
127  virtual const U8* getBuffAddr() const = 0;
128  const U8* getBuffAddrLeft() const;
129  U8* getBuffAddrSer();
130  SerializeStatus setBuff(const U8* src, NATIVE_UINT_TYPE length);
133  // Will increment deserialization pointer
135  // Will increment deserialization pointer
136 
137 
138 #ifdef BUILD_UT
139  bool operator==(const SerializeBufferBase& other) const;
140  friend std::ostream& operator<<(std::ostream& os, const SerializeBufferBase& buff);
141 #endif
142  PROTECTED:
143 
145 
146  PRIVATE:
147  // A no-implementation copy constructor here will prevent the default copy constructor from being called
148  // accidentally, and without an implementation it will create an error for the developer instead.
150 
151  void copyFrom(const SerializeBufferBase& src);
152  NATIVE_UINT_TYPE m_serLoc;
153  NATIVE_UINT_TYPE m_deserLoc;
154  };
155 
156  // Helper class for building buffers with external storage
157 
159  public:
162  void setExtBuffer(U8* buffPtr, NATIVE_UINT_TYPE size);
163  void clear();
164 
165  // pure virtual functions
167  U8* getBuffAddr();
168  const U8* getBuffAddr() const ;
169 
170  PRIVATE:
171 
172  // no copying
175 
176  // private data
177  U8* m_buff;
178  NATIVE_UINT_TYPE m_buffSize;
179  };
180 
181 }
182 #endif
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:25
float F32
32-bit floating point
Definition: BasicTypes.h:45
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformSizeType FwSizeType
Definition: FpConfig.h:18
C++-compatible configuration header for fprime configuration.
NATIVE_UINT_TYPE getBuffCapacity() const
returns capacity, not current size, of buffer
U8 * getBuffAddr()
gets buffer address for data filling
void setExtBuffer(U8 *buffPtr, NATIVE_UINT_TYPE size)
Set the external buffer.
void clear()
clear external buffer
ExternalSerializeBuffer()
default constructor
forward declaration
Serializable()
Default constructor.
virtual ~Serializable()
destructor
virtual SerializeStatus deserialize(SerializeBufferBase &buffer)=0
deserialize to contents
virtual SerializeStatus serialize(SerializeBufferBase &buffer) const =0
serialize contents
NATIVE_UINT_TYPE getBuffLeft() const
returns how much deserialization buffer is left
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
void resetDeser()
reset deserialization to beginning
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
SerializeStatus setBuffLen(NATIVE_UINT_TYPE length)
sets buffer length manually after filling with data
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
SerializeStatus setBuff(const U8 *src, NATIVE_UINT_TYPE length)
sets buffer contents and size
SerializeBufferBase & operator=(const SerializeBufferBase &src)
equal operator
virtual NATIVE_UINT_TYPE getBuffCapacity() const =0
returns capacity, not current size, of buffer
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
virtual ~SerializeBufferBase()
destructor
SerializeStatus deserializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for deserialization.
U8 * getBuffAddrSer()
gets address of end of serialization. DANGEROUS! Need to know max buffer size and adjust when done
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, NATIVE_UINT_TYPE size)
directly copies buffer without looking for a size in the stream.
SerializeStatus copyRaw(SerializeBufferBase &dest, NATIVE_UINT_TYPE size)
directly copies buffer without looking for a size in the stream.
NATIVE_UINT_TYPE getBuffLength() const
returns current buffer size
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