9 #include <argos3/core/utility/math/general.h>
11 #include <arpa/inet.h>
21 static SInt64 MAX_MANTISSA = 9223372036854775806LL;
29 static const SInt32 nTest = 42;
31 if(*
reinterpret_cast<const UInt8*
>(&nTest) == nTest) {
33 const UInt32 unHighWord = htonl(
static_cast<UInt32>(un_value >> 32));
34 const UInt32 unLowWord = htonl(
static_cast<UInt32>(un_value & 0xFFFFFFFFLL));
35 return static_cast<UInt64>(unLowWord) << 32 | unHighWord;
47 static const SInt32 nTest = 42;
49 if(*
reinterpret_cast<const UInt8*
>(&nTest) == nTest) {
51 const UInt32 unHighWord = ntohl(
static_cast<UInt32>(un_value >> 32));
52 const UInt32 unLowWord = ntohl(
static_cast<UInt32>(un_value & 0xFFFFFFFFLL));
53 return static_cast<UInt64>(unLowWord) << 32 | unHighWord;
75 m_vecBuffer.assign(un_size, un_value);
82 ::memset(&m_vecBuffer[0], 0,
sizeof(
UInt8) *
Size());
89 if(
this != &c_byte_array) {
90 m_vecBuffer = c_byte_array.m_vecBuffer;
99 return m_vecBuffer == c_byte_array.m_vecBuffer;
107 for(
size_t i = 0; i < un_size; ++i) {
108 m_vecBuffer.push_back(pun_buffer[i]);
118 if(
Size() < un_size)
THROW_ARGOSEXCEPTION(
"Attempting to extract too many bytes from byte array (" << un_size <<
" requested, " <<
Size() <<
" available)");
119 for(
size_t i = 0; i < un_size; ++i) {
120 *(pun_buffer+i) = m_vecBuffer[i];
122 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + un_size);
131 if(un_start >=
Size())
THROW_ARGOSEXCEPTION(
"Attempting to extract from byte array beyond the limits (" << un_start <<
" requested, " <<
Size() <<
" size)");
132 un_end = un_end < 0 ? Size() : Min<ssize_t>(un_end,
Size());
140 m_vecBuffer.push_back(un_value);
149 un_value = m_vecBuffer.front();
150 m_vecBuffer.erase(m_vecBuffer.begin());
158 m_vecBuffer.push_back(n_value);
167 n_value = m_vecBuffer.front();
168 m_vecBuffer.erase(m_vecBuffer.begin());
176 un_value = htons(un_value);
177 auto* punByte =
reinterpret_cast<UInt8*
>(&un_value);
178 m_vecBuffer.push_back(punByte[0]);
179 m_vecBuffer.push_back(punByte[1]);
188 auto* punByte =
reinterpret_cast<UInt8*
>(&un_value);
189 punByte[0] = m_vecBuffer[0];
190 punByte[1] = m_vecBuffer[1];
191 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + 2);
192 un_value = ntohs(un_value);
200 n_value = htons(n_value);
201 auto* punByte =
reinterpret_cast<UInt8*
>(&n_value);
202 m_vecBuffer.push_back(punByte[0]);
203 m_vecBuffer.push_back(punByte[1]);
212 auto* punByte =
reinterpret_cast<UInt8*
>(&n_value);
213 punByte[0] = m_vecBuffer[0];
214 punByte[1] = m_vecBuffer[1];
215 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + 2);
216 n_value = ntohs(n_value);
224 un_value = htonl(un_value);
225 auto* punByte =
reinterpret_cast<UInt8*
>(&un_value);
226 m_vecBuffer.push_back(punByte[0]);
227 m_vecBuffer.push_back(punByte[1]);
228 m_vecBuffer.push_back(punByte[2]);
229 m_vecBuffer.push_back(punByte[3]);
238 auto* punByte =
reinterpret_cast<UInt8*
>(&un_value);
239 punByte[0] = m_vecBuffer[0];
240 punByte[1] = m_vecBuffer[1];
241 punByte[2] = m_vecBuffer[2];
242 punByte[3] = m_vecBuffer[3];
243 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + 4);
244 un_value = ntohl(un_value);
252 n_value = htonl(n_value);
253 auto* punByte =
reinterpret_cast<UInt8*
>(&n_value);
254 m_vecBuffer.push_back(punByte[0]);
255 m_vecBuffer.push_back(punByte[1]);
256 m_vecBuffer.push_back(punByte[2]);
257 m_vecBuffer.push_back(punByte[3]);
266 auto* punByte =
reinterpret_cast<UInt8*
>(&n_value);
267 punByte[0] = m_vecBuffer[0];
268 punByte[1] = m_vecBuffer[1];
269 punByte[2] = m_vecBuffer[2];
270 punByte[3] = m_vecBuffer[3];
271 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + 4);
272 n_value = ntohl(n_value);
280 un_value = htonll(un_value);
281 auto* punByte =
reinterpret_cast<UInt8*
>(&un_value);
282 m_vecBuffer.push_back(punByte[0]);
283 m_vecBuffer.push_back(punByte[1]);
284 m_vecBuffer.push_back(punByte[2]);
285 m_vecBuffer.push_back(punByte[3]);
286 m_vecBuffer.push_back(punByte[4]);
287 m_vecBuffer.push_back(punByte[5]);
288 m_vecBuffer.push_back(punByte[6]);
289 m_vecBuffer.push_back(punByte[7]);
298 auto* punByte =
reinterpret_cast<UInt8*
>(&un_value);
299 punByte[0] = m_vecBuffer[0];
300 punByte[1] = m_vecBuffer[1];
301 punByte[2] = m_vecBuffer[2];
302 punByte[3] = m_vecBuffer[3];
303 punByte[4] = m_vecBuffer[4];
304 punByte[5] = m_vecBuffer[5];
305 punByte[6] = m_vecBuffer[6];
306 punByte[7] = m_vecBuffer[7];
307 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + 8);
308 un_value = ntohll(un_value);
316 n_value = htonll(n_value);
317 auto* punByte =
reinterpret_cast<UInt8*
>(&n_value);
318 m_vecBuffer.push_back(punByte[0]);
319 m_vecBuffer.push_back(punByte[1]);
320 m_vecBuffer.push_back(punByte[2]);
321 m_vecBuffer.push_back(punByte[3]);
322 m_vecBuffer.push_back(punByte[4]);
323 m_vecBuffer.push_back(punByte[5]);
324 m_vecBuffer.push_back(punByte[6]);
325 m_vecBuffer.push_back(punByte[7]);
334 auto* punByte =
reinterpret_cast<UInt8*
>(&n_value);
335 punByte[0] = m_vecBuffer[0];
336 punByte[1] = m_vecBuffer[1];
337 punByte[2] = m_vecBuffer[2];
338 punByte[3] = m_vecBuffer[3];
339 punByte[4] = m_vecBuffer[4];
340 punByte[5] = m_vecBuffer[5];
341 punByte[6] = m_vecBuffer[6];
342 punByte[7] = m_vecBuffer[7];
343 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + 8);
344 n_value = ntohll(n_value);
352 if(
sizeof(un_value) ==
sizeof(
UInt32)) {
353 *this << static_cast<UInt32>(un_value);
355 else if(
sizeof(un_value) ==
sizeof(
UInt64)) {
356 *this << static_cast<UInt64>(un_value);
365 if(
sizeof(un_value) ==
sizeof(
UInt32)) {
366 *
this >> *
reinterpret_cast<UInt32*
>(&un_value);
368 else if(
sizeof(un_value) ==
sizeof(
UInt64)) {
369 *
this >> *
reinterpret_cast<UInt64*
>(&un_value);
378 if(
sizeof(n_value) ==
sizeof(
SInt32)) {
379 *this << static_cast<SInt32>(n_value);
381 else if(
sizeof(n_value) ==
sizeof(
SInt64)) {
382 *this << static_cast<SInt64>(n_value);
391 if(
sizeof(n_value) ==
sizeof(
SInt32)) {
392 *
this >> *
reinterpret_cast<SInt32*
>(&n_value);
394 else if(
sizeof(n_value) ==
sizeof(
SInt64)) {
395 *
this >> *
reinterpret_cast<SInt64*
>(&n_value);
414 double fShiftedSignificand =
Abs(frexp(f_value, &nExponent)) - 0.5;
416 if(fShiftedSignificand < 0.0) {
429 nMantissa =
static_cast<SInt64>(fShiftedSignificand * 2.0 * (MAX_MANTISSA)) + 1;
432 nMantissa = -nMantissa;
467 double fSignificand = (
static_cast<double>(llabs(nMantissa) - 1) / MAX_MANTISSA) / 2.0 + 0.5;
469 f_value = ldexp(fSignificand, nExponent);
471 if(nMantissa < 0.0) {
482 *this << static_cast<double>(f_value);
491 *
this >> fDoubleValue;
492 f_value =
static_cast<float>(fDoubleValue);
501 for(
size_t i = 0; i < str_value.size(); ++i) {
502 *this << static_cast<UInt8>(str_value[i]);
505 *this << static_cast<UInt8>(0);
516 while(i <
Size() && m_vecBuffer[i] !=
'\0') {
517 str_value += m_vecBuffer[i];
520 if(m_vecBuffer[i] ==
'\0') {
523 m_vecBuffer.erase(m_vecBuffer.begin(), m_vecBuffer.begin() + i);
531 c_os <<
"CByteArray [";
532 for(
size_t i = 0; i < c_byte_array.
Size(); ++i) {
533 c_os <<
" " << c_byte_array.m_vecBuffer[i];
535 c_os <<
" ]" << std::endl;
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
signed int SInt32
32-bit signed integer.
unsigned int UInt32
32-bit unsigned integer.
signed short SInt16
16-bit signed integer.
unsigned char UInt8
8-bit unsigned integer.
signed long long SInt64
64-bit signed integer.
unsigned long long UInt64
64-bit unsigned integer.
unsigned short UInt16
16-bit unsigned integer.
signed char SInt8
8-bit signed integer.
The namespace containing all the ARGoS related code.
std::ostream & operator<<(std::ostream &c_os, const CByteArray &c_byte_array)
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Byte array utility class.
void Zero()
Sets the contents of the byte array to all zeros.
CByteArray & operator=(const CByteArray &c_byte_array)
Assignment operator.
size_t Size() const
Returns the current size of the byte array.
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.
const UInt8 * ToCArray() const
Returns the contents of the byte array as a const c-style array.
bool Empty() const
Returns true if the byte array is empty.
CByteArray & operator<<(UInt8 un_value)
Appends a 8-bit unsigned integer to the byte array.
CByteArray()
Class constructor.
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.
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.