12 #ifndef DOCWIRE_RANGED_H
13 #define DOCWIRE_RANGED_H
15 #include "safety_policy.h"
17 #include <type_traits>
44 template <auto Min, auto Max,
typename T, safety_policy safety_level = default_safety_level>
47 static_assert(!std::is_same_v<std::remove_const_t<decltype(Min)>,
unlimited_t> || !std::is_same_v<std::remove_const_t<decltype(Max)>,
unlimited_t>,
48 "ranged must have at least one concrete bound; use the raw type instead of ranged<..., unlimited, unlimited>.");
50 static_assert(std::is_same_v<std::remove_const_t<decltype(Min)>,
unlimited_t> || std::is_convertible_v<decltype(Min), T>,
51 "The Min bound must be convertible to the ranged type T.");
53 static_assert(std::is_same_v<std::remove_const_t<decltype(Max)>,
unlimited_t> || std::is_convertible_v<decltype(Max), T>,
54 "The Max bound must be convertible to the ranged type T.");
62 if constexpr (!std::is_same_v<std::remove_const_t<decltype(Min)>,
unlimited_t>) {
63 enforce<safety_level>(m_value >=
static_cast<T
>(Min),
"Value is below the expected minimum",
"value"_v = m_value,
"min"_v =
static_cast<T
>(Min));
65 if constexpr (!std::is_same_v<std::remove_const_t<decltype(Max)>,
unlimited_t>) {
66 enforce<safety_level>(m_value <=
static_cast<T
>(Max),
"Value is above the expected maximum",
"value"_v = m_value,
"max"_v =
static_cast<T
>(Max));
71 operator T()
const {
return m_value; }
72 T get()
const {
return m_value; }
82 template <auto Min,
typename T, safety_policy safety_level = default_safety_level>
83 using at_least = ranged<Min, unlimited, T, safety_level>;
89 template <auto Max,
typename T, safety_policy safety_level = default_safety_level>
90 using at_most = ranged<unlimited, Max, T, safety_level>;
96 template <auto Value,
typename T, safety_policy safety_level = default_safety_level>
97 using exactly = ranged<Value, Value, T, safety_level>;
103 template <
typename T, safety_policy safety_level = default_safety_level>
104 using non_negative = at_least<0, T, safety_level>;
A wrapper for numeric types that enforces a range [Min, Max].
ranged(T value)
Constructs a ranged value, enforcing the bounds in strict mode.
The main namespace for the DocWire SDK.
constexpr unlimited_t unlimited
A convenient instance of the unlimited_t marker.
A marker type to signify an unlimited bound in a ranged type.