18#ifndef LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
19#define LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
36template<
typename Tag,
int min,
int max,
int inv = min-1>
40 static_assert(min < max,
"min >= max");
80 percent = std::min<float>(1., std::max<float>(0., percent));
102 if (value < min || value > max)
103 throw std::runtime_error(
104 std::to_string(value) +
" is not in " +
"[" +
105 std::to_string(min) +
", " + std::to_string(max) +
"]");
134 return value == rhs.value;
141 inline operator int()
const
152 return min <= value && value <= max;
169 inline void set(
int new_value)
171 if (new_value < min || new_value > max)
172 throw std::runtime_error(
173 std::to_string(new_value) +
" is not in " +
"[" +
174 std::to_string(min) +
", " + std::to_string(max) +
"]");
196 out <<
" -> invalid";
A helper class to handle bounded integer values, with an optional domain for tagging domain-specific ...
BoundedInteger(const BoundedInteger< Tag, min, max, inv > &rhs)
Copy c'tor.
bool is_valid() const
is_valid checks whether the contained value is in [min, max].
static int invalid()
Returns the invalid value for the specified range.
BoundedInteger()
Constructs an invalid instance.
static int minimum()
Access the minimum value of the integer.
bool operator==(const BoundedInteger< Tag, min, max, inv > &rhs) const
Equality comparison operator.
BoundedInteger(int value)
Constructs an instance from a raw value.
void reset()
Resets the instance to an invalid value.
friend std::ostream & operator<<(std::ostream &out, const BoundedInteger< Tag, min, max, inv > &bi)
operator << pretty prints an instance of BoundedInteger.
void set(int new_value)
Assigns a new raw integer value.
int get() const
Returns the raw integer value contained in this instance.
BoundedInteger< Tag, min, max, inv > & operator=(const BoundedInteger< Tag, min, max, inv > &rhs)
Assignment operator.
static BoundedInteger< Tag, min, max, inv > from_percent(float percent)
from_percent creates a new instance, mapping to the defined range of valid values.
static int range()
Returns max - min.
static int maximum()
Access the maximum value of the integer.