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  }
60  static UnsignedStorageType mask()
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 
110  static int encode(StorageType value, ScalarCodec& codec, TailArrayOptimizationMode)
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 
158  static int encode(StorageType value, ScalarCodec& codec, TailArrayOptimizationMode)
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>
185 struct IsIntegerSpec<IntegerSpec<BitLen, Signedness, CastMode> >
186 {
187  enum { Result = 1 };
188 };
189 
190 
191 template <unsigned BitLen, Signedness Signedness, CastMode CastMode>
192 class UAVCAN_EXPORT YamlStreamer<IntegerSpec<BitLen, Signedness, 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
ROSCPP_DECL bool check()
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
Definition: type_util.hpp:84
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()
static UnsignedStorageType mask()
UAVCAN_EXPORT const T & max(const T &a, const T &b)
Definition: templates.hpp:291
static void truncate(StorageType &value)
static void validate()
int encode(const T value)
static void stream(Stream &s, const StorageType value, int)
TailArrayOptimizationMode
Definition: type_util.hpp:22
UAVCAN_EXPORT const T & min(const T &a, const T &b)
Definition: templates.hpp:281
static int encode(StorageType value, ScalarCodec &codec, TailArrayOptimizationMode)
static UnsignedStorageType mask()
int decode(T &value)
static StorageType max()
static void saturate(StorageType &value)
static StorageType min()
static int decode(StorageType &out_value, ScalarCodec &codec, TailArrayOptimizationMode)
static int encode(StorageType value, ScalarCodec &codec, TailArrayOptimizationMode)
static void extendDataTypeSignature(DataTypeSignature &)
int
Definition: libstubs.cpp:120


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39