5 #ifndef UAVCAN_MARSHAL_SCALAR_CODEC_HPP_INCLUDED
6 #define UAVCAN_MARSHAL_SCALAR_CODEC_HPP_INCLUDED
24 static void swapByteOrder(
uint8_t* bytes,
unsigned len);
26 template <
unsigned BitLen,
unsigned Size>
27 static typename EnableIf<(BitLen > 8)>::Type
30 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN)
31 static const bool big_endian = BYTE_ORDER == BIG_ENDIAN;
33 union {
long int l;
char c[
sizeof(
long int)]; } u;
35 const bool big_endian = u.c[
sizeof(
long int) - 1] == 1;
39 swapByteOrder(bytes, Size);
43 template <
unsigned BitLen,
unsigned Size>
44 static typename EnableIf<(BitLen <= 8)>::Type
47 template <
unsigned BitLen,
typename T>
51 StaticAssert<NumericTraits<T>::IsInteger>::check();
52 if (value & (T(1) << (BitLen - 1)))
54 value |= T(T(0xFFFFFFFFFFFFFFFFULL) & ~((T(1) << BitLen) - 1));
58 template <
unsigned BitLen,
typename T>
62 template <
unsigned BitLen,
typename T>
63 static typename EnableIf<((
sizeof(T) * 8) > BitLen)>::Type
66 value &= (T(1) << BitLen) - 1;
69 template <
unsigned BitLen,
typename T>
70 static typename EnableIf<((
sizeof(T) * 8) == BitLen)>::Type
73 template <
unsigned BitLen,
typename T>
77 StaticAssert<(BitLen <= BitStream::MaxBitsPerRW)>::check();
78 StaticAssert<static_cast<bool>(NumericTraits<T>::IsSigned) ? (BitLen > 1) :
true>::
check();
81 int encodeBytesImpl(
uint8_t* bytes,
unsigned bitlen);
82 int decodeBytesImpl(
uint8_t* bytes,
unsigned bitlen);
89 template <
unsigned BitLen,
typename T>
90 int encode(
const T value);
92 template <
unsigned BitLen,
typename T>
98 template <
unsigned BitLen,
typename T>
101 validate<BitLen, T>();
107 byte_union.value = value;
108 clearExtraBits<BitLen, T>(byte_union.value);
109 convertByteOrder<BitLen>(byte_union.bytes);
113 template <
unsigned BitLen,
typename T>
116 validate<BitLen, T>();
122 byte_union.value = T();
126 convertByteOrder<BitLen>(byte_union.bytes);
127 fixTwosComplement<BitLen, T>(byte_union.value);
128 value = byte_union.value;
135 #endif // UAVCAN_MARSHAL_SCALAR_CODEC_HPP_INCLUDED