source: src/Parameters/Value_impl.hpp@ ddceb1

ForceAnnealing_goodresults ForceAnnealing_tocheck
Last change on this file since ddceb1 was ddceb1, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Action::outputAsPython() access Parameters via getAsStringUnvalidated().

  • otherwise this would not work for Geometry Actions as these cannot yet be validated on e.g. dry-run.
  • as python commands operate via string arguments anyway, this is nothing but an additional transformation.
  • Property mode set to 100644
File size: 6.7 KB
Line 
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#include "ParameterExceptions.hpp"
28
29// static member
30template <class T> ConvertTo<T> Value<T>::Converter;
31
32/** Constructor of class Value.
33 */
34template <class T>
35Value<T>::Value() :
36 ValueSet(false),
37 validator(new DummyValidator<T>)
38{}
39
40/** Constructor of class Value with a validator.
41 *
42 * @param _validator general validator to use
43 */
44template <class T>
45Value<T>::Value(const Validator<T> &_validator) :
46 ValueSet(false),
47 validator(_validator.clone())
48{}
49
50/** Constructor of class Value with a discrete validator.
51 *
52 * @param _ValidValues vector with all valid values
53 */
54template <class T>
55Value<T>::Value(const std::vector<T> &_ValidValues) :
56 ValueSet(false),
57 validator(NULL)
58{
59 validator = new DiscreteValidator<T>(_ValidValues);
60}
61
62/** Constructor of class Value with a range validator.
63 *
64 * @param _ValidRange range of valid values
65 */
66template <class T>
67Value<T>::Value(const range<T> &_ValidRange) :
68 ValueSet(false),
69 validator(NULL)
70{
71 validator = new RangeValidator<T>(_ValidRange);
72}
73
74/** Destructor of class Value.
75 */
76template <class T>
77Value<T>::~Value()
78{
79 ASSERT(validator,
80 "Value<T>::~Value() - validator missing.");
81 delete(validator);
82}
83
84/** Checks whether \a _value is a valid value.
85 * \param _value value to check for validity.
86 * \return true - \a _value is valid, false - is not
87 */
88template <class T>
89inline bool Value<T>::isValid(const T & _value) const throw(ParameterValidatorException)
90{
91 if (validator == NULL) throw ParameterValidatorException();
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 */
100template <class T>
101bool Value<T>::operator==(const Value<T> &_instance) const throw(ParameterValidatorException)
102{
103 if (validator == NULL) throw ParameterValidatorException();
104 if (_instance.validator == NULL) throw ParameterValidatorException();
105 bool status = true;
106 status = status && (*validator == *_instance.validator);
107 status = status && (ValueSet == _instance.ValueSet);
108 if (ValueSet && _instance.ValueSet)
109 status = status && (value == _instance.value);
110 return status;
111}
112
113
114/** Getter of value
115 *
116 * @return value
117 */
118template <class T>
119inline const T & Value<T>::get() const throw(ParameterValueException)
120{
121 if (!isValid(value)) throw ParameterValueException();
122 if (!ValueSet) throw ParameterValueException();
123 return value;
124}
125
126/** Getter of value without any validation
127 *
128 * @return value
129 */
130template <class T>
131inline const T & Value<T>::getUnvalidated() const throw(ParameterValueException)
132{
133 if (!ValueSet) throw ParameterValueException();
134 return value;
135}
136
137/** Setter of value
138 *
139 * @param _value new value
140 */
141template <class T>
142inline void Value<T>::set(const T & _value) throw(ParameterException)
143{
144 // any value may be set, this allows Actions to have invalid parameters
145 // (e.g. because the given atom id does not yet exist) that are checked
146 // on performCall()
147// if (!isValid(_value)) throw ParameterValueException();
148 if (!ValueSet)
149 ValueSet = true;
150 value = _value;
151}
152
153
154/** Tests, if a value has been set
155 *
156 * @return true, if a value has been set
157 */
158template <class T>
159inline bool Value<T>::isSet() const
160{
161 return ValueSet;
162}
163
164
165
166/** Checks whether \a _value is a valid value.
167 * \param _value value to check for validity.
168 * \return true - \a _value is valid, false - is not
169 */
170template <class T>
171inline bool Value<T>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException)
172{
173 const T castvalue = Converter(_value);
174// LOG(0, "Converted value reads " << castvalue <<".");
175 return isValid(castvalue);
176}
177
178/** Getter of value, returning string.
179 *
180 * @return string value
181 */
182template <class T>
183inline const std::string Value<T>::getAsString() const throw(ParameterValueException)
184{
185 return toString(get());
186}
187
188/** Getter of unvalidated value, returning string.
189 *
190 * @return string value
191 */
192template <class T>
193inline const std::string Value<T>::getAsStringUnvalidated() const throw(ParameterValueException)
194{
195 return toString(getUnvalidated());
196}
197
198/** Setter of value for string
199 *
200 * @param _value string containing new value
201 */
202template <class T>
203inline void Value<T>::setAsString(const std::string &_value) throw(ParameterException)
204{
205 const T castvalue = Converter(_value);
206// LOG(0, "Converted value reads " << castvalue <<".");
207 set(castvalue);
208// LOG(0, "STATUS: Value is now set to " << value << ".");
209}
210
211/** Returns the validator as a const reference.
212 *
213 * @return the validator
214 */
215template <class T>
216inline const Validator<T> &Value<T>::getValidator() const
217{
218 if (validator == NULL) throw ParameterValidatorException();
219 return *validator;
220}
221
222/** Returns the validator.
223 *
224 * @return the validator
225 */
226template <class T>
227inline Validator<T> &Value<T>::getValidator()
228{
229 if (validator == NULL) throw ParameterValidatorException();
230 return *validator;
231}
232
233
234
235template <class T>
236inline const range<T> & Value<T>::getValidRange() const throw(ParameterValidatorException)
237{
238 return dynamic_cast<const RangeValidator<T>&>(getValidator()).getValidRange();
239}
240
241/** Setter for the valid range.
242 *
243 * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false.
244 *
245 * @param _range range (pair of values)
246 */
247template <class T>
248inline void Value<T>::setValidRange(const range<T> &_range) throw(ParameterValueException)
249{
250 dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
251 if (ValueSet) {
252 //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
253 if (!isValid(value)){
254 //std::cout << "ValueSet to false." << std::endl;
255 ValueSet = false;
256 // have full check again in assert such that it appears in output, too
257 throw ParameterValueException() << ParameterValidValues(toString(_range));
258 }
259 }
260 // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
261}
262
263template <class T>
264inline void Value<T>::appendValidValue(const T &_value) throw(ParameterValidatorException)
265{
266 dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
267}
268
269template <class T>
270inline const std::vector<T> &Value<T>::getValidValues() const throw(ParameterValidatorException)
271{
272 return dynamic_cast<const DiscreteValidator<T>&>(getValidator()).getValidValues();
273}
274
275
276
277#endif /* VALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.