CVB++ 14.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
21
24 ValueRange() noexcept = default;
25
26
27
29
34 ValueRange(T min, T max)
35 {
36 if(min > max)
37 throw std::invalid_argument("min > max");
38
39 min_ = min;
40 max_ = max;
41 }
42
43
44
46
50 T Min() const noexcept
51 {
52 return min_;
53 }
54
56
60 void SetMin(T min)
61 {
62 if (min > max_)
63 throw std::invalid_argument("min > max");
64 min_ = min;
65 }
66
68
72 T Max() const noexcept
73 {
74 return max_;
75 }
76
78
82 void SetMax(T max)
83 {
84 if (min_ > max)
85 throw std::invalid_argument("min > max");
86 max_ = max;
87 }
88
90
95 bool Contains(T value) const noexcept
96 {
97 return !(value < min_ || value < max_);
98 }
99
100
102
107 bool IntersectsWith(ValueRange<T> other) const noexcept
108 {
109 return !(min_ > other.max_ || max_ < other.min_);
110 }
111
113
118 bool operator==(const ValueRange<T>& valueRange) const noexcept
119 {
120 return min_ == valueRange.min_
121 && max_ == valueRange.max_;
122 }
123
125
130 bool operator!=(const ValueRange<T> & valueRange) const noexcept
131 {
132 return !(*this == valueRange);
133 }
134
136 template <class C>
137 explicit operator ValueRange<C>() const noexcept
138 {
140 "CVB: Unsupported data type - target must be floating point, source must be numeric!");
141 return ValueRange<C>(static_cast<C>(min_), static_cast<C>(max_));
142 }
143
144 private:
145
146 T min_ = T();
147 T max_ = T();
148};
149
150
152
159template <class T>
160inline ValueRange<int> Round(const ValueRange<T> & rhs) noexcept
161{
162 static_assert(std::is_arithmetic<T>::value, "CVB: Unsupported data type - must be numeric!");
163 return ValueRange<int>(static_cast<int>(std::round(rhs.Min())), static_cast<int>(std::round(rhs.Max())));
164}
165
166CVB_END_INLINE_NS
167
168}
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:118
bool operator!=(const ValueRange< T > &valueRange) const noexcept
Compares to an other value range.
Definition: value_range.hpp:130
T Min() const noexcept
Gets the minimum value.
Definition: value_range.hpp:50
bool IntersectsWith(ValueRange< T > other) const noexcept
Checks if this value range intersects with another value range.
Definition: value_range.hpp:107
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:60
T Max() const noexcept
Gets the maximum value.
Definition: value_range.hpp:72
void SetMax(T max)
Sets the maximum value.
Definition: value_range.hpp:82
bool Contains(T value) const noexcept
Checks if a value is within the range.
Definition: value_range.hpp:95
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:380