25 #ifndef INCLUDED_ValueOrRange_h_GUID_CAAC3116_82B6_4FB9_D32D_BC3391D2D072 26 #define INCLUDED_ValueOrRange_h_GUID_CAAC3116_82B6_4FB9_D32D_BC3391D2D072 32 #include <boost/iterator/iterator_facade.hpp> 33 #include <boost/assert.hpp> 37 #include <type_traits> 44 template <
typename ValueType>
46 :
public boost::iterator_facade<
47 ValueOrRangeIterator<ValueType>, ValueType const,
48 boost::forward_traversal_tag, ValueType const> {
55 friend class boost::iterator_core_access;
56 void increment() { m_value++; }
59 return this->m_value == other.m_value;
62 ValueType
const dereference()
const {
return m_value; }
71 typedef ValueType value_type;
75 typedef ValueType size_type;
77 static_assert(std::is_integral<ValueType>::value,
78 "Ranges only available with integral types");
100 static_assert(std::is_integral<T>::value,
101 "Ranges only available with integral types");
102 return !empty() && getMin() <= val && val <= getMax();
109 BOOST_ASSERT_MSG(!empty(),
"Should only call if not empty!");
117 BOOST_ASSERT_MSG(!empty(),
"Should only call if not empty!");
122 bool isValue()
const {
return (m_begin + 1) == m_end; }
129 bool empty()
const {
return m_begin == 0 && m_end == 0; }
131 size_type
size()
const {
return m_end - m_begin; }
133 value_type getValue()
const {
134 BOOST_ASSERT_MSG(isValue(),
"Should only call getValue if you know " 135 "it's an initialized value!");
160 if (minVal > maxVal) {
176 auto newMax = std::min(this->getMax(), other.
getMax());
177 auto newMin = std::max(this->getMin(), other.
getMin());
186 throw std::logic_error(
187 "Can't extend an empty range with a new max!");
189 if (maxVal >= m_end) {
196 const_iterator
begin()
const {
return const_iterator(m_begin); }
200 const_iterator
end()
const {
return const_iterator(m_end); }
204 explicit ValueOrRange(ValueType val) : m_begin(val) { m_setMax(val); }
207 ValueOrRange(ValueType maxVal, ValueType minVal) : m_begin(minVal) {
208 if (maxVal >= minVal) {
215 void m_setMax(ValueType maxVal) {
222 bool m_isInitialized;
228 #endif // INCLUDED_ValueOrRange_h_GUID_CAAC3116_82B6_4FB9_D32D_BC3391D2D072 ValueOrRange()
Default constructor: an empty range.
Definition: ValueOrRange.h:81
void extendRangeToMax(ValueType maxVal)
If this is an initialized range, extend it as needed such that the given maxVal is included...
Definition: ValueOrRange.h:184
Definition: RunLoopManager.h:42
static ValueOrRange RangeMaxMin(ValueType maxVal, ValueType minVal)
Factory method for creating a range from zero.
Definition: ValueOrRange.h:93
Class providing a unified container-like interface to either a single value or a range of integers...
Definition: ValueOrRange.h:69
bool isValue() const
Has the object been assigned a single value (range of size 1)
Definition: ValueOrRange.h:122
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
void setRangeMaxMin(ValueType maxVal, ValueType minVal=0)
Assign a range of values to this object.
Definition: ValueOrRange.h:159
bool contains(T val) const
Determine if the range contains the given value (treating a value instance of this object as essentia...
Definition: ValueOrRange.h:99
void setValue(ValueType val)
Assign a single value to this object.
Definition: ValueOrRange.h:140
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
Definition: newuoa.h:1888
static ValueOrRange RangeZeroTo(ValueType maxVal)
Factory method for creating a range from zero.
Definition: ValueOrRange.h:88
const_iterator begin() const
Get a "begin" iterator pointing to the first value in the range (or value)
Definition: ValueOrRange.h:196
ValueType getMin() const
Get minimum value - closed lower bound.
Definition: ValueOrRange.h:108
bool empty() const
Is the object an empty range? (default constructor, or setting to a range with max < min) ...
Definition: ValueOrRange.h:129
static ValueOrRange SingleValue(ValueType val)
Factory method for creating a single-value range.
Definition: ValueOrRange.h:84
bool isNonEmptyRange() const
Has the object been assigned a range of size > 1?
Definition: ValueOrRange.h:125
ValueOrRange getIntersection(ValueOrRange other) const
Gets the (possibly empty) intersection of the ranges/values.
Definition: ValueOrRange.h:168
const_iterator end() const
Get a "past-the-end" iterator pointing to one more than the max value)
Definition: ValueOrRange.h:200
void setEmpty()
Assign an empty range to this object.
Definition: ValueOrRange.h:146
Definition: ValueOrRange.h:45
ValueType getMax() const
Get maximum value - closed upper bound.
Definition: ValueOrRange.h:116