Merge remote-tracking branch 'upstream/mohr_serialize' into mueller_merge_mohr_serialize
This commit is contained in:
@ -5,13 +5,9 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* @brief Can be used to swap endianness of data
|
||||
* into big endian
|
||||
*/
|
||||
class EndianSwapper {
|
||||
class EndianConverter {
|
||||
private:
|
||||
EndianSwapper() {};
|
||||
EndianConverter() {};
|
||||
public:
|
||||
/**
|
||||
* Swap the endianness of a variable with arbitrary type
|
||||
@ -20,13 +16,13 @@ public:
|
||||
* @return Variable with swapped endianness
|
||||
*/
|
||||
template<typename T>
|
||||
static T swap(T in) {
|
||||
static T convertBigEndian(T in) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t *) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t *) ∈
|
||||
uint8_t *pointerOut = (uint8_t*) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t*) ∈
|
||||
for (uint8_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
@ -38,13 +34,8 @@ public:
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap the endianness of a buffer.
|
||||
* @param out
|
||||
* @param in
|
||||
* @param size
|
||||
*/
|
||||
static void swap(uint8_t* out, const uint8_t* in, size_t size) {
|
||||
static void convertBigEndian(uint8_t *out, const uint8_t *in,
|
||||
uint32_t size) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
@ -58,30 +49,36 @@ public:
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap endianness of buffer entries
|
||||
* Template argument specifies buffer type. The number of entries
|
||||
* (not the buffer size!) must be supplied
|
||||
* @param out
|
||||
* @param in
|
||||
* @param size Number of buffer entries (not size of buffer in bytes!)
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
static void swap(T * out, const T * in, uint32_t entries) {
|
||||
static T convertLittleEndian(T in) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
const uint8_t * in_buffer = reinterpret_cast<const uint8_t *>(in);
|
||||
uint8_t * out_buffer = reinterpret_cast<uint8_t *>(out);
|
||||
for (uint8_t count = 0; count < entries; count++) {
|
||||
for(uint8_t i = 0; i < sizeof(T);i++) {
|
||||
out_buffer[sizeof(T)* (count + 1) - i - 1] =
|
||||
in_buffer[count * sizeof(T) + i];
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t *) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t *) ∈
|
||||
for (uint8_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
memcpy(out, in, size*sizeof(T));
|
||||
return tmp;
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
return in;
|
||||
#else
|
||||
#error Unknown Byte Order
|
||||
#endif
|
||||
}
|
||||
static void convertLittleEndian(uint8_t *out, const uint8_t *in,
|
||||
uint32_t size) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
for (uint8_t count = 0; count < size; count++) {
|
||||
out[size - count - 1] = in[count];
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
memcpy(out, in, size);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user