-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundedParameter.hpp
executable file
·187 lines (155 loc) · 5.61 KB
/
BoundedParameter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// BoundedParameter class, part of the SPARE library.
// Copyright (C) 2011 Guido Del Vescovo
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/** @brief File BoundedParameter.hpp, containing the BoundedParameter class.
*
* The file contains the BoundedParameter template class, implementing a bounded numerical
* value.
*
* @file BoundedParameter.hpp
* @author Guido Del Vescovo
*/
#ifndef _BoundedParameter_h_
#define _BoundedParameter_h_
// BOOST INCLUDES
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
// SPARE INCLUDES
#include <spare/SpareExceptions.hpp>
#include <spare/SpareTypes.hpp>
namespace spare { // Inclusion in namespace spare.
/** @brief %BoundedParameter class, implementing a bounded numerical value.
*
* This is a simple wrapper class to a numerical value which lies into an interval. It is
* mainly intended for use as an algorithm parameter. The main operators are overloaded and
* a conversion operator to the ValueType is also provided, such that the object can be used
* like a ValueType object in a transparent way. An exception is thrown on assignment of a
* value which falls outside the bounds defined at construction time.
*/
template <typename ValueType>
class BoundedParameter
{
public:
// LIFECYCLE
/** Constructor taking lower and upper bounds.
*
* @param[in] rMinValue Lower bound of the parameter.
* @param[in] rMaxValue Upper bound of the parameter.
*/
BoundedParameter(
const ValueType& rMinValue,
const ValueType& rMaxValue)
: mMIN_VALUE(rMinValue),
mMAX_VALUE(rMaxValue)
{
mValue= mMIN_VALUE;
if (mMAX_VALUE < mMIN_VALUE)
{
throw SpareLogicError("BoundedParameter, 0, Invalid parameter range.");
}
}
// OPERATORS
/** Assignment operator.
*
* @param[in] rValue The value to assign.
* @return A reference to the current instance.
*/
template <typename ValueType_>
BoundedParameter<ValueType>&
operator=(const ValueType_& rValue)
{
if ( ( mMIN_VALUE > static_cast<ValueType>(rValue) ) ||
( mMAX_VALUE < static_cast<ValueType>(rValue) ) )
{
throw SpareLogicError("BoundedParameter, 1, Invalid parameter value (out of "
"range).");
}
mValue= rValue;
return *this;
}
/** Assignment operator.
*
* @param[in] rSource Object instance to assign.
* @return A reference to the current instance.
*/
BoundedParameter<ValueType>&
operator=(const BoundedParameter<ValueType>& rSource)
{
*this= rSource.mValue;
return *this;
}
/** == operator
*/
template <typename ValueType_>
bool operator==(const ValueType_& rValue) const
{ return (mValue == rValue); }
/** != operator
*/
template <typename ValueType_>
bool operator!=(const ValueType_& rValue) const
{ return (mValue != rValue); }
/** > operator
*/
template <typename ValueType_>
bool operator>(const ValueType_& rValue) const
{ return (mValue > rValue); }
/** < operator
*/
template <typename ValueType_>
bool operator<(const ValueType_& rValue) const
{ return (mValue < rValue); }
/** >= operator
*/
template <typename ValueType_>
bool operator>=(const ValueType_& rValue) const
{ return (mValue >= rValue); }
/** <= operator
*/
template <typename ValueType_>
bool operator<=(const ValueType_& rValue) const
{ return (mValue <= rValue); }
/** Conversion operator to ValueType.
*
* @return The value of the parameter.
*/
operator ValueType() const { return mValue; }
// ACCESS
/** Read access to the lower bound value.
*
* @return A reference to the value.
*/
const ValueType& MinValue() const { return mMIN_VALUE; }
/** Read access to the upper bound value.
*
* @return A reference to the value.
*/
const ValueType& MaxValue() const { return mMAX_VALUE; }
private:
// Valore minimo.
const ValueType mMIN_VALUE;
// Valore massimo.
const ValueType mMAX_VALUE;
// Valore corrente.
ValueType mValue;
// BOOST SERIALIZATION
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(mValue);
} // BOOST SERIALIZATION
}; // class BoundedParameter
} // namespace spare
#endif // _BoundedParameter_h_