1 | /*
|
---|
2 | * Value_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Apr 13, 2012
|
---|
5 | * Author: ankele
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef VALUE_IMPL_HPP_
|
---|
9 | #define VALUE_IMPL_HPP_
|
---|
10 |
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 |
|
---|
18 | #include <boost/any.hpp>
|
---|
19 |
|
---|
20 | #include "CodePatterns/Assert.hpp"
|
---|
21 |
|
---|
22 | #include "CodePatterns/Log.hpp"
|
---|
23 |
|
---|
24 | #include "Validators/DummyValidator.hpp"
|
---|
25 | #include "Validators/DiscreteValidator.hpp"
|
---|
26 | #include "Validators/RangeValidator.hpp"
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | // static member
|
---|
31 | template <class T> ConvertTo<T> Value<T>::Converter;
|
---|
32 |
|
---|
33 | /** Constructor of class Value.
|
---|
34 | */
|
---|
35 | template <class T>
|
---|
36 | Value<T>::Value() :
|
---|
37 | ValueSet(false),
|
---|
38 | validator(new DummyValidator<T>)
|
---|
39 | {}
|
---|
40 |
|
---|
41 | /** Constructor of class Value with a validator.
|
---|
42 | *
|
---|
43 | * @param _validator general validator to use
|
---|
44 | */
|
---|
45 | template <class T>
|
---|
46 | Value<T>::Value(const Validator<T> &_validator) :
|
---|
47 | ValueSet(false),
|
---|
48 | validator(_validator.clone())
|
---|
49 | {}
|
---|
50 |
|
---|
51 | /** Constructor of class Value with a discrete validator.
|
---|
52 | *
|
---|
53 | * @param _ValidValues vector with all valid values
|
---|
54 | */
|
---|
55 | template <class T>
|
---|
56 | Value<T>::Value(const std::vector<T> &_ValidValues) :
|
---|
57 | ValueSet(false),
|
---|
58 | validator(NULL)
|
---|
59 | {
|
---|
60 | validator = new DiscreteValidator<T>(_ValidValues);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Constructor of class Value with a range validator.
|
---|
64 | *
|
---|
65 | * @param _ValidRange range of valid values
|
---|
66 | */
|
---|
67 | template <class T>
|
---|
68 | Value<T>::Value(const range<T> &_ValidRange) :
|
---|
69 | ValueSet(false),
|
---|
70 | validator(NULL)
|
---|
71 | {
|
---|
72 | validator = new RangeValidator<T>(_ValidRange);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Destructor of class Value.
|
---|
76 | */
|
---|
77 | template <class T>
|
---|
78 | Value<T>::~Value()
|
---|
79 | {
|
---|
80 | ASSERT(validator,
|
---|
81 | "Value<T>::~Value() - validator missing.");
|
---|
82 | delete(validator);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Checks whether \a _value is a valid value.
|
---|
86 | * \param _value value to check for validity.
|
---|
87 | * \return true - \a _value is valid, false - is not
|
---|
88 | */
|
---|
89 | template <class T>
|
---|
90 | bool Value<T>::isValid(const T & _value) const
|
---|
91 | {
|
---|
92 | ASSERT(validator,
|
---|
93 | "Value<T>::isValid() - validator missing.");
|
---|
94 | return (*validator)(_value);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Compares this discrete value against another \a _instance.
|
---|
98 | *
|
---|
99 | * @param _instance other value to compare to
|
---|
100 | * @return true - if value and valid ranges are the same, false - else
|
---|
101 | */
|
---|
102 | template <class T>
|
---|
103 | bool Value<T>::operator==(const Value<T> &_instance) const
|
---|
104 | {
|
---|
105 | ASSERT(validator,
|
---|
106 | "Value<T>::operator==() - validator missing.");
|
---|
107 | ASSERT(_instance.validator,
|
---|
108 | "Value<T>::operator==() - instance.validator missing.");
|
---|
109 | bool status = true;
|
---|
110 | status = status && (*validator == *_instance.validator);
|
---|
111 | status = status && (ValueSet == _instance.ValueSet);
|
---|
112 | if (ValueSet && _instance.ValueSet)
|
---|
113 | status = status && (value == _instance.value);
|
---|
114 | return status;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /** Getter of value
|
---|
119 | *
|
---|
120 | * @return value
|
---|
121 | */
|
---|
122 | template <class T>
|
---|
123 | const T & Value<T>::get() const
|
---|
124 | {
|
---|
125 | ASSERT(ValueSet,
|
---|
126 | "Value<T>::get() - value has never been set.");
|
---|
127 | return value;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Setter of value
|
---|
131 | *
|
---|
132 | * @param _value new value
|
---|
133 | */
|
---|
134 | template <class T>
|
---|
135 | void Value<T>::set(const T & _value)
|
---|
136 | {
|
---|
137 | ASSERT(isValid(_value),
|
---|
138 | "Value<T>::setValue() - trying to set invalid value "+toString(_value)+".");
|
---|
139 | if (!ValueSet)
|
---|
140 | ValueSet = true;
|
---|
141 | value = _value;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** Tests, if a value has been set
|
---|
146 | *
|
---|
147 | * @return true, if a value has been set
|
---|
148 | */
|
---|
149 | template <class T>
|
---|
150 | bool Value<T>::isSet() const
|
---|
151 | {
|
---|
152 | return ValueSet;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 | /** Checks whether \a _value is a valid value.
|
---|
158 | * \param _value value to check for validity.
|
---|
159 | * \return true - \a _value is valid, false - is not
|
---|
160 | */
|
---|
161 | template <class T>
|
---|
162 | bool Value<T>::isValidAsString(const std::string _value) const
|
---|
163 | {
|
---|
164 | const T castvalue = Converter(_value);
|
---|
165 | // LOG(0, "Converted value reads " << castvalue <<".");
|
---|
166 | return isValid(castvalue);
|
---|
167 | }
|
---|
168 |
|
---|
169 | template <>
|
---|
170 | inline bool Value<std::string>::isValidAsString(const std::string _value) const
|
---|
171 | {
|
---|
172 | return isValid(_value);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** Getter of value, returning string.
|
---|
176 | *
|
---|
177 | * @return string value
|
---|
178 | */
|
---|
179 | template <class T>
|
---|
180 | const std::string Value<T>::getAsString() const
|
---|
181 | {
|
---|
182 | ASSERT(ValueSet,
|
---|
183 | "Value<T>::getAsString() - requesting unset value.");
|
---|
184 | return toString(value);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Setter of value for string
|
---|
188 | *
|
---|
189 | * @param _value string containing new value
|
---|
190 | */
|
---|
191 | template <class T>
|
---|
192 | void Value<T>::setAsString(const std::string _value)
|
---|
193 | {
|
---|
194 | const T castvalue = Converter(_value);
|
---|
195 | // LOG(0, "Converted value reads " << castvalue <<".");
|
---|
196 | set(castvalue);
|
---|
197 | // LOG(0, "STATUS: Value is now set to " << value << ".");
|
---|
198 | }
|
---|
199 |
|
---|
200 | template <>
|
---|
201 | inline void Value<std::string>::setAsString(const std::string _value)
|
---|
202 | {
|
---|
203 | set(_value);
|
---|
204 | // LOG(0, "STATUS: Value is now set to " << value << ".");
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Returns the validator as a const reference.
|
---|
208 | *
|
---|
209 | * @return the validator
|
---|
210 | */
|
---|
211 | template <class T>
|
---|
212 | const Validator<T> &Value<T>::getValidator() const
|
---|
213 | {
|
---|
214 | ASSERT(validator,
|
---|
215 | "Value<T>::getValidator() const - validator missing.");
|
---|
216 | return *validator;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Returns the validator.
|
---|
220 | *
|
---|
221 | * @return the validator
|
---|
222 | */
|
---|
223 | template <class T>
|
---|
224 | Validator<T> &Value<T>::getValidator()
|
---|
225 | {
|
---|
226 | ASSERT(validator,
|
---|
227 | "Value<T>::getValidator() - validator missing.");
|
---|
228 | return *validator;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 |
|
---|
233 | template <class T>
|
---|
234 | const range<T> & Value<T>::getValidRange() const
|
---|
235 | {
|
---|
236 | dynamic_cast<RangeValidator<T>&>(getValidator()).getValidRange();
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Setter for the valid range.
|
---|
240 | *
|
---|
241 | * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
|
---|
242 | *
|
---|
243 | * @param _range range (pair of values)
|
---|
244 | */
|
---|
245 | template <class T>
|
---|
246 | void Value<T>::setValidRange(const range<T> &_range)
|
---|
247 | {
|
---|
248 | dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
|
---|
249 | if (ValueSet) {
|
---|
250 | //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
|
---|
251 | if (!isValid(value)){
|
---|
252 | //std::cout << "ValueSet to false." << std::endl;
|
---|
253 | ValueSet = false;
|
---|
254 | // have full check again in assert such that it appears in output, too
|
---|
255 | ASSERT(isValid(value),
|
---|
256 | "Value<T>::setValidRange() - new range "
|
---|
257 | +toString(_range)+" invalidates current value "+toString(value)+".");
|
---|
258 | }
|
---|
259 | }
|
---|
260 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
261 | }
|
---|
262 |
|
---|
263 | template <class T>
|
---|
264 | void Value<T>::appendValidValue(const T &_value)
|
---|
265 | {
|
---|
266 | dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
|
---|
267 | }
|
---|
268 |
|
---|
269 | template <class T>
|
---|
270 | const std::vector<T> &Value<T>::getValidValues() const
|
---|
271 | {
|
---|
272 | dynamic_cast<DiscreteValidator<T>&>(getValidator()).getValidValues();
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|
277 | #endif /* VALUE_IMPL_HPP_ */
|
---|