templates.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_UTIL_TEMPLATES_HPP_INCLUDED
6 #define UAVCAN_UTIL_TEMPLATES_HPP_INCLUDED
7 
8 #include <climits>
9 #include <cstddef>
10 #include <cmath>
11 #include <uavcan/build_config.hpp>
12 
13 #ifndef UAVCAN_CPP_VERSION
14 # error UAVCAN_CPP_VERSION
15 #endif
16 #if UAVCAN_CPP_VERSION < UAVCAN_CPP11
17 # include <float.h> // cfloat may not be available
18 #else
19 # include <cfloat> // C++11 mode assumes that all standard headers are available
20 #endif
21 
22 namespace uavcan
23 {
28 template <bool Value>
30 
31 template <>
33 {
34  static void check() { }
35 };
36 
41 template <long N> struct ShowIntegerAsError;
42 
47 {
48  Noncopyable(const Noncopyable&);
49  Noncopyable& operator=(const Noncopyable&);
50 protected:
53 };
54 
58 template <bool B, typename T = void>
60 
61 template <typename T>
62 struct UAVCAN_EXPORT EnableIf<true, T>
63 {
64  typedef T Type;
65 };
66 
70 template <typename T, typename R = void>
72 {
73  typedef R Type;
74 };
75 
79 template <bool Condition, typename TrueType, typename FalseType>
81 
82 template <typename TrueType, typename FalseType>
84 {
85  typedef TrueType Result;
86 };
87 
88 template <typename TrueType, typename FalseType>
90 {
91  typedef FalseType Result;
92 };
93 
97 template<class T, class U>
98 struct IsSameType
99 {
100  enum { Result = 0 };
101 };
102 
103 template <typename T>
104 struct IsSameType<T, T>
105 {
106  enum { Result = 1 };
107 };
108 
112 template <typename T> struct RemoveReference { typedef T Type; };
113 template <typename T> struct RemoveReference<T&> { typedef T Type; };
114 #if UAVCAN_CPP_VERSION > UAVCAN_CPP03
115 template <typename T> struct RemoveReference<T&&> { typedef T Type; };
116 #endif
117 
121 template <typename U> struct ParameterType { typedef const U& Type; };
122 template <typename U> struct ParameterType<U&> { typedef U& Type; };
123 #if UAVCAN_CPP_VERSION > UAVCAN_CPP03
124 template <typename U> struct ParameterType<U&&> { typedef U&& Type; };
125 #endif
126 
130 template <bool> struct UAVCAN_EXPORT BooleanType { };
133 
134 template <int N> struct IntToType { };
135 
139 template <typename T1, typename T2>
141 {
142  template <typename U> static U returner();
143 
144  struct True_ { char x[2]; };
145  struct False_ { };
146 
147  static True_ test(const T2 &);
148  static False_ test(...);
149 
150 public:
151  enum { Result = sizeof(True_) == sizeof(IsImplicitlyConvertibleFromTo<T1, T2>::test(returner<T1>())) };
152 };
153 
159 template <typename From, typename To>
161 {
162  static To impl(const From& from, const To&, TrueType) { return To(from); }
163  static To impl(const From&, const To& default_, FalseType) { return default_; }
164 };
165 
170 template <typename To, typename From>
172 To coerceOrFallback(const From& from, const To& default_)
173 {
174  return CoerceOrFallbackImpl<From, To>::impl(from, default_,
176 }
177 
182 template <typename To, typename From>
184 To coerceOrFallback(const From& from)
185 {
186  return CoerceOrFallbackImpl<From, To>::impl(from, To(),
188 }
196 template <long A, long B>
197 struct EnumMin
198 {
199  enum { Result = (A < B) ? A : B };
200 };
201 
205 template <unsigned long A, unsigned long B>
206 struct EnumMax
207 {
208  enum { Result = (A > B) ? A : B };
209 };
210 
215 template <unsigned Value> struct UAVCAN_EXPORT CompileTimeIntSqrt;
216 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<1> { enum { Result = 1 }; };
217 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<4> { enum { Result = 2 }; };
218 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<9> { enum { Result = 3 }; };
219 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<16> { enum { Result = 4 }; };
220 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<25> { enum { Result = 5 }; };
221 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<36> { enum { Result = 6 }; };
222 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<49> { enum { Result = 7 }; };
223 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<64> { enum { Result = 8 }; };
224 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<81> { enum { Result = 9 }; };
225 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<100> { enum { Result = 10 }; };
226 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<121> { enum { Result = 11 }; };
227 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<144> { enum { Result = 12 }; };
228 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<169> { enum { Result = 13 }; };
229 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<196> { enum { Result = 14 }; };
230 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<225> { enum { Result = 15 }; };
231 template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<256> { enum { Result = 16 }; };
232 
236 template <typename InputIt, typename OutputIt>
238 OutputIt copy(InputIt first, InputIt last, OutputIt result)
239 {
240  while (first != last)
241  {
242  *result = *first;
243  ++first;
244  ++result;
245  }
246  return result;
247 }
248 
252 template <typename ForwardIt, typename T>
254 void fill(ForwardIt first, ForwardIt last, const T& value)
255 {
256  while (first != last)
257  {
258  *first = value;
259  ++first;
260  }
261 }
262 
266 template<typename OutputIt, typename T>
268 void fill_n(OutputIt first, std::size_t n, const T& value)
269 {
270  while (n--)
271  {
272  *first++ = value;
273  }
274 }
275 
279 template <typename T>
281 const T& min(const T& a, const T& b)
282 {
283  return (b < a) ? b : a;
284 }
285 
289 template <typename T>
291 const T& max(const T& a, const T& b)
292 {
293  return (a < b) ? b : a;
294 }
295 
299 template<typename InputIt1, typename InputIt2>
301 bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
302 {
303  while ((first1 != last1) && (first2 != last2))
304  {
305  if (*first1 < *first2)
306  {
307  return true;
308  }
309  if (*first2 < *first1)
310  {
311  return false;
312  }
313  ++first1;
314  ++first2;
315  }
316  return (first1 == last1) && (first2 != last2);
317 }
318 
322 template<typename InputIt1, typename InputIt2>
324 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
325 {
326  while (first1 != last1)
327  {
328  if (*first1 != *first2)
329  {
330  return false;
331  }
332  ++first1;
333  ++first2;
334  }
335  return true;
336 }
337 
341 template <typename T>
343 
345 template <>
347 {
348  enum { IsSigned = 0 };
349  enum { IsInteger = 1 };
350  static bool max() { return true; }
351  static bool min() { return false; }
352 };
353 
355 template <>
357 {
358  enum { IsSigned = 1 };
359  enum { IsInteger = 1 };
360  static char max() { return CHAR_MAX; }
361  static char min() { return CHAR_MIN; }
362 };
363 template <>
364 struct UAVCAN_EXPORT NumericTraits<signed char>
365 {
366  enum { IsSigned = 1 };
367  enum { IsInteger = 1 };
368  static signed char max() { return SCHAR_MAX; }
369  static signed char min() { return SCHAR_MIN; }
370 };
371 template <>
372 struct UAVCAN_EXPORT NumericTraits<unsigned char>
373 {
374  enum { IsSigned = 0 };
375  enum { IsInteger = 1 };
376  static unsigned char max() { return UCHAR_MAX; }
377  static unsigned char min() { return 0; }
378 };
379 
381 template <>
383 {
384  enum { IsSigned = 1 };
385  enum { IsInteger = 1 };
386  static short max() { return SHRT_MAX; }
387  static short min() { return SHRT_MIN; }
388 };
389 template <>
390 struct UAVCAN_EXPORT NumericTraits<unsigned short>
391 {
392  enum { IsSigned = 0 };
393  enum { IsInteger = 1 };
394  static unsigned short max() { return USHRT_MAX; }
395  static unsigned short min() { return 0; }
396 };
397 
399 template <>
401 {
402  enum { IsSigned = 1 };
403  enum { IsInteger = 1 };
404  static int max() { return INT_MAX; }
405  static int min() { return INT_MIN; }
406 };
407 template <>
409 {
410  enum { IsSigned = 0 };
411  enum { IsInteger = 1 };
412  static unsigned int max() { return UINT_MAX; }
413  static unsigned int min() { return 0; }
414 };
415 
417 template <>
419 {
420  enum { IsSigned = 1 };
421  enum { IsInteger = 1 };
422  static long max() { return LONG_MAX; }
423  static long min() { return LONG_MIN; }
424 };
425 template <>
427 {
428  enum { IsSigned = 0 };
429  enum { IsInteger = 1 };
430  static unsigned long max() { return ULONG_MAX; }
431  static unsigned long min() { return 0; }
432 };
433 
435 template <>
437 {
438  enum { IsSigned = 1 };
439  enum { IsInteger = 1 };
440  static long long max() { return LLONG_MAX; }
441  static long long min() { return LLONG_MIN; }
442 };
443 template <>
445 {
446  enum { IsSigned = 0 };
447  enum { IsInteger = 1 };
448  static unsigned long long max() { return ULLONG_MAX; }
449  static unsigned long long min() { return 0; }
450 };
451 
453 template <>
455 {
456  enum { IsSigned = 1 };
457  enum { IsInteger = 0 };
458  static float max() { return FLT_MAX; }
459  static float min() { return FLT_MIN; }
460  static float infinity() { return INFINITY; }
461  static float epsilon() { return FLT_EPSILON; }
462 };
463 
465 template <>
467 {
468  enum { IsSigned = 1 };
469  enum { IsInteger = 0 };
470  static double max() { return DBL_MAX; }
471  static double min() { return DBL_MIN; }
472  static double infinity() { return static_cast<double>(INFINITY) * static_cast<double>(INFINITY); }
473  static double epsilon() { return DBL_EPSILON; }
474 };
475 
476 #if defined(LDBL_MAX) && defined(LDBL_MIN) && defined(LDBL_EPSILON)
477 template <>
479 struct UAVCAN_EXPORT NumericTraits<long double>
480 {
481  enum { IsSigned = 1 };
482  enum { IsInteger = 0 };
483  static long double max() { return LDBL_MAX; }
484  static long double min() { return LDBL_MIN; }
485  static long double infinity() { return static_cast<long double>(INFINITY) * static_cast<long double>(INFINITY); }
486  static long double epsilon() { return LDBL_EPSILON; }
487 };
488 #endif
489 
490 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
491 # undef isnan
492 # undef isinf
493 # undef signbit
494 #endif
495 
500 template <typename T>
501 inline bool isNaN(T arg)
502 {
503 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
504  return std::isnan(arg);
505 #else
506  // coverity[same_on_both_sides : FALSE]
507  // cppcheck-suppress duplicateExpression
508  return !(arg <= arg);
509 #endif
510 }
511 
516 template <typename T>
517 inline bool isInfinity(T arg)
518 {
519 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
520  return std::isinf(arg);
521 #else
522  return (arg >= NumericTraits<T>::infinity()) || (arg <= -NumericTraits<T>::infinity());
523 #endif
524 }
525 
530 template <typename T>
531 inline bool isFinite(T arg)
532 {
533 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
534  return std::isfinite(arg);
535 #else
536  return !isNaN(arg) && !isInfinity(arg);
537 #endif
538 }
539 
544 template <typename T>
545 inline bool getSignBit(T arg)
546 {
547 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
548  return std::signbit(arg);
549 #else
550  // coverity[divide_by_zero : FALSE]
551  return arg < T(0) || (((arg <= T(0)) && (arg >= T(0))) && (T(1) / arg < T(0)));
552 #endif
553 }
554 
555 }
556 
557 #endif // UAVCAN_UTIL_TEMPLATES_HPP_INCLUDED
uavcan::NumericTraits< unsigned int >::min
static unsigned int min()
Definition: templates.hpp:413
uavcan::EnumMax
Definition: templates.hpp:206
uavcan::IsImplicitlyConvertibleFromTo::True_
Definition: templates.hpp:144
uavcan::Noncopyable
Definition: templates.hpp:46
std::size_t
unsigned long size_t
Definition: coverity_scan_model.cpp:19
epsilon
double epsilon
uavcan::ParameterType::Type
const typedef U & Type
Definition: templates.hpp:121
uavcan::isNaN
bool isNaN(T arg)
Definition: templates.hpp:501
uavcan::NumericTraits< double >::epsilon
static double epsilon()
Definition: templates.hpp:473
uavcan::NumericTraits< char >::min
static char min()
Definition: templates.hpp:361
uavcan::StaticAssert< true >::check
static void check()
Definition: templates.hpp:34
uavcan::ParameterType< U & >::Type
U & Type
Definition: templates.hpp:122
uavcan::NumericTraits< unsigned long >::min
static unsigned long min()
Definition: templates.hpp:431
uavcan::NumericTraits< double >::max
static double max()
Definition: templates.hpp:470
uavcan::CoerceOrFallbackImpl::impl
static To impl(const From &, const To &default_, FalseType)
Definition: templates.hpp:163
uavcan::NumericTraits< double >::min
static double min()
Definition: templates.hpp:471
uavcan::getSignBit
bool getSignBit(T arg)
Definition: templates.hpp:545
B
Definition: comparison.cpp:89
uavcan::NumericTraits< signed char >::max
static signed char max()
Definition: templates.hpp:368
uavcan::NumericTraits< signed char >::min
static signed char min()
Definition: templates.hpp:369
uavcan::NumericTraits< short >::min
static short min()
Definition: templates.hpp:387
uavcan::isInfinity
bool isInfinity(T arg)
Definition: templates.hpp:517
uavcan::NumericTraits< unsigned long long >::max
static unsigned long long max()
Definition: templates.hpp:448
uavcan::NumericTraits< int >::max
static int max()
Definition: templates.hpp:404
uavcan::NumericTraits< bool >::max
static bool max()
Definition: templates.hpp:350
uavcan::StaticAssert
struct UAVCAN_EXPORT StaticAssert
Definition: templates.hpp:29
uavcan::Noncopyable::Noncopyable
Noncopyable()
Definition: templates.hpp:51
uavcan::fill_n
UAVCAN_EXPORT void fill_n(OutputIt first, std::size_t n, const T &value)
Definition: templates.hpp:268
uavcan::TrueType
BooleanType< true > TrueType
Definition: templates.hpp:131
uavcan::NumericTraits< float >::min
static float min()
Definition: templates.hpp:459
test
Definition: dsdl/test.py:1
uavcan::NumericTraits< unsigned short >::max
static unsigned short max()
Definition: templates.hpp:394
uavcan::IntToType
Definition: templates.hpp:134
uavcan::RemoveReference
Definition: templates.hpp:112
uavcan::NumericTraits< float >::epsilon
static float epsilon()
Definition: templates.hpp:461
A
Definition: comparison.cpp:95
uavcan::EnumMin
Definition: templates.hpp:197
uavcan::NumericTraits< float >::infinity
static float infinity()
Definition: templates.hpp:460
uavcan::NumericTraits< unsigned long long >::min
static unsigned long long min()
Definition: templates.hpp:449
uavcan::EnableIfType::Type
R Type
Definition: templates.hpp:73
uavcan::NumericTraits
struct UAVCAN_EXPORT NumericTraits
Definition: templates.hpp:342
uavcan::max
const UAVCAN_EXPORT T & max(const T &a, const T &b)
Definition: templates.hpp:291
uavcan::NumericTraits< unsigned long >::max
static unsigned long max()
Definition: templates.hpp:430
uavcan::equal
UAVCAN_EXPORT bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
Definition: templates.hpp:324
uavcan::Select
struct UAVCAN_EXPORT Select
Definition: templates.hpp:80
uavcan::ParameterType
Definition: templates.hpp:121
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
uavcan::IsImplicitlyConvertibleFromTo
Definition: templates.hpp:140
uavcan::EnableIf
Definition: templates.hpp:59
uavcan::min
const UAVCAN_EXPORT T & min(const T &a, const T &b)
Definition: templates.hpp:281
uavcan::IsSameType
Definition: templates.hpp:98
uavcan::BooleanType
Definition: templates.hpp:130
uavcan::CompileTimeIntSqrt
struct UAVCAN_EXPORT CompileTimeIntSqrt
Definition: templates.hpp:215
uavcan::NumericTraits< long long >::max
static long long max()
Definition: templates.hpp:440
uavcan::RemoveReference::Type
T Type
Definition: templates.hpp:112
build_config.hpp
uavcan::Select< false, TrueType, FalseType >::Result
FalseType Result
Definition: templates.hpp:91
int
int
Definition: libstubs.cpp:120
uavcan::NumericTraits< unsigned int >::max
static unsigned int max()
Definition: templates.hpp:412
uavcan::RemoveReference< T & >::Type
T Type
Definition: templates.hpp:113
uavcan::NumericTraits< int >::min
static int min()
Definition: templates.hpp:405
uavcan::CoerceOrFallbackImpl::impl
static To impl(const From &from, const To &, TrueType)
Definition: templates.hpp:162
uavcan::NumericTraits< long >::max
static long max()
Definition: templates.hpp:422
uavcan::NumericTraits< long >::min
static long min()
Definition: templates.hpp:423
uavcan::CoerceOrFallbackImpl
Definition: templates.hpp:160
uavcan::EnumMin::Result
@ Result
Definition: templates.hpp:199
uavcan::lexicographical_compare
UAVCAN_EXPORT bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Definition: templates.hpp:301
uavcan::coerceOrFallback
UAVCAN_EXPORT To coerceOrFallback(const From &from, const To &default_)
Definition: templates.hpp:172
uavcan::NumericTraits< bool >::min
static bool min()
Definition: templates.hpp:351
uavcan::NumericTraits< unsigned short >::min
static unsigned short min()
Definition: templates.hpp:395
uavcan::NumericTraits< unsigned char >::max
static unsigned char max()
Definition: templates.hpp:376
uavcan::NumericTraits< char >::max
static char max()
Definition: templates.hpp:360
uavcan::Select< true, TrueType, FalseType >::Result
TrueType Result
Definition: templates.hpp:85
uavcan::isFinite
bool isFinite(T arg)
Definition: templates.hpp:531
uavcan::fill
UAVCAN_EXPORT void fill(ForwardIt first, ForwardIt last, const T &value)
Definition: templates.hpp:254
uavcan::NumericTraits< short >::max
static short max()
Definition: templates.hpp:386
uavcan::NumericTraits< double >::infinity
static double infinity()
Definition: templates.hpp:472
uavcan::EnumMax::Result
@ Result
Definition: templates.hpp:208
uavcan::NumericTraits< float >::max
static float max()
Definition: templates.hpp:458
uavcan::EnableIf< true, T >::Type
T Type
Definition: templates.hpp:64
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::Noncopyable::~Noncopyable
~Noncopyable()
Definition: templates.hpp:52
uavcan::NumericTraits< long long >::min
static long long min()
Definition: templates.hpp:441
uavcan::IsSameType::Result
@ Result
Definition: templates.hpp:100
uavcan::copy
UAVCAN_EXPORT OutputIt copy(InputIt first, InputIt last, OutputIt result)
Definition: templates.hpp:238
uavcan::FalseType
BooleanType< false > FalseType
Definition: templates.hpp:132
libuavcan_dsdl_compiler.pyratemp.long
long
Definition: pyratemp.py:199
pyuavcan_v0.driver.timestamp_estimator.x
x
Definition: timestamp_estimator.py:221
uavcan::EnableIfType
Definition: templates.hpp:71
uavcan::IsImplicitlyConvertibleFromTo::False_
Definition: templates.hpp:145
uavcan::ShowIntegerAsError
Definition: templates.hpp:41
uavcan::NumericTraits< unsigned char >::min
static unsigned char min()
Definition: templates.hpp:377


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