IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
peak_common_range.hpp
Go to the documentation of this file.
1
11#pragma once
12
15#include <peak_common_c/detail/peak_common_defines.h>
16
17#include <type_traits>
18
19namespace peak
20{
21namespace common
22{
23
24namespace detail
25{
26
42template <typename T>
43class RangeT : public detail::IntervalT<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
63 constexpr RangeT(T minimum, T maximum, T increment)
64 : detail::IntervalT<T>(minimum, maximum)
65 , m_increment{ increment }
66 {
67 if (increment > maximum - minimum)
68 {
69 throw peak::common::InvalidParameterException("The given increment is larger than the total range!");
70 }
71 }
72
80 PEAK_COMMON_NO_DISCARD constexpr T GetIncrement() const
81 {
82 return m_increment;
83 }
84
95 constexpr bool operator==(const RangeT& rhs) const
96 {
97 return detail::IntervalT<T>::operator==(rhs) && m_increment == rhs.m_increment;
98 }
99
108 constexpr bool operator!=(const RangeT& rhs) const
109 {
110 return !(rhs == *this);
111 }
112
121 friend std::ostream& operator<<(std::ostream& os, RangeT range)
122 {
123 return os << "{ minimum: " << range.GetMinimum() << ", maximum: " << range.GetMaximum() << ", increment: " << range.GetIncrement()
124 << " }";
125 }
126
133 PEAK_COMMON_NO_DISCARD std::string ToString() const
134 {
135 std::ostringstream ss;
136 ss << *this;
137 return ss.str();
138 }
139
140private:
141 T m_increment;
142
143 friend detail::BackendAccessor<RangeT<T>>;
144
145 template <typename U = T, typename std::enable_if<has_c_type_of<RangeT<U>>::value, bool>::type = false>
146 explicit RangeT(c_type_of_t<RangeT<U>> range)
147 : RangeT<U>(range.minimum, range.maximum, range.increment)
148 {}
149
150 template <typename U = T, typename std::enable_if<has_c_type_of<RangeT<U>>::value, bool>::type = false>
151 constexpr explicit operator detail::c_type_of_t<RangeT<U>>() const
152 {
153 return { this->GetMinimum(), this->GetMaximum(), this->GetIncrement() };
154 }
155};
156
157} // namespace detail
158
173
188
203
218
219
220} // namespace common
221} // namespace peak
Thrown when a function receives an invalid parameter.
Represents a continuous numeric interval with inclusive lower and upper bounds.
constexpr T GetMinimum() const
Returns the lower boundary of the interval.
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.
Represents a numeric range with defined boundaries and a fixed increment.
std::string ToString() const
Returns a string representation of the range.
constexpr bool operator==(const RangeT &rhs) const
Compares this range with another for equality.
constexpr T GetIncrement() const
Retrieves the increment value associated with this range.
constexpr RangeT(T minimum, T maximum, T increment)
Constructs a range with specified minimum, maximum, and increment values.
friend std::ostream & operator<<(std::ostream &os, RangeT range)
Outputs the range as a human-readable string to a stream.
constexpr bool operator!=(const RangeT &rhs) const
Compares this range with another for inequality.
typename c_type_of_< Type >::type c_type_of_t