argos::CByteArray Class Reference

Byte array utility class. More...

#include <byte_array.h>

Public Member Functions

 CByteArray ()
 Class constructor. More...
 
 CByteArray (const CByteArray &c_byte_array)
 Class copy constructor. More...
 
 CByteArray (const UInt8 *pun_buffer, size_t un_size)
 Class constructor. More...
 
 CByteArray (size_t un_size, UInt8 un_value=0)
 Class constructor. More...
 
size_t Size () const
 Returns the current size of the byte array. More...
 
void Resize (size_t un_size, UInt8 un_value=0)
 Resizes the byte array to the wanted size. More...
 
void Swap (CByteArray &c_other)
 Swaps the content of this byte array with the content of the passed one. More...
 
bool Empty () const
 Returns true if the byte array is empty. More...
 
const UInt8ToCArray () const
 Returns the contents of the byte array as a const c-style array. More...
 
UInt8ToCArray ()
 Returns the contents of the byte array as a c-style array. More...
 
void Clear ()
 Clears the byte array. More...
 
void Zero ()
 Sets the contents of the byte array to all zeros. More...
 
CByteArrayoperator= (const CByteArray &c_byte_array)
 Assignment operator. More...
 
UInt8operator[] (size_t un_index)
 Read/write index operator. More...
 
UInt8 operator[] (size_t un_index) const
 Read-only index operator. More...
 
bool operator== (const CByteArray &c_byte_array) const
 Equality comparison operator. More...
 
CByteArrayAddBuffer (const UInt8 *pun_buffer, size_t un_size)
 Appends bytes to the byte array. More...
 
CByteArrayFetchBuffer (UInt8 *pun_buffer, size_t un_size)
 Moves elements from the byte array into the passed buffer. More...
 
template<typename T >
PopFront ()
 Removes data from the beginning of the byte array and returns it. More...
 
CByteArrayoperator() (size_t un_start, ssize_t un_end=-1)
 Returns a new byte array that corresponds to a part of this byte array. More...
 
CByteArrayoperator<< (UInt8 un_value)
 Appends a 8-bit unsigned integer to the byte array. More...
 
CByteArrayoperator>> (UInt8 &un_value)
 Moves an 8-bit unsigned integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (SInt8 n_value)
 Appends a 8-bit signed integer to the byte array. More...
 
CByteArrayoperator>> (SInt8 &n_value)
 Moves an 8-bit signed integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (UInt16 un_value)
 Appends a 16-bit unsigned integer to the byte array. More...
 
CByteArrayoperator>> (UInt16 &un_value)
 Moves a 16-bit unsigned integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (SInt16 n_value)
 Appends a 16-bit signed integer to the byte array. More...
 
CByteArrayoperator>> (SInt16 &n_value)
 Moves a 16-bit signed integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (UInt32 un_value)
 Appends a 32-bit unsigned integer to the byte array. More...
 
CByteArrayoperator>> (UInt32 &un_value)
 Moves a 32-bit unsigned integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (SInt32 n_value)
 Appends a 32-bit signed integer to the byte array. More...
 
CByteArrayoperator>> (SInt32 &n_value)
 Moves a 32-bit signed integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (UInt64 un_value)
 Appends a 64-bit unsigned integer to the byte array. More...
 
CByteArrayoperator>> (UInt64 &un_value)
 Moves a 64-bit unsigned integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (SInt64 n_value)
 Appends a 64-bit signed integer to the byte array. More...
 
CByteArrayoperator>> (SInt64 &n_value)
 Moves a 64-bit signed integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (unsigned long int un_value)
 Appends an unsigned long integer to the byte array. More...
 
CByteArrayoperator>> (unsigned long int &un_value)
 Moves an unsigned long integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (signed long int n_value)
 Appends a signed long integer to the byte array. More...
 
CByteArrayoperator>> (signed long int &n_value)
 Moves a signed long integer from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (double f_value)
 Appends a double to the byte array. More...
 
CByteArrayoperator>> (double &f_value)
 Moves a double from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (float f_value)
 Appends a float to the byte array. More...
 
CByteArrayoperator>> (float &f_value)
 Moves a float from the beginning of the byte array to the target variable. More...
 
CByteArrayoperator<< (const std::string &str_value)
 Appends a std::string to the byte array. More...
 
CByteArrayoperator>> (std::string &str_value)
 Moves a std::string from the beginning of the byte array to the target variable. More...
 

Friends

std::ostream & operator<< (std::ostream &c_os, const CByteArray &c_byte_array)
 Stream operator. More...
 

Detailed Description

Byte array utility class.

This class is useful for serializing any kind of data into a byte array, to be then streamed to something. It internally stores the data in network order.

Definition at line 28 of file byte_array.h.

Constructor & Destructor Documentation

argos::CByteArray::CByteArray ( )
inline

Class constructor.

Definition at line 35 of file byte_array.h.

argos::CByteArray::CByteArray ( const CByteArray c_byte_array)
inline

Class copy constructor.

Definition at line 40 of file byte_array.h.

argos::CByteArray::CByteArray ( const UInt8 pun_buffer,
size_t  un_size 
)

Class constructor.

Copies the given buffer into the byte array. The original buffer can be safely deleted.

Parameters
pun_bufferthe original buffer to copy from.
un_sizethe size of the original buffer.

Definition at line 65 of file byte_array.cpp.

argos::CByteArray::CByteArray ( size_t  un_size,
UInt8  un_value = 0 
)

Class constructor.

Creates a byte array that contains un_size copies of un_value.

Parameters
un_sizethe initial size of the byte array.
un_valuethe value to use to populate the byte array.

Definition at line 73 of file byte_array.cpp.

Member Function Documentation

CByteArray & argos::CByteArray::AddBuffer ( const UInt8 pun_buffer,
size_t  un_size 
)

Appends bytes to the byte array.

The contents of the buffer can be erased, since this method copies them.

Parameters
pun_bufferthe byte buffer to copy from.
un_sizethe size of the byte buffer.
Returns
a reference to this byte array.

Definition at line 105 of file byte_array.cpp.

void argos::CByteArray::Clear ( )
inline

Clears the byte array.

After calling this method, the byte array is empty.

See also
Empty()
Size()

Definition at line 134 of file byte_array.h.

bool argos::CByteArray::Empty ( ) const
inline

Returns true if the byte array is empty.

Returns
true if the byte array is empty.

Definition at line 100 of file byte_array.h.

CByteArray & argos::CByteArray::FetchBuffer ( UInt8 pun_buffer,
size_t  un_size 
)

Moves elements from the byte array into the passed buffer.

The elements whose values were written into the target buffer are removed from the byte array.

Parameters
pun_bufferthe byte buffer to write into.
un_sizethe size of the target byte buffer.
Returns
a reference to this byte array.

Definition at line 116 of file byte_array.cpp.

CByteArray * argos::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.

The returned array is a copy from the element at position un_start up to the element at position un_end-1.

Parameters
un_startThe start index
un_endThe end index, or -1 to indicate the end of the array

Definition at line 129 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( UInt8  un_value)

Appends a 8-bit unsigned integer to the byte array.

Parameters
un_valuethe value of the 8-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 139 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( SInt8  n_value)

Appends a 8-bit signed integer to the byte array.

Parameters
n_valuethe value of the 8-bit signed integer.
Returns
a reference to this byte array.

Definition at line 157 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( UInt16  un_value)

Appends a 16-bit unsigned integer to the byte array.

Parameters
un_valuethe value of the 16-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 175 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( SInt16  n_value)

Appends a 16-bit signed integer to the byte array.

Parameters
n_valuethe value of the 16-bit signed integer.
Returns
a reference to this byte array.

Definition at line 199 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( UInt32  un_value)

Appends a 32-bit unsigned integer to the byte array.

Parameters
un_valuethe value of the 32-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 223 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( SInt32  n_value)

Appends a 32-bit signed integer to the byte array.

Parameters
n_valuethe value of the 32-bit signed integer.
Returns
a reference to this byte array.

Definition at line 251 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( UInt64  un_value)

Appends a 64-bit unsigned integer to the byte array.

Parameters
un_valuethe value of the 64-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 279 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( SInt64  n_value)

Appends a 64-bit signed integer to the byte array.

Parameters
n_valuethe value of the 64-bit signed integer.
Returns
a reference to this byte array.

Definition at line 315 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( unsigned long int  un_value)

Appends an unsigned long integer to the byte array.

This function should be avoided as much as possible because the size of long integers changes on 32- and 64-bit architectures.

Parameters
un_valuethe value of the unsigned long integer.
Returns
a reference to this byte array.

Definition at line 351 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( signed long int  n_value)

Appends a signed long integer to the byte array.

This function should be avoided as much as possible because the size of long integers changes on 32- and 64-bit architectures.

Parameters
n_valuethe value of the signed long integer.
Returns
a reference to this byte array.

Definition at line 377 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( double  f_value)

Appends a double to the byte array.

This method assumes that floating-point numbers are represented in the IEEE754 standard.

Parameters
f_valuethe value of the double.
Returns
a reference to this byte array.

Definition at line 403 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( float  f_value)

Appends a float to the byte array.

This method assumes that floating-point numbers are represented in the IEEE754 standard.

Parameters
f_valuethe value of the float.
Returns
a reference to this byte array.

Definition at line 481 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator<< ( const std::string &  str_value)

Appends a std::string to the byte array.

Parameters
str_valuethe value of the std::string.
Returns
a reference to this byte array.

Definition at line 499 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator= ( const CByteArray c_byte_array)

Assignment operator.

Deep-copies the given byte array into the current byte array.

Definition at line 88 of file byte_array.cpp.

bool argos::CByteArray::operator== ( const CByteArray c_byte_array) const

Equality comparison operator.

Returns
true if the content of this array and the given one are identical.

Definition at line 98 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( UInt8 un_value)

Moves an 8-bit unsigned integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
un_valuethe buffer for the 8-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 147 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( SInt8 n_value)

Moves an 8-bit signed integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
n_valuethe buffer for the 8-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 165 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( UInt16 un_value)

Moves a 16-bit unsigned integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
un_valuethe buffer for the 16-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 186 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( SInt16 n_value)

Moves a 16-bit signed integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
n_valuethe buffer for the 16-bit signed integer.
Returns
a reference to this byte array.

Definition at line 210 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( UInt32 un_value)

Moves a 32-bit unsigned integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
un_valuethe buffer for the 32-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 236 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( SInt32 n_value)

Moves a 32-bit signed integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
n_valuethe buffer for the 32-bit signed integer.
Returns
a reference to this byte array.

Definition at line 264 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( UInt64 un_value)

Moves a 64-bit unsigned integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
un_valuethe buffer for the 64-bit unsigned integer.
Returns
a reference to this byte array.

Definition at line 296 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( SInt64 n_value)

Moves a 64-bit signed integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
n_valuethe buffer for the 64-bit signed integer.
Returns
a reference to this byte array.

Definition at line 332 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( unsigned long int &  un_value)

Moves an unsigned long integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array. This function should be avoided as much as possible because the size of long integers changes on 32- and 64-bit architectures.

Parameters
un_valuethe buffer for the unsigned long integer.
Returns
a reference to this byte array.

Definition at line 364 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( signed long int &  n_value)

Moves a signed long integer from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array. This function should be avoided as much as possible because the size of long integers changes on 32- and 64-bit architectures.

Parameters
n_valuethe buffer for the signed long integer.
Returns
a reference to this byte array.

Definition at line 390 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( double &  f_value)

Moves a double from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array. This method assumes that floating-point numbers are represented in the IEEE754 standard.

Parameters
f_valuethe buffer for the double.
Returns
a reference to this byte array.

Definition at line 444 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( float &  f_value)

Moves a float from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array. This method assumes that floating-point numbers are represented in the IEEE754 standard.

Parameters
f_valuethe buffer for the float.
Returns
a reference to this byte array.

Definition at line 489 of file byte_array.cpp.

CByteArray & argos::CByteArray::operator>> ( std::string &  str_value)

Moves a std::string from the beginning of the byte array to the target variable.

The element whose value was written into the target buffer are removed from the byte array.

Parameters
str_valuethe buffer for the std::string.
Returns
a reference to this byte array.

Definition at line 512 of file byte_array.cpp.

UInt8& argos::CByteArray::operator[] ( size_t  un_index)
inline

Read/write index operator.

Parameters
un_indexthe index of the wanted element.
Returns
a reference to the wanted element.
Exceptions
CARGoSExceptionif the passed index is out of bounds.

Definition at line 157 of file byte_array.h.

UInt8 argos::CByteArray::operator[] ( size_t  un_index) const
inline

Read-only index operator.

Parameters
un_indexthe index of the wanted element.
Returns
the value of the wanted element.
Exceptions
CARGoSExceptionif the passed index is out of bounds.

Definition at line 168 of file byte_array.h.

template<typename T >
T argos::CByteArray::PopFront ( )
inline

Removes data from the beginning of the byte array and returns it.

For example:

  CByteArray b;
  // ... fill into b ...
  UInt32 i = PopFront<UInt32>();
Returns
the removed data.

Definition at line 209 of file byte_array.h.

void argos::CByteArray::Resize ( size_t  un_size,
UInt8  un_value = 0 
)
inline

Resizes the byte array to the wanted size.

If the new size is smaller than the old one, the first un_size elements are kept, and the extra ones are erased. If the new size is greater than the old one, new elements are added to the byte array and initialized with un_value. This operation could entail a reallocation of the internal storage structure, which would invalide the pointer returned by ToCArray().

Parameters
un_sizeThe new size.
un_valueThe init value for the padding elements.
See also
ToCArray()

Definition at line 83 of file byte_array.h.

size_t argos::CByteArray::Size ( ) const
inline

Returns the current size of the byte array.

Returns
the current size of the byte array.

Definition at line 66 of file byte_array.h.

void argos::CByteArray::Swap ( CByteArray c_other)
inline

Swaps the content of this byte array with the content of the passed one.

Parameters
c_otherThe byte array to swap content with.

Definition at line 92 of file byte_array.h.

const UInt8* argos::CByteArray::ToCArray ( ) const
inline

Returns the contents of the byte array as a const c-style array.

The returned pointer is valid under two conditions: (i) the CByteArray object has not been deallocated, and (ii) the contents of the byte array have not been changed. If the byte array is empty, this method returns NULL.

Returns
the contents of the byte array as a const c-style array.

Definition at line 112 of file byte_array.h.

UInt8* argos::CByteArray::ToCArray ( )
inline

Returns the contents of the byte array as a c-style array.

The returned pointer is valid under two conditions: (i) the CByteArray object has not been deallocated, and (ii) the contents of the byte array have not been changed. If the byte array is empty, this method returns NULL.

Returns
the contents of the byte array as a c-style array.

Definition at line 124 of file byte_array.h.

void argos::CByteArray::Zero ( )

Sets the contents of the byte array to all zeros.

This method does not change the size of the byte array.

See also
Size()

Definition at line 81 of file byte_array.cpp.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  c_os,
const CByteArray c_byte_array 
)
friend

Stream operator.

It streams the byte array through the given stream. Useful for files, for instance.

Parameters
c_osthe C++ output stream the byte array to.
c_byte_arraythe byte array to stream.
Returns
the new state of the output stream.

Definition at line 530 of file byte_array.cpp.