1#ifndef SANISIZER_ATTEST_HPP
2#define SANISIZER_ATTEST_HPP
29template<
typename Integer_, Integer_ max_>
31 static_assert(std::is_integral<Integer_>::value);
32 static_assert(max_ >= 0);
51 static constexpr Integer_
max = max_;
64template<
typename Value_>
69 static constexpr bool value =
false;
78template<
typename Integer_, Integer_ max_>
83 static constexpr bool value =
true;
91template<
typename Value_>
104template<
typename Value_>
106 if constexpr(std::is_integral<Value_>::value) {
118template<
typename Value_>
120 if constexpr(std::is_integral<Value_>::value) {
121 return std::numeric_limits<Value_>::max();
136template<
typename Max_, Max_ new_max_,
typename Value_>
138 constexpr auto unsigned_new_limit =
static_cast<typename std::make_unsigned<Max_>::type
>(new_max_);
139 if constexpr(std::is_integral<Value_>::value) {
140 constexpr auto max_value =
static_cast<typename std::make_unsigned<Value_>::type
>(std::numeric_limits<Value_>::max());
141 if constexpr(max_value <= unsigned_new_limit) {
148 typedef typename Value_::Integer WrappedInteger;
149 constexpr auto max_value =
static_cast<typename std::make_unsigned<WrappedInteger>::type
>(Value_::max);
150 if constexpr(max_value <= unsigned_new_limit) {
165template<
typename Max_,
typename Value_>
167 return attest_max<Max_, std::numeric_limits<Max_>::max()>(x);
177template<
typename Dest_,
typename Value_>
179 static_assert(std::is_integral<Dest_>::value);
180 constexpr auto umaxed = as_unsigned(std::numeric_limits<Dest_>::max());
182 if constexpr(umaxed < as_unsigned(get_max<Value_>())) {
183 if (umaxed < as_unsigned(
get_value(x))) {
184 throw std::overflow_error(
"overflow detected when casting size-like values in sanisizer");
193template<
typename Value_>
194Value_ attest_gez(Value_ x) {
return x; }
Sanitize sizes to avoid integer overflow.
Definition arithmetic.hpp:16
constexpr auto get_value(Value_ x)
Definition attest.hpp:105
constexpr bool check_overflow(Value_ x)
Definition attest.hpp:178
constexpr auto get_max()
Definition attest.hpp:119
constexpr auto attest_max(Value_ x)
Definition attest.hpp:137
constexpr auto attest_max_by_type(Value_ x)
Definition attest.hpp:166
Attest to additional compile-time properties of an integer.
Definition attest.hpp:30
static constexpr Integer_ max
Definition attest.hpp:51
Integer_ Integer
Definition attest.hpp:45
constexpr Attestation(Integer_ x)
Definition attest.hpp:38
Integer_ value
Definition attest.hpp:56
static constexpr bool value
Definition attest.hpp:69
static constexpr bool value
Definition attest.hpp:96