IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
peak_common_interval.hpp
Go to the documentation of this file.
1
11#pragma once
12
17#include <peak_common_c/detail/peak_common_defines.h>
18#include <peak_common_c/types/peak_common_simple_types.h>
19
20#include <type_traits>
21#include <sstream>
22
23namespace peak
24{
25namespace common
26{
27
28namespace detail
29{
30
42template <typename T>
44{
45
46public:
47 static_assert(std::is_arithmetic<T>::value, "Template argument of Interval must be arithmetic.");
48
49 using type = T;
50
61 constexpr IntervalT(T minimum, T maximum)
62 : m_minimum(minimum)
63 , m_maximum(maximum)
64 {
65 if (minimum > maximum)
66 {
67 throw peak::common::InvalidParameterException("The maximum value has to be greater than or equal to the minimum value!");
68 }
69 }
70
77 PEAK_COMMON_NO_DISCARD constexpr T GetMinimum() const
78 {
79 return m_minimum;
80 }
81
88 PEAK_COMMON_NO_DISCARD constexpr T GetMaximum() const
89 {
90 return m_maximum;
91 }
92
103 template <typename H
105 ,
106 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
108 >
109 constexpr bool operator==(const IntervalT<H>& rhs) const
110 {
111 return GetMinimum() == rhs.GetMinimum() && GetMaximum() == rhs.GetMaximum();
112 }
113
125 template <typename H
127 ,
128 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
130 >
131 constexpr bool operator==(const IntervalT<H>& rhs) const
132 {
133 // Unit in the last place
134 constexpr uint32_t ulp = 4;
135
136 using common_type = std::common_type_t<H, T>;
137
138 return detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetMinimum()), static_cast<common_type>(rhs.GetMinimum()), ulp)
139 && detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetMaximum()), static_cast<common_type>(rhs.GetMaximum()), ulp);
140 }
141
149 constexpr bool operator!=(const IntervalT& rhs) const
150 {
151 return !(rhs == *this);
152 }
153
162 friend std::ostream& operator<<(std::ostream& os, IntervalT interval)
163 {
164 return os << "{ minimum: " << interval.m_minimum << ", maximum: " << interval.m_maximum << " }";
165 }
166
173 PEAK_COMMON_NO_DISCARD std::string ToString() const
174 {
175 std::ostringstream ss;
176 ss << *this;
177 return ss.str();
178 }
179
180private:
181 T m_minimum;
182 T m_maximum;
183
184 friend detail::BackendAccessor<IntervalT<T>>;
185
186 template <typename U = T, typename std::enable_if<has_c_type_of<IntervalT<U>>::value, bool>::type = false>
187 explicit IntervalT(c_type_of_t<IntervalT<U>> interval)
188 : IntervalT<U>(interval.minimum, interval.maximum)
189 {}
190
191 template <typename U = T, typename std::enable_if<has_c_type_of<IntervalT<U>>::value, bool>::type = false>
192 constexpr explicit operator detail::c_type_of_t<IntervalT<U>>() const
193 {
194 return { this->GetMinimum(), this->GetMaximum() };
195 }
196};
197
198} // namespace detail
199
215
231
247
248} // namespace common
249} // namespace peak
Thrown when a function receives an invalid parameter.
Represents a continuous numeric interval with inclusive lower and upper bounds.
friend std::ostream & operator<<(std::ostream &os, IntervalT interval)
Outputs the interval as a human-readable string to a stream.
constexpr T GetMinimum() const
Returns the lower boundary of the interval.
std::string ToString() const
Returns a string representation of the interval.
constexpr IntervalT(T minimum, T maximum)
Constructs an interval with the given lower and upper bounds.
constexpr bool operator==(const IntervalT< H > &rhs) const
Compares two intervals for equality (integral types).
constexpr T GetMaximum() const
Returns the upper boundary of the interval.
constexpr bool operator!=(const IntervalT &rhs) const
Inequality comparison operator.
constexpr bool AreAlmostEqual(H lhs, H rhs, uint32_t ulp)
Compares two floating-point values for approximate equality using a ULP-based tolerance.
typename c_type_of_< Type >::type c_type_of_t