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 "Actions/toPythonString.hpp"
|
---|
25 | #include "Validators/DummyValidator.hpp"
|
---|
26 | #include "Validators/DiscreteValidator.hpp"
|
---|
27 | #include "Validators/RangeValidator.hpp"
|
---|
28 | #include "ParameterExceptions.hpp"
|
---|
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 | inline bool Value<T>::isValid(const T & _value) const throw(ParameterValidatorException)
|
---|
91 | {
|
---|
92 | if (validator == NULL) throw ParameterValidatorException();
|
---|
93 | return (*validator)(_value);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Compares this discrete value against another \a _instance.
|
---|
97 | *
|
---|
98 | * @param _instance other value to compare to
|
---|
99 | * @return true - if value and valid ranges are the same, false - else
|
---|
100 | */
|
---|
101 | template <class T>
|
---|
102 | bool Value<T>::operator==(const Value<T> &_instance) const throw(ParameterValidatorException)
|
---|
103 | {
|
---|
104 | if (validator == NULL) throw ParameterValidatorException();
|
---|
105 | if (_instance.validator == NULL) throw ParameterValidatorException();
|
---|
106 | bool status = true;
|
---|
107 | status = status && (*validator == *_instance.validator);
|
---|
108 | status = status && (ValueSet == _instance.ValueSet);
|
---|
109 | if (ValueSet && _instance.ValueSet)
|
---|
110 | status = status && (value == _instance.value);
|
---|
111 | return status;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /** Getter of value
|
---|
116 | *
|
---|
117 | * @return value
|
---|
118 | */
|
---|
119 | template <class T>
|
---|
120 | inline const T & Value<T>::get() const throw(ParameterValueException)
|
---|
121 | {
|
---|
122 | if (!isValid(value)) throw ParameterValueException();
|
---|
123 | if (!ValueSet) throw ParameterValueException();
|
---|
124 | return value;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Getter of value without any validation
|
---|
128 | *
|
---|
129 | * @return value
|
---|
130 | */
|
---|
131 | template <class T>
|
---|
132 | inline const T & Value<T>::getUnvalidated() const throw(ParameterValueException)
|
---|
133 | {
|
---|
134 | if (!ValueSet) throw ParameterValueException();
|
---|
135 | return value;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Setter of value
|
---|
139 | *
|
---|
140 | * @param _value new value
|
---|
141 | */
|
---|
142 | template <class T>
|
---|
143 | inline void Value<T>::set(const T & _value) throw(ParameterException)
|
---|
144 | {
|
---|
145 | // any value may be set, this allows Actions to have invalid parameters
|
---|
146 | // (e.g. because the given atom id does not yet exist) that are checked
|
---|
147 | // on performCall()
|
---|
148 | // if (!isValid(_value)) throw ParameterValueException();
|
---|
149 | if (!ValueSet)
|
---|
150 | ValueSet = true;
|
---|
151 | value = _value;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /** Tests, if a value has been set
|
---|
156 | *
|
---|
157 | * @return true, if a value has been set
|
---|
158 | */
|
---|
159 | template <class T>
|
---|
160 | inline bool Value<T>::isSet() const
|
---|
161 | {
|
---|
162 | return ValueSet;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 | /** Checks whether \a _value is a valid value.
|
---|
168 | * \param _value value to check for validity.
|
---|
169 | * \return true - \a _value is valid, false - is not
|
---|
170 | */
|
---|
171 | template <class T>
|
---|
172 | inline bool Value<T>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException)
|
---|
173 | {
|
---|
174 | const T castvalue = Converter(_value);
|
---|
175 | // LOG(0, "Converted value reads " << castvalue <<".");
|
---|
176 | return isValid(castvalue);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Getter of value, returning string.
|
---|
180 | *
|
---|
181 | * @return string value
|
---|
182 | */
|
---|
183 | template <class T>
|
---|
184 | inline const std::string Value<T>::getAsString() const throw(ParameterValueException)
|
---|
185 | {
|
---|
186 | return toString(get());
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Getter of unvalidated value, returning string.
|
---|
190 | *
|
---|
191 | * @return string value
|
---|
192 | */
|
---|
193 | template <class T>
|
---|
194 | inline const std::string Value<T>::getAsStringUnvalidated() const throw(ParameterValueException)
|
---|
195 | {
|
---|
196 | return toPythonString(getUnvalidated());
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** Setter of value for string
|
---|
200 | *
|
---|
201 | * @param _value string containing new value
|
---|
202 | */
|
---|
203 | template <class T>
|
---|
204 | inline void Value<T>::setAsString(const std::string &_value) throw(ParameterException)
|
---|
205 | {
|
---|
206 | const T castvalue = Converter(_value);
|
---|
207 | // LOG(0, "Converted value reads " << castvalue <<".");
|
---|
208 | set(castvalue);
|
---|
209 | // LOG(0, "STATUS: Value is now set to " << value << ".");
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Returns the validator as a const reference.
|
---|
213 | *
|
---|
214 | * @return the validator
|
---|
215 | */
|
---|
216 | template <class T>
|
---|
217 | inline const Validator<T> &Value<T>::getValidator() const
|
---|
218 | {
|
---|
219 | if (validator == NULL) throw ParameterValidatorException();
|
---|
220 | return *validator;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Returns the validator.
|
---|
224 | *
|
---|
225 | * @return the validator
|
---|
226 | */
|
---|
227 | template <class T>
|
---|
228 | inline Validator<T> &Value<T>::getValidator()
|
---|
229 | {
|
---|
230 | if (validator == NULL) throw ParameterValidatorException();
|
---|
231 | return *validator;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 |
|
---|
236 | template <class T>
|
---|
237 | inline const range<T> & Value<T>::getValidRange() const throw(ParameterValidatorException)
|
---|
238 | {
|
---|
239 | return dynamic_cast<const RangeValidator<T>&>(getValidator()).getValidRange();
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** Setter for the valid range.
|
---|
243 | *
|
---|
244 | * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false.
|
---|
245 | *
|
---|
246 | * @param _range range (pair of values)
|
---|
247 | */
|
---|
248 | template <class T>
|
---|
249 | inline void Value<T>::setValidRange(const range<T> &_range) throw(ParameterValueException)
|
---|
250 | {
|
---|
251 | dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
|
---|
252 | if (ValueSet) {
|
---|
253 | //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
|
---|
254 | if (!isValid(value)){
|
---|
255 | //std::cout << "ValueSet to false." << std::endl;
|
---|
256 | ValueSet = false;
|
---|
257 | // have full check again in assert such that it appears in output, too
|
---|
258 | throw ParameterValueException() << ParameterValidValues(toString(_range));
|
---|
259 | }
|
---|
260 | }
|
---|
261 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
262 | }
|
---|
263 |
|
---|
264 | template <class T>
|
---|
265 | inline void Value<T>::appendValidValue(const T &_value) throw(ParameterValidatorException)
|
---|
266 | {
|
---|
267 | dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
|
---|
268 | }
|
---|
269 |
|
---|
270 | template <class T>
|
---|
271 | inline const std::vector<T> &Value<T>::getValidValues() const throw(ParameterValidatorException)
|
---|
272 | {
|
---|
273 | return dynamic_cast<const DiscreteValidator<T>&>(getValidator()).getValidValues();
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 |
|
---|
278 | #endif /* VALUE_IMPL_HPP_ */
|
---|