integer_spec.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_MARSHAL_INTEGER_SPEC_HPP_INCLUDED
6 #define UAVCAN_MARSHAL_INTEGER_SPEC_HPP_INCLUDED
7 
8 #include <uavcan/std.hpp>
9 #include <uavcan/data_type.hpp>
13 
14 namespace uavcan
15 {
16 
18 
23 template <unsigned BitLen_, Signedness Signedness, CastMode CastMode>
25 {
26  struct ErrorNoSuchInteger;
27 
28 public:
29  enum { IsSigned = Signedness == SignednessSigned };
30  enum { BitLen = BitLen_ };
31  enum { MinBitLen = BitLen };
32  enum { MaxBitLen = BitLen };
33  enum { IsPrimitive = 1 };
34 
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;
40 
42 
43 private:
44  IntegerSpec();
45 
47  {
48  static StorageType max()
49  {
50  StaticAssert<(sizeof(uintmax_t) >= 8)>::check();
51  if (IsSigned == 0)
52  {
53  return StorageType((uintmax_t(1) << static_cast<unsigned>(BitLen)) - 1U);
54  }
55  else
56  {
57  return StorageType((uintmax_t(1) << (static_cast<unsigned>(BitLen) - 1U)) - 1);
58  }
59  }
61  {
62  StaticAssert<(sizeof(uintmax_t) >= 8U)>::check();
63  return UnsignedStorageType((uintmax_t(1) << static_cast<unsigned>(BitLen)) - 1U);
64  }
65  };
66 
67  struct LimitsImpl64
68  {
69  static StorageType max()
70  {
71  return StorageType((IsSigned == 0) ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFLL);
72  }
73  static UnsignedStorageType mask() { return 0xFFFFFFFFFFFFFFFFULL; }
74  };
75 
76  typedef typename Select<(BitLen == 64), LimitsImpl64, LimitsImplGeneric>::Result Limits;
77 
78  static void saturate(StorageType& value)
79  {
80  if (value > max())
81  {
82  value = max();
83  }
84  else if (value <= min()) // 'Less or Equal' allows to suppress compiler warning on unsigned types
85  {
86  value = min();
87  }
88  else
89  {
90  ; // Valid range
91  }
92  }
93 
94  static void truncate(StorageType& value) { value = value & StorageType(mask()); }
95 
96  static void validate()
97  {
98  StaticAssert<(BitLen <= (sizeof(StorageType) * 8))>::check();
99  // coverity[result_independent_of_operands : FALSE]
101  // coverity[result_independent_of_operands : FALSE]
103  }
104 
105 public:
106  static StorageType max() { return Limits::max(); }
107  static StorageType min() { return IsSigned ? StorageType(-max() - 1) : 0; }
108  static UnsignedStorageType mask() { return Limits::mask(); }
109 
111  {
112  validate();
113  // cppcheck-suppress duplicateExpression
114  if (CastMode == CastModeSaturate)
115  {
116  saturate(value);
117  }
118  else
119  {
120  truncate(value);
121  }
122  return codec.encode<BitLen>(value);
123  }
124 
125  static int decode(StorageType& out_value, ScalarCodec& codec, TailArrayOptimizationMode)
126  {
127  validate();
128  return codec.decode<BitLen>(out_value);
129  }
130 
132 };
133 
137 template <CastMode CastMode>
139 {
140 public:
141  enum { IsSigned = 0 };
142  enum { BitLen = 1 };
143  enum { MinBitLen = 1 };
144  enum { MaxBitLen = 1 };
145  enum { IsPrimitive = 1 };
146 
147  typedef bool StorageType;
148  typedef bool UnsignedStorageType;
149 
150 private:
151  IntegerSpec();
152 
153 public:
154  static StorageType max() { return true; }
155  static StorageType min() { return false; }
156  static UnsignedStorageType mask() { return true; }
157 
159  {
160  return codec.encode<BitLen>(value);
161  }
162 
163  static int decode(StorageType& out_value, ScalarCodec& codec, TailArrayOptimizationMode)
164  {
165  return codec.decode<BitLen>(out_value);
166  }
167 
169 };
170 
171 template <CastMode CastMode>
172 class IntegerSpec<1, SignednessSigned, CastMode>; // Invalid instantiation
173 
174 template <Signedness Signedness, CastMode CastMode>
175 class IntegerSpec<0, Signedness, CastMode>; // Invalid instantiation
176 
177 
178 template <typename T>
180 {
181  enum { Result = 0 };
182 };
183 
184 template <unsigned BitLen, Signedness Signedness, CastMode CastMode>
186 {
187  enum { Result = 1 };
188 };
189 
190 
191 template <unsigned BitLen, Signedness Signedness, CastMode CastMode>
193 {
196 
197 public:
198  template <typename Stream> // cppcheck-suppress passedByValue
199  static void stream(Stream& s, const StorageType value, int)
200  {
201  // Get rid of character types - we want its integer representation, not ASCII code
202  typedef typename Select<(sizeof(StorageType) >= sizeof(int)), StorageType,
203  typename Select<RawType::IsSigned, int, unsigned>::Result >::Result TempType;
204  s << TempType(value);
205  }
206 };
207 
208 }
209 
210 #endif // UAVCAN_MARSHAL_INTEGER_SPEC_HPP_INCLUDED
data_type.hpp
check
ROSCPP_DECL bool check()
uavcan::TailArrayOptimizationMode
TailArrayOptimizationMode
Definition: type_util.hpp:22
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::min
static StorageType min()
Definition: integer_spec.hpp:155
uavcan::IntegerSpec::LimitsImplGeneric::max
static StorageType max()
Definition: integer_spec.hpp:48
templates.hpp
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::StorageType
bool StorageType
Definition: integer_spec.hpp:147
uavcan::YamlStreamer< IntegerSpec< BitLen, Signedness, CastMode > >::RawType
IntegerSpec< BitLen, Signedness, CastMode > RawType
Definition: integer_spec.hpp:194
uavcan::IntegerSpec::mask
static UnsignedStorageType mask()
Definition: integer_spec.hpp:108
uavcan::ScalarCodec::decode
int decode(T &value)
Definition: scalar_codec.hpp:114
uavcan::IsIntegerSpec::Result
@ Result
Definition: integer_spec.hpp:181
uavcan::IntegerSpec::truncate
static void truncate(StorageType &value)
Definition: integer_spec.hpp:94
uavcan::CastMode
CastMode
Definition: type_util.hpp:17
uavcan::IntegerSpec::encode
static int encode(StorageType value, ScalarCodec &codec, TailArrayOptimizationMode)
Definition: integer_spec.hpp:110
uavcan::SignednessSigned
@ SignednessSigned
Definition: integer_spec.hpp:17
uavcan::IntegerSpec::max
static StorageType max()
Definition: integer_spec.hpp:106
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::encode
static int encode(StorageType value, ScalarCodec &codec, TailArrayOptimizationMode)
Definition: integer_spec.hpp:158
uavcan::IntegerSpec::StorageType
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
Definition: integer_spec.hpp:39
uavcan::StaticAssert
struct UAVCAN_EXPORT StaticAssert
Definition: templates.hpp:29
std.hpp
uavcan::IntegerSpec::LimitsImpl64::max
static StorageType max()
Definition: integer_spec.hpp:69
validate
ROSCPP_DECL bool validate(const std::string &name, std::string &error)
type_util.hpp
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::mask
static UnsignedStorageType mask()
Definition: integer_spec.hpp:156
uavcan::IntegerSpec::extendDataTypeSignature
static void extendDataTypeSignature(DataTypeSignature &)
Definition: integer_spec.hpp:131
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::UnsignedStorageType
bool UnsignedStorageType
Definition: integer_spec.hpp:148
uavcan::max
const UAVCAN_EXPORT T & max(const T &a, const T &b)
Definition: templates.hpp:291
uavcan::Signedness
Signedness
Definition: integer_spec.hpp:17
uavcan::Select
struct UAVCAN_EXPORT Select
Definition: templates.hpp:80
uavcan::IntegerSpec::min
static StorageType min()
Definition: integer_spec.hpp:107
uavcan::IntegerSpec
Definition: integer_spec.hpp:24
uavcan::SignednessUnsigned
@ SignednessUnsigned
Definition: integer_spec.hpp:17
scalar_codec.hpp
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
uavcan::min
const UAVCAN_EXPORT T & min(const T &a, const T &b)
Definition: templates.hpp:281
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::max
static StorageType max()
Definition: integer_spec.hpp:154
uavcan::IntegerSpec::LimitsImplGeneric::mask
static UnsignedStorageType mask()
Definition: integer_spec.hpp:60
uavcan::IsIntegerSpec
Definition: integer_spec.hpp:179
uavcan::IntegerSpec::decode
static int decode(StorageType &out_value, ScalarCodec &codec, TailArrayOptimizationMode)
Definition: integer_spec.hpp:125
uavcan::YamlStreamer< IntegerSpec< BitLen, Signedness, CastMode > >::stream
static void stream(Stream &s, const StorageType value, int)
Definition: integer_spec.hpp:199
uavcan::IntegerSpec::Limits
Select<(BitLen==64), LimitsImpl64, LimitsImplGeneric >::Result Limits
Definition: integer_spec.hpp:76
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::decode
static int decode(StorageType &out_value, ScalarCodec &codec, TailArrayOptimizationMode)
Definition: integer_spec.hpp:163
uavcan::IntegerSpec::LimitsImplGeneric
Definition: integer_spec.hpp:46
uavcan::YamlStreamer
class UAVCAN_EXPORT YamlStreamer
Definition: type_util.hpp:84
uavcan::IntegerSpec::saturate
static void saturate(StorageType &value)
Definition: integer_spec.hpp:78
pyuavcan_v0.dsdl.signature.s
s
Definition: signature.py:73
uavcan::IntegerSpec::UnsignedStorageType
IntegerSpec< BitLen, SignednessUnsigned, CastMode >::StorageType UnsignedStorageType
Definition: integer_spec.hpp:41
uavcan::ScalarCodec::encode
int encode(const T value)
Definition: scalar_codec.hpp:99
uavcan::YamlStreamer< IntegerSpec< BitLen, Signedness, CastMode > >::StorageType
RawType::StorageType StorageType
Definition: integer_spec.hpp:195
uavcan::IntegerSpec::validate
static void validate()
Definition: integer_spec.hpp:96
uavcan::DataTypeSignature
Definition: data_type.hpp:107
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::IntegerSpec::LimitsImpl64::mask
static UnsignedStorageType mask()
Definition: integer_spec.hpp:73
uavcan::IntegerSpec< 1, SignednessUnsigned, CastMode >::extendDataTypeSignature
static void extendDataTypeSignature(DataTypeSignature &)
Definition: integer_spec.hpp:168
uavcan::ScalarCodec
Definition: scalar_codec.hpp:20
uavcan::CastModeSaturate
@ CastModeSaturate
Definition: type_util.hpp:17
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184
uavcan::IntegerSpec::LimitsImpl64
Definition: integer_spec.hpp:67


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02