5 #ifndef UAVCAN_MARSHAL_INTEGER_SPEC_HPP_INCLUDED 6 #define UAVCAN_MARSHAL_INTEGER_SPEC_HPP_INCLUDED 23 template <
unsigned BitLen_, Signedness Signedness, CastMode CastMode>
26 struct ErrorNoSuchInteger;
30 enum { BitLen = BitLen_ };
31 enum { MinBitLen = BitLen };
32 enum { MaxBitLen = BitLen };
33 enum { IsPrimitive = 1 };
35 typedef typename Select<(BitLen <= 8), typename Select<IsSigned, int8_t, uint8_t>::Result,
36 typename Select<(BitLen <= 16), typename Select<IsSigned, int16_t, uint16_t>::Result,
37 typename Select<(BitLen <= 32), typename Select<IsSigned, int32_t, uint32_t>::Result,
38 typename Select<(BitLen <= 64), typename Select<IsSigned, int64_t, uint64_t>::Result,
39 ErrorNoSuchInteger>::Result>::Result>::Result>::Result
StorageType;
48 static StorageType
max()
50 StaticAssert<(sizeof(uintmax_t) >= 8)>::
check();
53 return StorageType((uintmax_t(1) << static_cast<unsigned>(BitLen)) - 1U);
57 return StorageType((uintmax_t(1) << (static_cast<unsigned>(BitLen) - 1U)) - 1);
60 static UnsignedStorageType
mask()
62 StaticAssert<(sizeof(uintmax_t) >= 8U)>::
check();
63 return UnsignedStorageType((uintmax_t(1) << static_cast<unsigned>(BitLen)) - 1U);
69 static StorageType
max()
71 return StorageType((IsSigned == 0) ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFLL);
73 static UnsignedStorageType
mask() {
return 0xFFFFFFFFFFFFFFFFULL; }
76 typedef typename Select<(BitLen == 64), LimitsImpl64, LimitsImplGeneric>::Result
Limits;
84 else if (value <=
min())
94 static void truncate(StorageType& value) { value = value & StorageType(mask()); }
98 StaticAssert<(BitLen <= (sizeof(StorageType) * 8))>::check();
107 static StorageType
min() {
return IsSigned ? StorageType(-
max() - 1) : 0; }
108 static UnsignedStorageType
mask() {
return Limits::mask(); }
122 return codec.
encode<BitLen>(value);
128 return codec.
decode<BitLen>(out_value);
137 template <CastMode CastMode>
141 enum { IsSigned = 0 };
143 enum { MinBitLen = 1 };
144 enum { MaxBitLen = 1 };
145 enum { IsPrimitive = 1 };
154 static StorageType
max() {
return true; }
155 static StorageType
min() {
return false; }
156 static UnsignedStorageType
mask() {
return true; }
160 return codec.
encode<BitLen>(value);
165 return codec.
decode<BitLen>(out_value);
171 template <CastMode CastMode>
174 template <Signedness Signedness, CastMode CastMode>
178 template <
typename T>
184 template <
unsigned BitLen, Signedness Signedness, CastMode CastMode>
191 template <
unsigned BitLen, Signedness Signedness, CastMode CastMode>
198 template <
typename Stream>
199 static void stream(Stream&
s,
const StorageType value,
int)
202 typedef typename Select<(sizeof(StorageType) >=
sizeof(
int)), StorageType,
203 typename Select<RawType::IsSigned, int, unsigned>::Result >::Result TempType;
204 s << TempType(value);
210 #endif // UAVCAN_MARSHAL_INTEGER_SPEC_HPP_INCLUDED static UnsignedStorageType mask()
Select<(BitLen<=8), typename Select< IsSigned, int8_t, uint8_t >::Result, typename Select<(BitLen<=16), typename Select< IsSigned, int16_t, uint16_t >::Result, typename Select<(BitLen<=32), typename Select< IsSigned, int32_t, uint32_t >::Result, typename Select<(BitLen<=64), typename Select< IsSigned, int64_t, uint64_t >::Result, ErrorNoSuchInteger >::Result >::Result >::Result >::Result StorageType
static int decode(StorageType &out_value, ScalarCodec &codec, TailArrayOptimizationMode)
class UAVCAN_EXPORT YamlStreamer
ROSCPP_DECL bool validate(const std::string &name, std::string &error)
static void extendDataTypeSignature(DataTypeSignature &)
IntegerSpec< BitLen, SignednessUnsigned, CastMode >::StorageType UnsignedStorageType
Select<(BitLen==64), LimitsImpl64, LimitsImplGeneric >::Result Limits
static UnsignedStorageType mask()
IntegerSpec< BitLen, Signedness, CastMode > RawType
static UnsignedStorageType mask()
UAVCAN_EXPORT const T & max(const T &a, const T &b)
static void truncate(StorageType &value)
int encode(const T value)
static void stream(Stream &s, const StorageType value, int)
TailArrayOptimizationMode
UAVCAN_EXPORT const T & min(const T &a, const T &b)
static int encode(StorageType value, ScalarCodec &codec, TailArrayOptimizationMode)
static UnsignedStorageType mask()
static void saturate(StorageType &value)
RawType::StorageType StorageType
static int decode(StorageType &out_value, ScalarCodec &codec, TailArrayOptimizationMode)
static int encode(StorageType value, ScalarCodec &codec, TailArrayOptimizationMode)
static void extendDataTypeSignature(DataTypeSignature &)