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