- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

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>
83 Dst numericConvertBound(Src v)
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 }
93 QI_ASSERT_UNREACHABLE();
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
qi::numericConvertBound
Dst numericConvertBound(Src v)
Definition: numeric.hpp:83
qi::numericConvert
Dst numericConvert(Src v)
Definition: numeric.hpp:50
qi::numericIsInRange
bool numericIsInRange(Src v)
Definition: numeric.hpp:103
qi::NumericPositiveOverflow
boost::numeric::positive_overflow NumericPositiveOverflow
Definition: numeric.hpp:19
assert.hpp
QI_ASSERT_UNREACHABLE
#define QI_ASSERT_UNREACHABLE()
Definition: assert.hpp:33
qi::NumericNegativeOverflow
boost::numeric::negative_overflow NumericNegativeOverflow
Definition: numeric.hpp:20