CVB++ 15.0
value_range.hpp
1#pragma once
2
3#include <cmath>
4
5#include "global.hpp"
6
7namespace Cvb
8{
9
10CVB_BEGIN_INLINE_NS
11
13
15template <class T>
16class ValueRange final
17{
18 public:
19
20 using ValueType = T;
21
23
26 ValueRange() noexcept = default;
27
28
29
31
36 ValueRange(T min, T max)
37 {
38 if(min > max)
39 throw std::invalid_argument("min > max");
40
41 min_ = min;
42 max_ = max;
43 }
44
45
46
48
52 T Min() const noexcept
53 {
54 return min_;
55 }
56
58
62 void SetMin(T min)
63 {
64 if (min > max_)
65 throw std::invalid_argument("min > max");
66 min_ = min;
67 }
68
70
74 T Max() const noexcept
75 {
76 return max_;
77 }
78
80
84 void SetMax(T max)
85 {
86 if (min_ > max)
87 throw std::invalid_argument("min > max");
88 max_ = max;
89 }
90
92
97 bool Contains(T value) const noexcept
98 {
99 return !(value < min_ || value < max_);
100 }
101
102
104
109 bool IntersectsWith(ValueRange<T> other) const noexcept
110 {
111 return !(min_ > other.max_ || max_ < other.min_);
112 }
113
115
120 bool operator==(const ValueRange<T>& valueRange) const noexcept
121 {
122 return min_ == valueRange.min_
123 && max_ == valueRange.max_;
124 }
125
127
132 bool operator!=(const ValueRange<T> & valueRange) const noexcept
133 {
134 return !(*this == valueRange);
135 }
136
138 template <class C>
139 explicit operator ValueRange<C>() const noexcept
140 {
142 "CVB: Unsupported data type - target must be floating point, source must be numeric!");
143 return ValueRange<C>(static_cast<C>(min_), static_cast<C>(max_));
144 }
145
146 private:
147
148 T min_ = T();
149 T max_ = T();
150};
151
152
154
161template <class T>
162inline ValueRange<int> Round(const ValueRange<T> & rhs) noexcept
163{
164 static_assert(std::is_arithmetic<T>::value, "CVB: Unsupported data type - must be numeric!");
165 return ValueRange<int>(static_cast<int>(std::round(rhs.Min())), static_cast<int>(std::round(rhs.Max())));
166}
167
168CVB_END_INLINE_NS
169
170}
Container for range definitions.
Definition: value_range.hpp:17
bool operator==(const ValueRange< T > &valueRange) const noexcept
Compares to an other value range.
Definition: value_range.hpp:120
bool operator!=(const ValueRange< T > &valueRange) const noexcept
Compares to an other value range.
Definition: value_range.hpp:132
T Min() const noexcept
Gets the minimum value.
Definition: value_range.hpp:52
bool IntersectsWith(ValueRange< T > other) const noexcept
Checks if this value range intersects with another value range.
Definition: value_range.hpp:109
ValueRange() noexcept=default
Creates a default value range e.g. from 0 to 0.
void SetMin(T min)
Sets the minimum value.
Definition: value_range.hpp:62
T Max() const noexcept
Gets the maximum value.
Definition: value_range.hpp:74
void SetMax(T max)
Sets the maximum value.
Definition: value_range.hpp:84
bool Contains(T value) const noexcept
Checks if a value is within the range.
Definition: value_range.hpp:97
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:382