lomiri-location-service ..
An aggregating location service providing positioning and geocoding capabilities to applications.
bounded_integer.h
Go to the documentation of this file.
1/*
2 * Copyright © 2012-2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 */
18#ifndef LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
19#define LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
20
21#include <iostream>
22#include <stdexcept>
23
24namespace com
25{
26namespace lomiri
27{
28namespace location
29{
30namespace connectivity
31{
36template<typename Tag, int min, int max, int inv = min-1>
38{
39public:
40 static_assert(min < max, "min >= max");
41
45 inline static int invalid()
46 {
47 return inv;
48 }
49
53 inline static int minimum()
54 {
55 return min;
56 }
57
61 inline static int maximum()
62 {
63 return max;
64 }
65
69 inline static int range()
70 {
71 return max - min;
72 }
73
78 {
79 // Capping to [0,1]
80 percent = std::min<float>(1., std::max<float>(0., percent));
81
83 {
84 static_cast<int>(minimum() + percent * range())
85 };
86 }
87
91 inline BoundedInteger() : value(min-1)
92 {
93 }
94
100 inline explicit BoundedInteger(int value) : value(value)
101 {
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) + "]");
106 }
107
112 inline BoundedInteger(const BoundedInteger<Tag, min, max, inv>& rhs) : value(rhs.value)
113 {
114 }
115
122 {
123 value = rhs.value;
124 return *this;
125 }
126
132 inline bool operator==(const BoundedInteger<Tag, min, max, inv>& rhs) const
133 {
134 return value == rhs.value;
135 }
136
141 inline operator int() const
142 {
143 return get();
144 }
145
150 inline bool is_valid() const
151 {
152 return min <= value && value <= max;
153 }
154
159 inline int get() const
160 {
161 return value;
162 }
163
169 inline void set(int new_value)
170 {
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) + "]");
175
176 value = new_value;
177 }
178
180 inline void reset()
181 {
182 value = invalid();
183 }
184
191 inline friend std::ostream& operator<<(std::ostream& out, const BoundedInteger<Tag, min, max, inv>& bi)
192 {
193 out << bi.value;
194
195 if (!bi.is_valid())
196 out << " -> invalid";
197
198 return out;
199 }
200
201private:
202 int value;
203};
204}
205}
206}
207}
208
209#endif // LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
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 maximum()
Access the maximum value of the integer.
Definition accuracy.h:24