7using ValueLimitType = int;
8using ValueLimitSimplePair = std::pair<ValueLimitType, ValueLimitType>;
40constexpr bool inBound(ValueLimitType value, ValueLimitSimplePair p) {
52 const ValueLimitSimplePair vp) {
53 if (vp.first > vp.second)
54 throw std::logic_error(
"boundWithPair: min > max");
57 if (value > vp.second)
68template <ValueLimitType MIN = 0, ValueLimitType MAX = 100>
70 static_assert(MIN <= MAX,
"MIN must be <= MAX");
78 static constexpr ValueLimitType
boundValue(ValueLimitType value) {
79 return value < MIN ? MIN : (value > MAX ? MAX : value);
90template <ValueLimitType MIN = 0, ValueLimitType MAX = 100>
103 constexpr ValueLimiter(ValueLimitType runtime_min, ValueLimitType runtime_max) {
112 constexpr ValueLimitType
getMin() const noexcept {
120 constexpr ValueLimitType
getMax() const noexcept {
130 if (v > runtime_max_)
131 runtime_min_ = runtime_max_;
142 if (v < runtime_min_)
143 runtime_max_ = runtime_min_;
156 constexpr ValueLimitType
boundValue(ValueLimitType value)
const {
157 if (value < runtime_min_)
159 if (value > runtime_max_)
165 ValueLimitType runtime_min_ = MIN;
166 ValueLimitType runtime_max_ = MAX;
169#ifdef __BOUND_PROPERTY_GETSET
170#error "__BOUND_PROPERTY_GETSET is defined, which is not allowed!"
172#define __BOUND_PROPERTY_GETSET(typeName, typeVar, boundary_pair) \
173 inline void set##typeVar(typeName __value) { \
174 typeVar = ValueLimitUtils::boundWithPair(__value, boundary_pair); \
176 inline typeName get##typeVar() const { \
181#ifdef __BOUND_PROPERTY_GETSET_UNALLOWED_OUTRANGE
182#error "__BOUND_PROPERTY_GETSET_UNALLOWED_OUTRANGE is defined, which is not allowed!"
184#define __BOUND_PROPERTY_GETSET_UNALLOWED_OUTRANGE(typeName, typeVar, boundary_pair) \
185 inline void set##typeVar(typeName __value) { \
186 if (!ValueLimitUtils::inBound(__value, boundary_pair)) \
187 throw std::overflow_error("Value you set is out ranges"); \
190 inline typeName get##typeVar() const { \
Compile-time value limiter with fixed bounds.
Definition ValueLimiter.hpp:69
static constexpr ValueLimitType boundValue(ValueLimitType value)
Clamps a value to the static bounds.
Definition ValueLimiter.hpp:78
Runtime-configurable value limiter with compile-time defaults.
Definition ValueLimiter.hpp:91
constexpr ValueLimitType boundValue(ValueLimitType value) const
Clamps a value to the current bounds.
Definition ValueLimiter.hpp:156
static constexpr ValueLimitType compileMax
Definition ValueLimiter.hpp:149
constexpr ValueLimiter(ValueLimitType runtime_min, ValueLimitType runtime_max)
Construct with runtime-specified bounds.
Definition ValueLimiter.hpp:103
constexpr ValueLimitType getMin() const noexcept
Gets the current minimum bound.
Definition ValueLimiter.hpp:112
constexpr ValueLimitType getMax() const noexcept
Gets the current maximum bound.
Definition ValueLimiter.hpp:120
void setMin(ValueLimitType v)
Sets the minimum bound.
Definition ValueLimiter.hpp:129
ValueLimiter()=default
Default constructor using template bounds.
static constexpr ValueLimitType compileMin
Definition ValueLimiter.hpp:148
void setMax(ValueLimitType v)
Sets the maximum bound.
Definition ValueLimiter.hpp:141
Utility functions for working with value limits.
constexpr int minFromPair(ValueLimitSimplePair p) noexcept
Gets the minimum value from a pair.
Definition ValueLimiter.hpp:21
constexpr bool inBound(ValueLimitType value, ValueLimitSimplePair p)
Checks if a value is within bounds.
Definition ValueLimiter.hpp:40
constexpr int maxFromPair(ValueLimitSimplePair p) noexcept
Gets the maximum value from a pair.
Definition ValueLimiter.hpp:30
constexpr ValueLimitType boundWithPair(const ValueLimitType value, const ValueLimitSimplePair vp)
Clamps a value to the specified bounds.
Definition ValueLimiter.hpp:51