source: src/Parameters/Value_impl.hpp@ 3e6b93

Last change on this file since 3e6b93 was 3e6b93, checked in by Frederik Heber <heber@…>, 11 years ago

Changed checking of Parameter::isValid() on set(), not on get().

  • this would fix problems with ActionQueue::OutputAs...() commands that need to get() parameter values after usage by the Action. If files were forced to be non-present before, then written by the Action, the Validator will then fail.
  • Property mode set to 100644
File size: 6.2 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 // Actions' parameters may later become invalid because a file has been
122 // written that was supposed to not exist (!FilePresentValidator())
123 // if (!isValid(value)) throw ParameterValueException();
124 if (!ValueSet) throw ParameterValueException();
125 return value;
126}
127
128/** Setter of value
129 *
130 * @param _value new value
131 */
132template <class T>
133inline void Value<T>::set(const T & _value) throw(ParameterException)
134{
135 if (!isValid(_value)) throw ParameterValueException();
136 if (!ValueSet)
137 ValueSet = true;
138 value = _value;
139}
140
141
142/** Tests, if a value has been set
143 *
144 * @return true, if a value has been set
145 */
146template <class T>
147inline bool Value<T>::isSet() const
148{
149 return ValueSet;
150}
151
152
153
154/** Checks whether \a _value is a valid value.
155 * \param _value value to check for validity.
156 * \return true - \a _value is valid, false - is not
157 */
158template <class T>
159inline bool Value<T>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException)
160{
161 const T castvalue = Converter(_value);
162// LOG(0, "Converted value reads " << castvalue <<".");
163 return isValid(castvalue);
164}
165
166/** Getter of value, returning string.
167 *
168 * @return string value
169 */
170template <class T>
171inline const std::string Value<T>::getAsString() const throw(ParameterValueException)
172{
173 return toString(get());
174}
175
176/** Setter of value for string
177 *
178 * @param _value string containing new value
179 */
180template <class T>
181inline void Value<T>::setAsString(const std::string &_value) throw(ParameterException)
182{
183 const T castvalue = Converter(_value);
184// LOG(0, "Converted value reads " << castvalue <<".");
185 set(castvalue);
186// LOG(0, "STATUS: Value is now set to " << value << ".");
187}
188
189/** Returns the validator as a const reference.
190 *
191 * @return the validator
192 */
193template <class T>
194inline const Validator<T> &Value<T>::getValidator() const
195{
196 if (validator == NULL) throw ParameterValidatorException();
197 return *validator;
198}
199
200/** Returns the validator.
201 *
202 * @return the validator
203 */
204template <class T>
205inline Validator<T> &Value<T>::getValidator()
206{
207 if (validator == NULL) throw ParameterValidatorException();
208 return *validator;
209}
210
211
212
213template <class T>
214inline const range<T> & Value<T>::getValidRange() const throw(ParameterValidatorException)
215{
216 return dynamic_cast<const RangeValidator<T>&>(getValidator()).getValidRange();
217}
218
219/** Setter for the valid range.
220 *
221 * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false.
222 *
223 * @param _range range (pair of values)
224 */
225template <class T>
226inline void Value<T>::setValidRange(const range<T> &_range) throw(ParameterValueException)
227{
228 dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
229 if (ValueSet) {
230 //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
231 if (!isValid(value)){
232 //std::cout << "ValueSet to false." << std::endl;
233 ValueSet = false;
234 // have full check again in assert such that it appears in output, too
235 throw ParameterValueException() << ParameterValidValues(toString(_range));
236 }
237 }
238 // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
239}
240
241template <class T>
242inline void Value<T>::appendValidValue(const T &_value) throw(ParameterValidatorException)
243{
244 dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
245}
246
247template <class T>
248inline const std::vector<T> &Value<T>::getValidValues() const throw(ParameterValidatorException)
249{
250 return dynamic_cast<const DiscreteValidator<T>&>(getValidator()).getValidValues();
251}
252
253
254
255#endif /* VALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.