ARGoS 3
A parallel, multi-engine simulator for swarm robotics
byte_array.h
Go to the documentation of this file.
1
11#ifndef BYTE_ARRAY_H
12#define BYTE_ARRAY_H
13
14#include <argos3/core/utility/datatypes/datatypes.h>
15#include <argos3/core/utility/configuration/argos_exception.h>
16#include <vector>
17#include <iterator>
18#include <unistd.h>
19
20namespace argos {
21
28 class CByteArray {
29
30 public:
31
36
40 CByteArray(const CByteArray& c_byte_array) :
41 m_vecBuffer(c_byte_array.m_vecBuffer) {}
42
50 CByteArray(const UInt8* pun_buffer,
51 size_t un_size);
52
59 CByteArray(size_t un_size,
60 UInt8 un_value = 0);
61
66 inline size_t Size() const {
67 return m_vecBuffer.size();
68 }
69
83 inline void Resize(size_t un_size,
84 UInt8 un_value = 0) {
85 m_vecBuffer.resize(un_size, un_value);
86 }
87
92 inline void Swap(CByteArray& c_other) {
93 m_vecBuffer.swap(c_other.m_vecBuffer);
94 }
95
100 inline bool Empty() const {
101 return m_vecBuffer.empty();
102 }
103
112 inline const UInt8* ToCArray() const {
113 return !Empty() ? &m_vecBuffer[0] : NULL;
114 }
115
124 inline UInt8* ToCArray() {
125 return !Empty() ? &m_vecBuffer[0] : NULL;
126 }
127
134 inline void Clear() {
135 m_vecBuffer.clear();
136 }
137
143 void Zero();
144
149 CByteArray& operator=(const CByteArray& c_byte_array);
150
157 inline UInt8& operator[](size_t un_index) {
158 if(un_index >= Size()) THROW_ARGOSEXCEPTION("CByteArray: index out of bounds [index = " << un_index << ", size=" << Size() << "]");
159 return m_vecBuffer.at(un_index);
160 }
161
168 inline UInt8 operator[](size_t un_index) const {
169 if(un_index >= Size()) THROW_ARGOSEXCEPTION("CByteArray: index out of bounds [index = " << un_index << ", size=" << Size() << "]");
170 return m_vecBuffer.at(un_index);
171 }
172
177 bool operator==(const CByteArray& c_byte_array) const;
178
186 CByteArray& AddBuffer(const UInt8* pun_buffer,
187 size_t un_size);
188
196 CByteArray& FetchBuffer(UInt8* pun_buffer,
197 size_t un_size);
198
209 template<typename T> T PopFront() {
210 T tRetVal;
211 *this >> tRetVal;
212 return tRetVal;
213 }
214
223 CByteArray* operator()(size_t un_start,
224 ssize_t un_end = -1);
225
231 CByteArray& operator<<(UInt8 un_value);
232
239 CByteArray& operator>>(UInt8& un_value);
240
246 CByteArray& operator<<(SInt8 n_value);
247
254 CByteArray& operator>>(SInt8& n_value);
255
261 CByteArray& operator<<(UInt16 un_value);
262
269 CByteArray& operator>>(UInt16& un_value);
270
276 CByteArray& operator<<(SInt16 n_value);
277
284 CByteArray& operator>>(SInt16& n_value);
285
291 CByteArray& operator<<(UInt32 un_value);
292
299 CByteArray& operator>>(UInt32& un_value);
300
306 CByteArray& operator<<(SInt32 n_value);
307
314 CByteArray& operator>>(SInt32& n_value);
315
321 CByteArray& operator<<(UInt64 un_value);
322
329 CByteArray& operator>>(UInt64& un_value);
330
336 CByteArray& operator<<(SInt64 n_value);
337
344 CByteArray& operator>>(SInt64& n_value);
345
354 CByteArray& operator<<(unsigned long int un_value);
355
365 CByteArray& operator>>(unsigned long int& un_value);
366
375 CByteArray& operator<<(signed long int n_value);
376
386 CByteArray& operator>>(signed long int& n_value);
387
395 CByteArray& operator<<(double f_value);
396
406 CByteArray& operator>>(double& f_value);
407
415 CByteArray& operator<<(float f_value);
416
426 CByteArray& operator>>(float& f_value);
427
433 CByteArray& operator<<(const std::string& str_value);
434
441 CByteArray& operator>>(std::string& str_value);
442
450 friend std::ostream& operator<<(std::ostream& c_os, const CByteArray& c_byte_array);
451
452 private:
453
454 std::vector<UInt8> m_vecBuffer;
455
456 };
457
458}
459
460#endif
signed int SInt32
32-bit signed integer.
Definition datatypes.h:93
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
signed short SInt16
16-bit signed integer.
Definition datatypes.h:74
unsigned char UInt8
8-bit unsigned integer.
Definition datatypes.h:60
signed long long SInt64
64-bit signed integer.
Definition datatypes.h:103
unsigned long long UInt64
64-bit unsigned integer.
Definition datatypes.h:107
unsigned short UInt16
16-bit unsigned integer.
Definition datatypes.h:78
signed char SInt8
8-bit signed integer.
Definition datatypes.h:45
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
Byte array utility class.
Definition byte_array.h:28
void Zero()
Sets the contents of the byte array to all zeros.
CByteArray(const CByteArray &c_byte_array)
Class copy constructor.
Definition byte_array.h:40
CByteArray & operator=(const CByteArray &c_byte_array)
Assignment operator.
void Clear()
Clears the byte array.
Definition byte_array.h:134
T PopFront()
Removes data from the beginning of the byte array and returns it.
Definition byte_array.h:209
size_t Size() const
Returns the current size of the byte array.
Definition byte_array.h:66
CByteArray & AddBuffer(const UInt8 *pun_buffer, size_t un_size)
Appends bytes to the byte array.
CByteArray & FetchBuffer(UInt8 *pun_buffer, size_t un_size)
Moves elements from the byte array into the passed buffer.
UInt8 & operator[](size_t un_index)
Read/write index operator.
Definition byte_array.h:157
void Swap(CByteArray &c_other)
Swaps the content of this byte array with the content of the passed one.
Definition byte_array.h:92
bool Empty() const
Returns true if the byte array is empty.
Definition byte_array.h:100
CByteArray()
Class constructor.
Definition byte_array.h:35
bool operator==(const CByteArray &c_byte_array) const
Equality comparison operator.
CByteArray & operator>>(UInt8 &un_value)
Moves an 8-bit unsigned integer from the beginning of the byte array to the target variable.
const UInt8 * ToCArray() const
Returns the contents of the byte array as a const c-style array.
Definition byte_array.h:112
UInt8 * ToCArray()
Returns the contents of the byte array as a c-style array.
Definition byte_array.h:124
UInt8 operator[](size_t un_index) const
Read-only index operator.
Definition byte_array.h:168
friend std::ostream & operator<<(std::ostream &c_os, const CByteArray &c_byte_array)
Stream operator.
void Resize(size_t un_size, UInt8 un_value=0)
Resizes the byte array to the wanted size.
Definition byte_array.h:83
CByteArray * operator()(size_t un_start, ssize_t un_end=-1)
Returns a new byte array that corresponds to a part of this byte array.