6#include <bond/core/config.h>
8#include <boost/static_assert.hpp>
9#include <boost/utility/enable_if.hpp>
21 inline const char* checked_add(
const char* ptr, uint32_t offset)
24 BOOST_STATIC_ASSERT(
sizeof(
const char*) ==
sizeof(std::uintptr_t));
26 std::uintptr_t uintptr =
reinterpret_cast<std::uintptr_t
>(ptr);
27 if (((std::numeric_limits<uintptr_t>::max)() - offset) < uintptr)
29 throw std::overflow_error(
"Offset caused pointer to overflow");
34 template <
typename T,
typename U>
35 inline typename boost::enable_if_c<std::is_unsigned<T>::value && std::is_unsigned<U>::value, T>::type
36 checked_add(T lhs, U rhs)
38 BOOST_STATIC_ASSERT(
sizeof(T) >=
sizeof(U));
40 if (((std::numeric_limits<T>::max)() - lhs) < rhs)
42 throw std::overflow_error(
"Overflow on addition");
48 inline typename boost::enable_if_c<std::is_unsigned<T>::value && (
sizeof(T) <
sizeof(uint64_t)), T>::type
49 checked_multiply(T lhs, uint8_t rhs)
51 uint64_t result =
static_cast<uint64_t
>(lhs) *
static_cast<uint64_t
>(rhs);
53 if (result > (std::numeric_limits<T>::max)())
55 throw std::overflow_error(
"Overflow on multiplication");
58 return static_cast<T
>(result);
namespace bond
Definition: apply.h:17