IDS peak IPL v1.18.0
Loading...
Searching...
No Matches
peak_common_interval.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
14
15#include <type_traits>
16#include <sstream>
17
18namespace peak
19{
20namespace common
21{
22
23namespace detail
24{
25
37template <typename T>
39{
40
41public:
42 static_assert(std::is_arithmetic<T>::value, "Template argument of Interval must be arithmetic.");
43
44 using type = T;
45
56 constexpr IntervalT(T minimum, T maximum)
57 : m_minimum(minimum)
58 , m_maximum(maximum)
59 {
60 if (minimum > maximum)
61 {
62 throw peak::common::InvalidParameterException("The maximum value has to be greater than or equal to the minimum value!");
63 }
64 }
65
73 {
74 return m_minimum;
75 }
76
84 {
85 return m_maximum;
86 }
87
100 PEAK_COMMON_NO_DISCARD constexpr T Clamp(T value) const
101 {
102 return detail::Clamp(value, m_minimum, m_maximum);
103 }
104
115 template <typename H
117 ,
118 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
120 >
121 constexpr bool operator==(const IntervalT<H>& rhs) const
122 {
123 return GetMinimum() == rhs.GetMinimum() && GetMaximum() == rhs.GetMaximum();
124 }
125
137 template <typename H
139 ,
140 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
142 >
143 constexpr bool operator==(const IntervalT<H>& rhs) const
144 {
145 // Unit in the last place
146 constexpr uint32_t ulp = 4;
147
148 using common_type = std::common_type_t<H, T>;
149
150 return detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetMinimum()), static_cast<common_type>(rhs.GetMinimum()), ulp)
151 && detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetMaximum()), static_cast<common_type>(rhs.GetMaximum()), ulp);
152 }
153
161 constexpr bool operator!=(const IntervalT& rhs) const
162 {
163 return !(rhs == *this);
164 }
165
174 friend std::ostream& operator<<(std::ostream& os, IntervalT interval)
175 {
176 return os << "{ minimum: " << interval.m_minimum << ", maximum: " << interval.m_maximum << " }";
177 }
178
186 {
187 std::ostringstream ss;
188 ss << *this;
189 return ss.str();
190 }
191
192private:
193 T m_minimum;
194 T m_maximum;
195
196 friend detail::BackendAccessor<IntervalT<T>>;
197
198 template <typename U = T, typename std::enable_if<has_c_type_of<IntervalT<U>>::value, bool>::type = false>
199 explicit IntervalT(c_type_of_t<IntervalT<U>> interval)
200 : IntervalT<U>(interval.minimum, interval.maximum)
201 {}
202
203 template <typename U = T, typename std::enable_if<has_c_type_of<IntervalT<U>>::value, bool>::type = false>
204 constexpr explicit operator detail::c_type_of_t<IntervalT<U>>() const
205 {
206 return { this->GetMinimum(), this->GetMaximum() };
207 }
208};
209
210} // namespace detail
211
227
243
259
275
276
292
293} // namespace common
294} // 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
friend std::ostream & operator<<(std::ostream &os, IntervalT interval)
Outputs the interval as a human-readable string to a stream.
Definition peak_common_interval.hpp:174
constexpr IntervalT(T minimum, T maximum)
Constructs an interval with the given lower and upper bounds.
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
PEAK_COMMON_NO_DISCARD std::string ToString() const
Returns a string representation of the interval.
Definition peak_common_interval.hpp:185
constexpr bool operator==(const IntervalT< H > &rhs) const
Compares two intervals for equality (integral types).
Definition peak_common_interval.hpp:121
T type
Definition peak_common_interval.hpp:44
PEAK_COMMON_NO_DISCARD constexpr T Clamp(T value) const
Clamps a value to the bounds of this interval.
Definition peak_common_interval.hpp:100
constexpr bool operator!=(const IntervalT &rhs) const
Inequality comparison operator.
Definition peak_common_interval.hpp:161
PEAK_COMMON_NO_DISCARD constexpr T GetMinimum() const
Returns the lower boundary of the interval.
Definition peak_common_interval.hpp:72
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::IntervalT< float > IntervalF
Interval with floating-point boundaries.
Definition peak_common_interval.hpp:274
detail::IntervalT< int32_t > Interval
Interval with signed 32-bit integer boundaries.
Definition peak_common_interval.hpp:242
detail::IntervalT< double > IntervalD
Interval with double-precision floating-point boundaries.
Definition peak_common_interval.hpp:291
detail::IntervalT< int64_t > Interval64
Interval with signed 64-bit integer boundaries.
Definition peak_common_interval.hpp:258
detail::IntervalT< uint32_t > IntervalU
Interval with unsigned 32-bit integer boundaries.
Definition peak_common_interval.hpp:226
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