byte_array.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef FLATHEADERS
35 #else
36 #include "byte_array.h"
37 #include "simple_serialize.h"
38 #include "log_wrapper.h"
39 #endif
40 
41 namespace industrial
42 {
43 namespace byte_array
44 {
45 
46 using namespace industrial::simple_serialize;
47 using namespace industrial::shared_types;
48 using namespace industrial::byte_array;
49 
51 {
52  this->init();
53 #ifdef BYTE_SWAPPING
54  LOG_COMM("Byte swapping enabled");
55 #endif
56 }
57 
59 {
60 }
61 
63 {
64  this->buffer_.clear();
65 }
66 
67 bool ByteArray::init(const char* buffer, const shared_int byte_size)
68 {
69  bool rtn;
70 
71  if (this->getMaxBufferSize() >= byte_size)
72  {
73  LOG_COMM("Initializing buffer to size: %d", byte_size);
74  this->load((void*)buffer, byte_size);
75  rtn = true;
76  }
77  else
78  {
79  LOG_ERROR("Failed to initialize byte array, buffer size: %u greater than max: %u",
80  byte_size, this->getMaxBufferSize());
81  rtn = false;
82  }
83  return rtn;
84 }
85 
87 {
88  if (buffer.getBufferSize() != 0)
89  {
90  this->buffer_ = buffer.buffer_;
91  }
92  else
93  {
94  LOG_WARN("Byte array copy not performed, buffer to copy is empty");
95  }
96 }
97 
98 void ByteArray::copyTo(std::vector<char> &out)
99 {
100  out.assign(buffer_.begin(), buffer_.end());
101 }
102 
103 
104 #ifdef BYTE_SWAPPING
105 void ByteArray::swap(void *value, shared_int byteSize)
106 {
107  LOG_COMM("Executing byte swapping");
108 
109  LOG_COMM("Value (swapping-input): %u", (unsigned int)(*(unsigned int*)value));
110  for (unsigned int i = 0; i < byteSize / 2; i++)
111  {
112  unsigned int endIndex = byteSize - i - 1;
113  char endByte = ((char*)value)[endIndex];
114  unsigned int endInt = endByte;
115 
116  unsigned int beginIndex = i;
117  char beginByte = ((char*)value)[beginIndex];
118  unsigned int beginInt = beginByte;
119 
120  LOG_COMM("Swap beginIndex i: %u, endIndex: %u, begin[]: %u, end[]: %u",
121  beginIndex, endIndex, beginInt, endInt);
122  ((char*)value)[endIndex] = beginByte;
123  ((char*)value)[beginIndex] = endByte;
124  }
125  LOG_COMM("Value (swapping-output): %u", (unsigned int)(*(unsigned int*)value));
126 
127 }
128 #endif
129 
130 char* ByteArray::getRawDataPtr()
131 {
132  this->copyTo( this->getRawDataPtr_buffer_ );
133  return &getRawDataPtr_buffer_[0];
134 }
135 
136 /****************************************************************
137  // load(*)
138  //
139  // Methods for loading various data types.
140  //
141  */
143 {
144  return this->load(&value, sizeof(shared_bool));
145 }
146 
148 {
149 #ifdef BYTE_SWAPPING
150  LOG_COMM("Value (loading-input): %f", value);
151  this->swap(&value, sizeof(shared_real));
152  LOG_COMM("Value (loading-output): %f", value);
153 #endif
154 
155  return this->load(&value, sizeof(shared_real));
156 }
157 
159 {
160 #ifdef BYTE_SWAPPING
161  LOG_COMM("Value (loading-input): %d", value);
162  this->swap(&value, sizeof(shared_int));
163  LOG_COMM("Value (loading-output): %d", value);
164 #endif
165 
166  return this->load(&value, sizeof(shared_int));
167 }
168 
170 {
171  LOG_COMM("Executing byte array load through simple serialize");
172  return value.load(this);
173 }
174 
176 {
177  LOG_COMM("Executing byte array load through byte array");
178  std::deque<char>& src = value.buffer_;
179  std::deque<char>& dest = this->buffer_;
180 
181  if (this->getBufferSize()+value.getBufferSize() > this->getMaxBufferSize())
182  {
183  LOG_ERROR("Additional data would exceed buffer size");
184  return false;
185  }
186 
187  dest.insert(dest.end(), src.begin(), src.end());
188  return true;
189 }
190 
191 bool ByteArray::load(void* value, const shared_int byte_size)
192 {
193 
194  bool rtn;
195 
196  LOG_COMM("Executing byte array load through void*, size: %d", byte_size);
197  // Check inputs
198  if (NULL == value)
199  {
200  LOG_ERROR("NULL point passed into load method");
201  return false;
202  }
203  if (this->getBufferSize()+byte_size > this->getMaxBufferSize())
204  {
205  LOG_ERROR("Additional data would exceed buffer size");
206  return false;
207  }
208 
209  try
210  {
211  char* bytePtr = (char*)value;
212  this->buffer_.insert(this->buffer_.end(), bytePtr, bytePtr + byte_size);
213 
214  rtn = true;
215  }
216  catch (std::exception)
217  {
218  LOG_ERROR("Failed to load byte array");
219  rtn = false;
220  }
221 
222  return rtn;
223 }
224 
225 /****************************************************************
226  // unload(*)
227  //
228  // Methods for unloading various data types. Unloading data shortens
229  // the internal buffer. The resulting memory that holds the data is
230  // lost.
231  //
232  */
234 {
235  shared_bool rtn = this->unload(&value, sizeof(shared_bool));
236  return rtn;
237 
238 }
239 
241 {
242  bool rtn = this->unload(&value, sizeof(shared_real));
243 
244 #ifdef BYTE_SWAPPING
245  LOG_COMM("Value (unloading-input): %f", value);
246  this->swap(&value, sizeof(shared_real));
247  LOG_COMM("Value (unloading-output): %f", value);
248 #endif
249 
250  return rtn;
251 }
252 
254 {
255  bool rtn = this->unload(&value, sizeof(shared_int));
256 
257 #ifdef BYTE_SWAPPING
258  LOG_COMM("Value (unloading-input): %d", value);
259  this->swap(&value, sizeof(shared_int));
260  LOG_COMM("Value (unloading-output): %d", value);
261 #endif
262  return rtn;
263 }
264 
266 {
267  LOG_COMM("Executing byte array unload through simple serialize");
268  return value.unload(this);
269 }
270 
271 
272 bool ByteArray::unload(ByteArray &value, const shared_int byte_size)
273 {
274  LOG_COMM("Executing byte array unload through byte array");
275  bool rtn;
276 
277  if (byte_size <= this->getBufferSize())
278  {
279  std::deque<char>& src = this->buffer_;
280  std::deque<char>& dest = value.buffer_;
281 
282  dest.insert(dest.end(), src.end()-byte_size, src.end());
283  src.erase(src.end()-byte_size, src.end());
284  rtn = true;
285  }
286  else
287  {
288  LOG_ERROR("Buffer smaller than requested size.");
289  rtn = false;
290  }
291 
292  return rtn;
293 }
294 
295 bool ByteArray::unload(void* value, shared_int byteSize)
296 {
297  bool rtn;
298 
299  LOG_COMM("Executing byte array unload through void*, size: %d", byteSize);
300  // Check inputs
301  if (NULL == value)
302  {
303  LOG_ERROR("NULL point passed into unload method");
304  return false;
305  }
306 
307  if (byteSize <= this->getBufferSize())
308  {
309  std::deque<char>& src = this->buffer_;
310 
311  std::copy(src.end()-byteSize, src.end(), (char*)value);
312  src.erase(src.end()-byteSize, src.end());
313  rtn = true;
314  }
315  else
316  {
317  LOG_ERROR("Buffer is smaller than requested byteSize.");
318  rtn = false;
319  }
320 
321  return rtn;
322 }
323 
324 
325 
326 /****************************************************************
327  // unloadFront(*)
328  //
329  // Methods for unloading various data types. Unloading data shortens
330  // the internal buffer and requires a memmove. These functions should
331  // be used sparingly, as they are expensive.
332  //
333  */
335 {
336  bool rtn = this->unloadFront(&value, sizeof(shared_real));
337 
338 #ifdef BYTE_SWAPPING
339  LOG_COMM("Value (unloading-input): %f", value);
340  this->swap(&value, sizeof(shared_real));
341  LOG_COMM("Value (unloading-output): %f", value);
342 #endif
343  return rtn;
344 }
345 
347 {
348  bool rtn = this->unloadFront(&value, sizeof(shared_int));
349 
350 #ifdef BYTE_SWAPPING
351  LOG_COMM("Value (unloading-input): %d", value);
352  this->swap(&value, sizeof(shared_int));
353  LOG_COMM("Value (unloading-output): %d", value);
354 #endif
355  return rtn;
356 }
357 
359 {
360  bool rtn;
361 
362  LOG_COMM("Executing byte array unloadFront through void*, size: %d", byteSize);
363  // Check inputs
364  if (NULL == value)
365  {
366  LOG_ERROR("NULL point passed into unloadFront method");
367  return false;
368  }
369 
370  if (byteSize <= this->getBufferSize())
371  {
372  std::deque<char>& src = this->buffer_;
373 
374  std::copy(src.begin(), src.begin()+byteSize, (char*)value);
375  src.erase(src.begin(), src.begin()+byteSize);
376  rtn = true;
377  }
378  else
379  {
380  LOG_ERROR("Buffer is smaller than requested byteSize.");
381  rtn = false;
382  }
383 
384  return rtn;
385 }
386 
388 {
389  return this->buffer_.size();
390 }
391 
393 {
394  return this->buffer_.max_size();
395 }
396 
397 
399 {
400 #ifdef BYTE_SWAPPING
401  return true;
402 #endif
403  return false;
404 }
405 
406 } // namespace byte_array
407 } // namespace industrial
industrial::shared_types::shared_bool
bool shared_bool
Definition: shared_types.h:71
LOG_WARN
#define LOG_WARN(format,...)
Definition: log_wrapper.h:133
industrial::byte_array::ByteArray::ByteArray
ByteArray(void)
Default constructor.
Definition: byte_array.cpp:50
industrial::shared_types
Contains platform specific type definitions that guarantee the size of primitive data types.
Definition: shared_types.h:52
industrial::byte_array::ByteArray::copyTo
void copyTo(std::vector< char > &out)
Copy to std::vector, for raw-ptr access.
Definition: byte_array.cpp:98
industrial
Definition: byte_array.h:45
industrial::byte_array::ByteArray::unload
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array
Definition: byte_array.cpp:233
industrial::byte_array::ByteArray::getMaxBufferSize
unsigned int getMaxBufferSize()
gets current buffer size
Definition: byte_array.cpp:392
industrial::simple_serialize
Definition: byte_array.h:47
industrial::byte_array::ByteArray::isByteSwapEnabled
static bool isByteSwapEnabled()
returns true if byte swapping is enabled (this is a global option set by compiler flag)....
Definition: byte_array.cpp:398
byte_array.h
industrial::byte_array::ByteArray::~ByteArray
~ByteArray(void)
Destructor.
Definition: byte_array.cpp:58
LOG_ERROR
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
industrial::shared_types::shared_real
float shared_real
Definition: shared_types.h:66
industrial::byte_array::ByteArray
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
LOG_COMM
#define LOG_COMM(format,...)
Definition: log_wrapper.h:130
industrial::byte_array::ByteArray::init
void init()
Initializes or Reinitializes an empty buffer.
Definition: byte_array.cpp:62
industrial::simple_serialize::SimpleSerialize::load
virtual bool load(industrial::byte_array::ByteArray *buffer)=0
Virtual method for loading an object into a ByteArray.
industrial::byte_array::ByteArray::copyFrom
void copyFrom(ByteArray &buffer)
Deep-Copy.
Definition: byte_array.cpp:86
industrial::simple_serialize::SimpleSerialize
Interface for loading and unloading a class to/from a ByteArray.
Definition: simple_serialize.h:52
log_wrapper.h
industrial::shared_types::shared_int
int shared_int
Definition: shared_types.h:62
industrial::byte_array
Definition: byte_array.h:56
industrial::byte_array::ByteArray::load
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
Definition: byte_array.cpp:142
industrial::simple_serialize::SimpleSerialize::unload
virtual bool unload(industrial::byte_array::ByteArray *buffer)=0
Virtual method for unloading an object from a ByteArray.
init
void init(const M_string &remappings)
industrial::byte_array::ByteArray::unloadFront
bool unloadFront(industrial::shared_types::shared_real &value)
unloads a double value from the beginning of the byte array. If byte swapping is enabled,...
Definition: byte_array.cpp:334
industrial::byte_array::ByteArray::getBufferSize
unsigned int getBufferSize()
gets current buffer size
Definition: byte_array.cpp:387
simple_serialize.h
industrial::byte_array::ByteArray::buffer_
std::deque< char > buffer_
internal data buffer
Definition: byte_array.h:341


simple_message
Author(s): Shaun Edwards
autogenerated on Wed Mar 2 2022 00:24:53