bounded_integer.h
Go to the documentation of this file.
/*
* Copyright © 2012-2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
#define LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
#include <iostream>
#include <stdexcept>
namespace com
{
namespace lomiri
{
namespace location
{
namespace connectivity
{
template<typename Tag, int min, int max, int inv = min-1>
class BoundedInteger
{
public:
static_assert(min < max, "min >= max");
inline static int invalid()
{
return inv;
}
inline static int minimum()
{
return min;
}
inline static int maximum()
{
return max;
}
inline static int range()
{
return max - min;
}
inline static BoundedInteger<Tag, min, max, inv> from_percent(float percent)
{
// Capping to [0,1]
percent = std::min<float>(1., std::max<float>(0., percent));
return BoundedInteger<Tag, min, max, inv>
{
static_cast<int>(minimum() + percent * range())
};
}
inline BoundedInteger() : value(min-1)
{
}
inline explicit BoundedInteger(int value) : value(value)
{
if (value < min || value > max)
throw std::runtime_error(
std::to_string(value) + " is not in " + "[" +
std::to_string(min) + ", " + std::to_string(max) + "]");
}
inline BoundedInteger(const BoundedInteger<Tag, min, max, inv>& rhs) : value(rhs.value)
{
}
inline BoundedInteger<Tag, min, max, inv>& operator=(const BoundedInteger<Tag, min, max, inv>& rhs)
{
value = rhs.value;
return *this;
}
inline bool operator==(const BoundedInteger<Tag, min, max, inv>& rhs) const
{
return value == rhs.value;
}
inline operator int() const
{
return get();
}
inline bool is_valid() const
{
return min <= value && value <= max;
}
inline int get() const
{
return value;
}
inline void set(int new_value)
{
if (new_value < min || new_value > max)
throw std::runtime_error(
std::to_string(new_value) + " is not in " + "[" +
std::to_string(min) + ", " + std::to_string(max) + "]");
value = new_value;
}
inline void reset()
{
value = invalid();
}
inline friend std::ostream& operator<<(std::ostream& out, const BoundedInteger<Tag, min, max, inv>& bi)
{
out << bi.value;
if (!bi.is_valid())
out << " -> invalid";
return out;
}
private:
int value;
};
}
}
}
}
#endif // LOCATION_SERVICE_COM_LOMIRI_LOCATION_CONNECTIVITY_BOUNDED_INTEGER_H_
com::lomiri::location::connectivity::BoundedInteger::BoundedInteger
BoundedInteger(const BoundedInteger< Tag, min, max, inv > &rhs)
Copy c'tor.
Definition bounded_integer.h:112
com::lomiri::location::connectivity::BoundedInteger::is_valid
bool is_valid() const
is_valid checks whether the contained value is in [min, max].
Definition bounded_integer.h:150
com::lomiri::location::connectivity::BoundedInteger::invalid
static int invalid()
Returns the invalid value for the specified range.
Definition bounded_integer.h:45
com::lomiri::location::connectivity::BoundedInteger::BoundedInteger
BoundedInteger()
Constructs an invalid instance.
Definition bounded_integer.h:91
com::lomiri::location::connectivity::BoundedInteger::minimum
static int minimum()
Access the minimum value of the integer.
Definition bounded_integer.h:53
com::lomiri::location::connectivity::BoundedInteger::operator==
bool operator==(const BoundedInteger< Tag, min, max, inv > &rhs) const
Equality comparison operator.
Definition bounded_integer.h:132
com::lomiri::location::connectivity::BoundedInteger::BoundedInteger
BoundedInteger(int value)
Constructs an instance from a raw value.
Definition bounded_integer.h:100
com::lomiri::location::connectivity::BoundedInteger::reset
void reset()
Resets the instance to an invalid value.
Definition bounded_integer.h:180
com::lomiri::location::connectivity::BoundedInteger::operator<<
friend std::ostream & operator<<(std::ostream &out, const BoundedInteger< Tag, min, max, inv > &bi)
operator << pretty prints an instance of BoundedInteger.
Definition bounded_integer.h:191
com::lomiri::location::connectivity::BoundedInteger::set
void set(int new_value)
Assigns a new raw integer value.
Definition bounded_integer.h:169
com::lomiri::location::connectivity::BoundedInteger::get
int get() const
Returns the raw integer value contained in this instance.
Definition bounded_integer.h:159
com::lomiri::location::connectivity::BoundedInteger::operator=
BoundedInteger< Tag, min, max, inv > & operator=(const BoundedInteger< Tag, min, max, inv > &rhs)
Assignment operator.
Definition bounded_integer.h:121
com::lomiri::location::connectivity::BoundedInteger::from_percent
static BoundedInteger< Tag, min, max, inv > from_percent(float percent)
from_percent creates a new instance, mapping to the defined range of valid values.
Definition bounded_integer.h:77
com::lomiri::location::connectivity::BoundedInteger::range
static int range()
Returns max - min.
Definition bounded_integer.h:69
com::lomiri::location::connectivity::BoundedInteger::maximum
static int maximum()
Access the maximum value of the integer.
Definition bounded_integer.h:61
com::lomiri::location::connectivity
Definition bounded_integer.h:31
com::lomiri::location
Definition accuracy.h:28
com::lomiri
Definition accuracy.h:26
com
Definition accuracy.h:24