CCIMXDesktop
 
Loading...
Searching...
No Matches
ValueLimiter.hpp
1#ifndef VALUELIMITER_H
2#define VALUELIMITER_H
3
4#include <stdexcept>
5#include <utility>
6
7using ValueLimitType = int;
8using ValueLimitSimplePair = std::pair<ValueLimitType, ValueLimitType>;
14namespace ValueLimitUtils {
15
21constexpr int minFromPair(ValueLimitSimplePair p) noexcept {
22 return p.first;
23}
24
30constexpr int maxFromPair(ValueLimitSimplePair p) noexcept {
31 return p.second;
32}
33
40constexpr bool inBound(ValueLimitType value, ValueLimitSimplePair p) {
41 return value >= minFromPair(p) && value <= maxFromPair(p);
42}
43
51constexpr ValueLimitType boundWithPair(const ValueLimitType value,
52 const ValueLimitSimplePair vp) {
53 if (vp.first > vp.second)
54 throw std::logic_error("boundWithPair: min > max");
55 if (value < vp.first)
56 return vp.first;
57 if (value > vp.second)
58 return vp.second;
59 return value;
60}
61
68template <ValueLimitType MIN = 0, ValueLimitType MAX = 100>
70 static_assert(MIN <= MAX, "MIN must be <= MAX");
71
72public:
78 static constexpr ValueLimitType boundValue(ValueLimitType value) {
79 return value < MIN ? MIN : (value > MAX ? MAX : value);
80 }
81};
82}
83
90template <ValueLimitType MIN = 0, ValueLimitType MAX = 100>
92public:
96 ValueLimiter() = default;
97
103 constexpr ValueLimiter(ValueLimitType runtime_min, ValueLimitType runtime_max) {
104 setMin(runtime_min);
105 setMax(runtime_max);
106 }
107
112 constexpr ValueLimitType getMin() const noexcept {
113 return runtime_min_;
114 }
115
120 constexpr ValueLimitType getMax() const noexcept {
121 return runtime_max_;
122 }
123
129 void setMin(ValueLimitType v) {
130 if (v > runtime_max_)
131 runtime_min_ = runtime_max_;
132 else
133 runtime_min_ = v;
134 }
135
141 void setMax(ValueLimitType v) {
142 if (v < runtime_min_)
143 runtime_max_ = runtime_min_;
144 else
145 runtime_max_ = v;
146 }
147
148 static constexpr ValueLimitType compileMin = MIN;
149 static constexpr ValueLimitType compileMax = MAX;
156 constexpr ValueLimitType boundValue(ValueLimitType value) const {
157 if (value < runtime_min_)
158 return runtime_min_;
159 if (value > runtime_max_)
160 return runtime_max_;
161 return value;
162 }
163
164private:
165 ValueLimitType runtime_min_ = MIN;
166 ValueLimitType runtime_max_ = MAX;
167};
168
169#ifdef __BOUND_PROPERTY_GETSET
170#error "__BOUND_PROPERTY_GETSET is defined, which is not allowed!"
171#else
172#define __BOUND_PROPERTY_GETSET(typeName, typeVar, boundary_pair) \
173 inline void set##typeVar(typeName __value) { \
174 typeVar = ValueLimitUtils::boundWithPair(__value, boundary_pair); \
175 } \
176 inline typeName get##typeVar() const { \
177 return typeVar; \
178 }
179#endif
180
181#ifdef __BOUND_PROPERTY_GETSET_UNALLOWED_OUTRANGE
182#error "__BOUND_PROPERTY_GETSET_UNALLOWED_OUTRANGE is defined, which is not allowed!"
183#else
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"); \
188 typeVar = __value; \
189 } \
190 inline typeName get##typeVar() const { \
191 return typeVar; \
192 }
193#endif
194
195#endif // VALUELIMITER_H
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