F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Buffer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Buffer.cpp
3 // \author mstarch
4 // \brief cpp file for Fw::Buffer implementation
5 //
6 // \copyright
7 // Copyright 2009-2020, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 #include <Fw/Buffer/Buffer.hpp>
13 #include <Fw/FPrimeBasicTypes.hpp>
14 #include <Fw/Types/Assert.hpp>
15 
16 #if FW_SERIALIZABLE_TO_STRING
17 #include <Fw/Types/String.hpp>
18 #endif
19 #include <cstring>
20 
21 namespace Fw {
22 
24  : Serializable(),
25  m_serialize_repr(),
26  m_bufferData(nullptr),
27  m_offset(0),
28  m_size(0),
29  m_capacity(0),
30  m_context(0xFFFFFFFF) {}
31 
33  : Serializable(),
34  m_serialize_repr(),
35  m_bufferData(src.m_bufferData),
36  m_offset(src.m_offset),
37  m_size(src.m_size),
38  m_capacity(src.m_capacity),
39  m_context(src.m_context) {
40  if (src.m_bufferData != nullptr) {
41  this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size);
42  }
43 }
44 
45 Buffer::Buffer(U8* data, FwSizeType size, U32 context)
46  : Serializable(),
47  m_serialize_repr(),
48  m_bufferData(data),
49  m_offset(0),
50  m_size(size),
51  m_capacity(size),
52  m_context(context) {
53  if (m_bufferData != nullptr) {
54  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
55  }
56 }
57 
59  // Ward against self-assignment
60  if (this != &src) {
61  this->m_bufferData = src.m_bufferData;
62  this->m_offset = src.m_offset;
63  this->m_size = src.m_size;
64  this->m_capacity = src.m_capacity;
65  this->m_context = src.m_context;
66  if (this->m_bufferData != nullptr) {
67  this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size);
68  }
69  }
70  return *this;
71 }
72 
73 bool Buffer::operator==(const Buffer& src) const {
74  return (this->m_bufferData == src.m_bufferData) && (this->m_offset == src.m_offset) &&
75  (this->m_size == src.m_size) && (this->m_capacity == src.m_capacity) && (this->m_context == src.m_context);
76 }
77 
78 bool Buffer::isValid() const {
79  return (this->m_bufferData != nullptr) && (this->m_size > 0);
80 }
81 
82 U8* Buffer::getData() const {
83  return (this->m_bufferData == nullptr) ? nullptr : (this->m_bufferData + this->m_offset);
84 }
85 
87  return this->m_bufferData;
88 }
89 
91  return this->m_size;
92 }
93 
95  return this->m_capacity;
96 }
97 
99  return this->m_offset;
100 }
101 
102 U32 Buffer::getContext() const {
103  return this->m_context;
104 }
105 
106 void Buffer::advance(const FwSignedSizeType amount) {
107  FW_ASSERT(this->m_bufferData != nullptr);
108  // New offset must remain within [0, capacity]
109  if (amount < 0) {
110  FW_ASSERT(this->m_offset >= static_cast<FwSizeType>(-amount), static_cast<FwAssertArgType>(this->m_offset),
111  static_cast<FwAssertArgType>(amount));
112  } else {
113  // Advancing must not move past the end of the represented data
114  FW_ASSERT(static_cast<FwSizeType>(amount) <= this->m_size, static_cast<FwAssertArgType>(this->m_size),
115  static_cast<FwAssertArgType>(amount));
116  }
117  this->m_offset = static_cast<FwSizeType>(static_cast<FwSignedSizeType>(this->m_offset) + amount);
118  // Keep the end of the represented data fixed
119  this->m_size = static_cast<FwSizeType>(static_cast<FwSignedSizeType>(this->m_size) - amount);
120  FW_ASSERT(this->m_offset + this->m_size <= this->m_capacity, static_cast<FwAssertArgType>(this->m_offset),
121  static_cast<FwAssertArgType>(this->m_size), static_cast<FwAssertArgType>(this->m_capacity));
122  this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size);
123 }
124 
125 void Buffer::setData(U8* const data) {
126  FW_ASSERT(this->m_bufferData != nullptr);
127  FW_ASSERT(data >= this->m_bufferData && data <= &this->m_bufferData[this->m_capacity]);
128  this->advance(static_cast<FwSignedSizeType>(data - (this->m_bufferData + this->m_offset)));
129 }
130 
131 void Buffer::setSize(const FwSizeType size) {
132  FW_ASSERT(this->m_offset + size <= this->m_capacity, static_cast<FwAssertArgType>(this->m_offset),
133  static_cast<FwAssertArgType>(size), static_cast<FwAssertArgType>(this->m_capacity));
134  this->m_size = size;
135  if (m_bufferData != nullptr) {
136  this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size);
137  }
138 }
139 
140 void Buffer::setContext(const U32 context) {
141  this->m_context = context;
142 }
143 
144 void Buffer::set(U8* const data, const FwSizeType size, const U32 context) {
145  this->m_bufferData = data;
146  this->m_offset = 0;
147  this->m_size = size;
148  this->m_capacity = size;
149  if (m_bufferData != nullptr) {
150  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
151  }
152  this->m_context = context;
153 }
154 
156  if (this->isValid()) {
157  Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData + this->m_offset, this->m_size);
158  esb.resetSer();
159  return esb;
160  } else {
162  }
163 }
164 
166  if (this->isValid()) {
167  Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData + this->m_offset, this->m_size);
168  Fw::SerializeStatus stat = esb.setBuffLen(this->m_size);
170  return esb;
171  } else {
173  }
174 }
175 
177  Fw::SerializeStatus stat;
178  stat = buffer.serializeFrom(reinterpret_cast<PlatformPointerCastType>(this->m_bufferData), mode);
179  if (stat != Fw::FW_SERIALIZE_OK) {
180  return stat;
181  }
182  stat = buffer.serializeFrom(this->m_size, mode);
183  if (stat != Fw::FW_SERIALIZE_OK) {
184  return stat;
185  }
186  stat = buffer.serializeFrom(this->m_context, mode);
187  if (stat != Fw::FW_SERIALIZE_OK) {
188  return stat;
189  }
190  stat = buffer.serializeFrom(this->m_offset, mode);
191  if (stat != Fw::FW_SERIALIZE_OK) {
192  return stat;
193  }
194  stat = buffer.serializeFrom(this->m_capacity, mode);
195  if (stat != Fw::FW_SERIALIZE_OK) {
196  return stat;
197  }
198  return stat;
199 }
200 
202  Fw::SerializeStatus stat;
203  PlatformPointerCastType pointer;
204  stat = buffer.deserializeTo(pointer, mode);
205  if (stat != Fw::FW_SERIALIZE_OK) {
206  return stat;
207  }
208  this->m_bufferData = reinterpret_cast<U8*>(pointer);
209 
210  stat = buffer.deserializeTo(this->m_size, mode);
211  if (stat != Fw::FW_SERIALIZE_OK) {
212  return stat;
213  }
214  stat = buffer.deserializeTo(this->m_context, mode);
215  if (stat != Fw::FW_SERIALIZE_OK) {
216  return stat;
217  }
218  stat = buffer.deserializeTo(this->m_offset, mode);
219  if (stat != Fw::FW_SERIALIZE_OK) {
220  return stat;
221  }
222  stat = buffer.deserializeTo(this->m_capacity, mode);
223  if (stat != Fw::FW_SERIALIZE_OK) {
224  return stat;
225  }
226 
227  if (this->m_bufferData != nullptr) {
228  this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size);
229  }
230  return stat;
231 }
232 
233 #if FW_SERIALIZABLE_TO_STRING
234 void Buffer::toString(Fw::StringBase& text) const {
235  static const char* formatString = "(data = %p, size = %u, context = %u, offset = %u, capacity = %u)";
236  (void)text.format(formatString, this->m_bufferData, this->m_size, this->m_context, this->m_offset,
237  this->m_capacity); // display string may safely truncate
238 }
239 #endif
240 
241 #ifdef BUILD_UT
242 std::ostream& operator<<(std::ostream& os, const Buffer& obj) {
243  Fw::String str;
244  obj.toString(str);
245  os << str.toChar();
246  return os;
247 }
248 #endif
249 
250 } // end namespace Fw
Serialization/Deserialization operation was successful.
void setData(U8 *data)
Definition: Buffer.cpp:125
void set(U8 *data, FwSizeType size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:144
PlatformSizeType FwSizeType
void setContext(U32 context)
Definition: Buffer.cpp:140
void setSize(FwSizeType size)
Definition: Buffer.cpp:131
U32 getContext() const
Definition: Buffer.cpp:102
U8 * getData() const
Definition: Buffer.cpp:82
void advance(FwSignedSizeType amount)
Definition: Buffer.cpp:106
bool operator==(const Buffer &src) const
Definition: Buffer.cpp:73
PlatformSignedSizeType FwSignedSizeType
Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG)
Definition: Buffer.cpp:201
Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &serialBuffer, Fw::Endianness mode=Fw::Endianness::BIG) const
Definition: Buffer.cpp:176
FwSizeType getCapacity() const
Definition: Buffer.cpp:94
virtual SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG)=0
Serialize an 8-bit unsigned integer value.
SerializeStatus
forward declaration for string
virtual SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG)=0
Deserialize an 8-bit unsigned integer value.
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:165
bool isValid() const
Definition: Buffer.cpp:78
FwSizeType getOffset() const
Definition: Buffer.cpp:98
void resetSer() override
Reset serialization pointer to beginning of buffer.
const char * toChar() const
Convert to a C-style char*.
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:58
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
SerializeStatus setBuffLen(Serializable::SizeType length) override
Set buffer length manually.
FwSizeType getSize() const
Definition: Buffer.cpp:90
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Set the external buffer.
External serialize buffer with member copy semantics.
Implementation of malloc based allocator.
Endianness
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:155
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Buffer & operator=(const Buffer &src)
Definition: Buffer.cpp:58
U8 * getOriginalData() const
Definition: Buffer.cpp:86