3 #include "../platform.hpp"
19 #ifdef MICROSTRAIN_USE_STD_ENDIAN
25 namespace serialization
27 using Endian = std::endian;
31 #else // MICROSTRAIN_USE_STD_ENDIAN
35 namespace serialization
48 #endif // MICROSTRAIN_USE_STD_ENDIAN
52 namespace serialization
63 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T) == 1,
size_t>::type
66 buffer[0] =
reinterpret_cast<const uint8_t *
>(&value)[0];
71 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T) == 2,
size_t>::type
74 buffer[0] =
reinterpret_cast<const uint8_t *
>(&value)[1];
75 buffer[1] =
reinterpret_cast<const uint8_t *
>(&value)[0];
80 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T) == 4,
size_t>::type
83 buffer[0] =
reinterpret_cast<const uint8_t *
>(&value)[3];
84 buffer[1] =
reinterpret_cast<const uint8_t *
>(&value)[2];
85 buffer[2] =
reinterpret_cast<const uint8_t *
>(&value)[1];
86 buffer[3] =
reinterpret_cast<const uint8_t *
>(&value)[0];
91 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T) == 8,
size_t>::type
94 buffer[0] =
reinterpret_cast<const uint8_t *
>(&value)[7];
95 buffer[1] =
reinterpret_cast<const uint8_t *
>(&value)[6];
96 buffer[2] =
reinterpret_cast<const uint8_t *
>(&value)[5];
97 buffer[3] =
reinterpret_cast<const uint8_t *
>(&value)[4];
98 buffer[4] =
reinterpret_cast<const uint8_t *
>(&value)[3];
99 buffer[5] =
reinterpret_cast<const uint8_t *
>(&value)[2];
100 buffer[6] =
reinterpret_cast<const uint8_t *
>(&value)[1];
101 buffer[7] =
reinterpret_cast<const uint8_t *
>(&value)[0];
106 namespace little_endian
110 typename std::enable_if<std::is_arithmetic<T>::value,
size_t>::type
113 std::memcpy(buffer, &value,
sizeof(value));
114 return sizeof(value);
120 template<Endian E,
class T>
121 typename std::enable_if<std::is_arithmetic<T>::value,
size_t>::type
133 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T)==1,
size_t>::type
134 read(
const uint8_t* buffer, T& value)
136 reinterpret_cast<uint8_t*
>(&value)[0] = buffer[0];
141 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T)==2,
size_t>::type
142 read(
const uint8_t* buffer, T& value)
144 reinterpret_cast<uint8_t*
>(&value)[0] = buffer[1];
145 reinterpret_cast<uint8_t*
>(&value)[1] = buffer[0];
150 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T)==4,
size_t>::type
151 read(
const uint8_t* buffer, T& value)
153 reinterpret_cast<uint8_t*
>(&value)[0] = buffer[3];
154 reinterpret_cast<uint8_t*
>(&value)[1] = buffer[2];
155 reinterpret_cast<uint8_t*
>(&value)[2] = buffer[1];
156 reinterpret_cast<uint8_t*
>(&value)[3] = buffer[0];
161 typename std::enable_if<std::is_arithmetic<T>::value &&
sizeof(T)==8,
size_t>::type
162 read(
const uint8_t* buffer, T& value)
164 reinterpret_cast<uint8_t*
>(&value)[0] = buffer[7];
165 reinterpret_cast<uint8_t*
>(&value)[1] = buffer[6];
166 reinterpret_cast<uint8_t*
>(&value)[2] = buffer[5];
167 reinterpret_cast<uint8_t*
>(&value)[3] = buffer[4];
168 reinterpret_cast<uint8_t*
>(&value)[4] = buffer[3];
169 reinterpret_cast<uint8_t*
>(&value)[5] = buffer[2];
170 reinterpret_cast<uint8_t*
>(&value)[6] = buffer[1];
171 reinterpret_cast<uint8_t*
>(&value)[7] = buffer[0];
176 namespace little_endian
180 typename std::enable_if<std::is_arithmetic<T>::value,
size_t>::type
181 read(
const uint8_t* buffer, T& value)
183 std::memcpy(&value, buffer,
sizeof(T));
190 template<Endian E,
class T>
191 size_t read(
const uint8_t* buffer, T& value)
197 template<Endian E,
class T>
201 read<E,T>(buffer, value);