libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
numeric.hpp
Go to the documentation of this file.
1 /*
2 ** Copyright (C) 2018 SoftBank Robotics Europe
3 ** See COPYING for the license
4 */
5 
6 #ifndef QI_NUMERIC_HPP
7 #define QI_NUMERIC_HPP
8 
9 #pragma once
10 
11 #include <qi/assert.hpp>
12 #include <boost/numeric/conversion/converter.hpp>
13 #include <cstdint>
14 #include <limits>
15 #include <stdexcept>
16 
17 namespace qi
18 {
19  using NumericPositiveOverflow = boost::numeric::positive_overflow;
20  using NumericNegativeOverflow = boost::numeric::negative_overflow;
21 
49  template<typename Dst, typename Src>
50  Dst numericConvert(Src v)
51  {
52  return boost::numeric::converter<Dst, Src>::convert(v);
53  }
54 
82  template<typename Dst, typename Src>
84  {
85  using Converter = boost::numeric::converter<Dst, Src>;
86 
87  switch (Converter::out_of_range(v))
88  {
89  case boost::numeric::cNegOverflow: return std::numeric_limits<Dst>::min();
90  case boost::numeric::cPosOverflow: return std::numeric_limits<Dst>::max();
91  case boost::numeric::cInRange: return Converter::convert(v);
92  }
94  return Dst{};
95  }
96 
102  template<typename Dst, typename Src>
103  bool numericIsInRange(Src v)
104  {
105  using Converter = boost::numeric::converter<Dst, Src>;
106  return Converter::out_of_range(v) == boost::numeric::cInRange;
107  }
108 }
109 
110 #endif // QI_NUMERIC_HPP
Dst numericConvertBound(Src v)
Definition: numeric.hpp:83
Dst numericConvert(Src v)
Definition: numeric.hpp:50
bool numericIsInRange(Src v)
Definition: numeric.hpp:103
boost::numeric::positive_overflow NumericPositiveOverflow
Definition: numeric.hpp:19
#define QI_ASSERT_UNREACHABLE()
Definition: assert.hpp:33
boost::numeric::negative_overflow NumericNegativeOverflow
Definition: numeric.hpp:20