29 #ifndef PIRANHA_DETAIL_SAFE_INTEGRAL_ADDER_HPP 30 #define PIRANHA_DETAIL_SAFE_INTEGRAL_ADDER_HPP 34 #include <type_traits> 36 #include <piranha/config.hpp> 54 template <typename T, typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
int>::type = 0>
55 inline void safe_integral_adder(T &a,
const T &b)
58 if (unlikely(a > std::numeric_limits<T>::max() - b)) {
59 piranha_throw(std::overflow_error,
"overflow in the addition of two signed integrals");
62 if (unlikely(a < std::numeric_limits<T>::min() - b)) {
63 piranha_throw(std::overflow_error,
"overflow in the addition of two signed integrals");
66 a =
static_cast<T
>(a + b);
69 template <typename T, typename std::enable_if<std::is_integral<T>::value && !std::is_signed<T>::value,
int>::type = 0>
70 inline void safe_integral_adder(T &a,
const T &b)
72 if (unlikely(a > std::numeric_limits<T>::max() - b)) {
73 piranha_throw(std::overflow_error,
"overflow in the addition of two unsigned integrals");
75 a =
static_cast<T
>(a + b);
78 template <typename T, typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
int>::type = 0>
79 inline void safe_integral_subber(T &a,
const T &b)
82 if (unlikely(a > std::numeric_limits<T>::max() + b)) {
83 piranha_throw(std::overflow_error,
"overflow in the subtraction of two signed integrals");
86 if (unlikely(a < std::numeric_limits<T>::min() + b)) {
87 piranha_throw(std::overflow_error,
"overflow in the subtraction of two signed integrals");
90 a =
static_cast<T
>(a - b);
93 template <typename T, typename std::enable_if<std::is_integral<T>::value && !std::is_signed<T>::value,
int>::type = 0>
94 inline void safe_integral_subber(T &a,
const T &b)
96 if (unlikely(a < b)) {
97 piranha_throw(std::overflow_error,
"overflow in the subtraction of two unsigned integrals");
99 a =
static_cast<T
>(a - b);
#define piranha_throw(exception_type,...)
Exception-throwing macro.