sanisizer
Sanitize sizes to avoid integer overflow
Loading...
Searching...
No Matches
cap.hpp
Go to the documentation of this file.
1#ifndef SANISIZER_CAP_HPP
2#define SANISIZER_CAP_HPP
3
4#include <type_traits>
5
6#include "utils.hpp"
7#include "generics.hpp"
8#include "comparisons.hpp"
9
15namespace sanisizer {
16
28template<typename Dest_, typename Value_>
29constexpr Dest_ cap(Value_ x) {
30 static_assert(std::is_integral<Dest_>::value);
31 constexpr auto maxed = get_max<Dest_>();
32
33 const auto val = get_value(x);
34 if constexpr(is_greater_than_or_equal(maxed, get_max<Value_>())) {
35 return val;
36 } else if (is_greater_than_or_equal(maxed, val)) {
37 return val;
38 } else {
39 return maxed;
40 }
41}
42
43}
44
45#endif
Signedness-safe integer comparisons.
Generics for operating on integer representations.
Sanitize sizes to avoid integer overflow.
Definition Bounded.hpp:15
constexpr auto get_value(const Value_ x)
Definition generics.hpp:23
constexpr bool is_greater_than_or_equal(Left_ left, Right_ right)
Definition comparisons.hpp:107
constexpr auto get_max()
Definition generics.hpp:39
constexpr Dest_ cap(Value_ x)
Definition cap.hpp:29