IDS peak IPL v1.18.0
Loading...
Searching...
No Matches
peak_common_range.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
11
12#include <type_traits>
13#include <cmath>
14
15namespace peak
16{
17namespace common
18{
19
20namespace detail
21{
22
38template <typename T>
39class RangeT : public detail::IntervalT<T>
40{
41
42public:
43 static_assert(std::is_arithmetic<T>::value, "Template argument of Interval must be arithmetic.");
44
45 using type = T;
46
59 constexpr RangeT(T minimum, T maximum, T increment)
60 : detail::IntervalT<T>(minimum, maximum)
61 , m_increment{ increment }
62 {
63 if (increment > maximum - minimum)
64 {
65 throw peak::common::InvalidParameterException("The given increment is larger than the total range!");
66 }
67 }
68
77 {
78 return m_increment;
79 }
80
93 PEAK_COMMON_NO_DISCARD constexpr T Constrain(T value) const
94 {
95 const auto minimum = IntervalT<T>::GetMinimum();
96 const auto maximum = IntervalT<T>::GetMaximum();
97
98 if (!IsIncrementEqualToZero<T>() && value > minimum)
99 {
100 value = RoundToIncrement(value);
101 }
102
103 return detail::Clamp(value, minimum, maximum);
104 }
105
116 constexpr bool operator==(const RangeT& rhs) const
117 {
118 return detail::IntervalT<T>::operator==(rhs) && m_increment == rhs.m_increment;
119 }
120
129 constexpr bool operator!=(const RangeT& rhs) const
130 {
131 return !(rhs == *this);
132 }
133
142 friend std::ostream& operator<<(std::ostream& os, RangeT range)
143 {
144 return os << "{ minimum: " << range.GetMinimum() << ", maximum: " << range.GetMaximum() << ", increment: " << range.GetIncrement()
145 << " }";
146 }
147
155 {
156 std::ostringstream ss;
157 ss << *this;
158 return ss.str();
159 }
160
161private:
162 T m_increment;
163
164 friend detail::BackendAccessor<RangeT<T>>;
165
166 template <typename U = T, typename std::enable_if<has_c_type_of<RangeT<U>>::value, bool>::type = false>
167 explicit RangeT(c_type_of_t<RangeT<U>> range)
168 : RangeT<U>(range.minimum, range.maximum, range.increment)
169 {}
170
171 template <typename U = T, typename std::enable_if<has_c_type_of<RangeT<U>>::value, bool>::type = false>
172 constexpr explicit operator detail::c_type_of_t<RangeT<U>>() const
173 {
174 return { this->GetMinimum(), this->GetMaximum(), this->GetIncrement() };
175 }
176
177 template <typename V = T, std::enable_if_t<std::is_integral<V>::value, int> = 0>
178 PEAK_COMMON_NO_DISCARD constexpr bool IsIncrementEqualToZero() const
179 {
180 return m_increment == 0;
181 }
182
183 template <typename V = T, std::enable_if_t<std::is_floating_point<V>::value, int> = 0>
184 PEAK_COMMON_NO_DISCARD constexpr bool IsIncrementEqualToZero() const
185 {
186 // Unit in the last place
187 constexpr uint32_t ulp = 4;
188 return detail::AreAlmostEqual(m_increment, static_cast<T>(0), ulp);
189 }
190
191 template<typename V = T, std::enable_if_t<std::is_integral<V>::value, int> = 0>
192 PEAK_COMMON_NO_DISCARD constexpr V RoundToIncrement(V value) const
193 {
194 const auto offset = value - IntervalT<T>::GetMinimum();
195 const auto step = (offset + m_increment / 2) / m_increment;
196 return IntervalT<T>::GetMinimum() + (step * m_increment);
197 }
198
199 template<typename V = T, std::enable_if_t<std::is_floating_point<V>::value, int> = 0>
200 PEAK_COMMON_NO_DISCARD constexpr V RoundToIncrement(V value) const
201 {
202 const auto step = std::round((value - IntervalT<T>::GetMinimum()) / m_increment);
203 return IntervalT<T>::GetMinimum() + (static_cast<V>(step) * m_increment);
204 }
205};
206
207} // namespace detail
208
223
238
253
268
283
298
299
300} // namespace common
301} // namespace peak
Thrown when a function receives an invalid parameter.
Definition peak_common_exceptions.hpp:53
Represents a continuous numeric interval with inclusive lower and upper bounds.
Definition peak_common_interval.hpp:39
constexpr IntervalT(uint32_t minimum, uint32_t maximum)
Definition peak_common_interval.hpp:56
PEAK_COMMON_NO_DISCARD constexpr T GetMaximum() const
Returns the upper boundary of the interval.
Definition peak_common_interval.hpp:83
constexpr bool operator==(const IntervalT< H > &rhs) const
Compares two intervals for equality (integral types).
Definition peak_common_interval.hpp:121
PEAK_COMMON_NO_DISCARD constexpr T GetMinimum() const
Returns the lower boundary of the interval.
Definition peak_common_interval.hpp:72
Represents a numeric range with defined boundaries and a fixed increment.
Definition peak_common_range.hpp:40
PEAK_COMMON_NO_DISCARD constexpr T Constrain(T value) const
Returns a value that is constrained to this range.
Definition peak_common_range.hpp:93
constexpr bool operator==(const RangeT &rhs) const
Compares this range with another for equality.
Definition peak_common_range.hpp:116
constexpr RangeT(T minimum, T maximum, T increment)
Constructs a range with specified minimum, maximum, and increment values.
Definition peak_common_range.hpp:59
T type
Definition peak_common_range.hpp:45
PEAK_COMMON_NO_DISCARD constexpr T GetIncrement() const
Retrieves the increment value associated with this range.
Definition peak_common_range.hpp:76
constexpr bool operator!=(const RangeT &rhs) const
Compares this range with another for inequality.
Definition peak_common_range.hpp:129
PEAK_COMMON_NO_DISCARD std::string ToString() const
Returns a string representation of the range.
Definition peak_common_range.hpp:154
friend std::ostream & operator<<(std::ostream &os, RangeT range)
Outputs the range as a human-readable string to a stream.
Definition peak_common_range.hpp:142
constexpr bool AreAlmostEqual(H lhs, H rhs, uint32_t ulp)
Compares two floating-point values for approximate equality using a ULP-based tolerance.
Definition peak_common_math.hpp:45
detail::RangeT< uint32_t > RangeU
Alias for a range of 32-bit unsigned integers.
Definition peak_common_range.hpp:222
detail::RangeT< double > RangeD
Alias for a range of double-precision floating-point numbers.
Definition peak_common_range.hpp:282
detail::RangeT< int32_t > Range
Alias for a range of 32-bit signed integers.
Definition peak_common_range.hpp:237
detail::RangeT< int64_t > Range64
Alias for a range of 64-bit signed integers.
Definition peak_common_range.hpp:252
detail::RangeT< uint8_t > RangeU8
Alias for a range of 8-bit unsigned integers.
Definition peak_common_range.hpp:297
detail::RangeT< float > RangeF
Alias for a range of floating-point numbers.
Definition peak_common_range.hpp:267
Namespace for internal implementation details and low-level types.
Definition peak_common_math.hpp:19
constexpr T Clamp(T value, T minimum, T maximum)
Definition peak_common_math.hpp:51
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:17
#define PEAK_COMMON_NO_DISCARD
Definition peak_common_defines.h:89