18 #ifndef LOCATION_SERVICE_COM_UBUNTU_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_ 19 #define LOCATION_SERVICE_COM_UBUNTU_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_ 30 namespace connectivity
36 template<
typename Tag,
int min,
int max,
int inv = min-1>
40 static_assert(min < max, "min >= max
"); 45 inline static int invalid() 53 inline static int minimum() 61 inline static int maximum() 69 inline static int range() 77 inline static BoundedInteger<Tag, min, max, inv> from_percent(float percent) 80 percent = std::min<float>(1., std::max<float>(0., percent)); 82 return BoundedInteger<Tag, min, max, inv> 84 static_cast<int>(minimum() + percent * range()) 91 inline BoundedInteger() : value(min-1) 100 inline explicit BoundedInteger(int value) : value(value) 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) + "]
"); 112 inline BoundedInteger(const BoundedInteger<Tag, min, max, inv>& rhs) : value(rhs.value) 121 inline BoundedInteger<Tag, min, max, inv>& operator=(const BoundedInteger<Tag, min, max, inv>& rhs) 132 inline bool operator==(const BoundedInteger<Tag, min, max, inv>& rhs) const 134 return value == rhs.value; 141 inline operator int() const 150 inline bool is_valid() const 152 return min <= value && value <= max; 159 inline int get() const 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) + "]
"); 191 inline friend std::ostream& operator<<(std::ostream& out, const BoundedInteger<Tag, min, max, inv>& bi) 209 #endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
static int invalid()
Returns the invalid value for the specified range.
A helper class to handle bounded integer values, with an optional domain for tagging domain-specific ...