| [a9a8f9] | 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 | 
 | 
|---|
| [db1048] | 24 | #include "Actions/toPythonString.hpp"
 | 
|---|
| [6c05d8] | 25 | #include "Validators/DummyValidator.hpp"
 | 
|---|
| [4892c3] | 26 | #include "Validators/DiscreteValidator.hpp"
 | 
|---|
 | 27 | #include "Validators/RangeValidator.hpp"
 | 
|---|
| [e45c1d] | 28 | #include "ParameterExceptions.hpp"
 | 
|---|
| [f10b0c] | 29 | 
 | 
|---|
| [047cad] | 30 | // static member
 | 
|---|
 | 31 | template <class T> ConvertTo<T> Value<T>::Converter;
 | 
|---|
 | 32 | 
 | 
|---|
| [a9a8f9] | 33 | /** Constructor of class Value.
 | 
|---|
 | 34 |  */
 | 
|---|
 | 35 | template <class T>
 | 
|---|
 | 36 | Value<T>::Value() :
 | 
|---|
 | 37 |   ValueSet(false),
 | 
|---|
| [6c05d8] | 38 |   validator(new DummyValidator<T>)
 | 
|---|
| [a9a8f9] | 39 | {}
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | /** Constructor of class Value with a validator.
 | 
|---|
 | 42 |  *
 | 
|---|
| [4892c3] | 43 |  * @param _validator general validator to use
 | 
|---|
| [a9a8f9] | 44 |  */
 | 
|---|
 | 45 | template <class T>
 | 
|---|
 | 46 | Value<T>::Value(const Validator<T> &_validator) :
 | 
|---|
 | 47 |   ValueSet(false),
 | 
|---|
| [4892c3] | 48 |   validator(_validator.clone())
 | 
|---|
| [a9a8f9] | 49 | {}
 | 
|---|
 | 50 | 
 | 
|---|
| [4892c3] | 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 | 
 | 
|---|
| [a9a8f9] | 75 | /** Destructor of class Value.
 | 
|---|
 | 76 |  */
 | 
|---|
 | 77 | template <class T>
 | 
|---|
 | 78 | Value<T>::~Value()
 | 
|---|
 | 79 | {
 | 
|---|
| [6c05d8] | 80 |   ASSERT(validator,
 | 
|---|
 | 81 |       "Value<T>::~Value() - validator missing.");
 | 
|---|
 | 82 |   delete(validator);
 | 
|---|
| [a9a8f9] | 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>
 | 
|---|
| [9e6722] | 90 | inline bool Value<T>::isValid(const T & _value) const throw(ParameterValidatorException)
 | 
|---|
| [a9a8f9] | 91 | {
 | 
|---|
| [e45c1d] | 92 |   if (validator == NULL) throw ParameterValidatorException();
 | 
|---|
| [6c05d8] | 93 |   return (*validator)(_value);
 | 
|---|
| [a9a8f9] | 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>
 | 
|---|
| [e45c1d] | 102 | bool Value<T>::operator==(const Value<T> &_instance) const throw(ParameterValidatorException)
 | 
|---|
| [a9a8f9] | 103 | {
 | 
|---|
| [e45c1d] | 104 |   if (validator == NULL) throw ParameterValidatorException();
 | 
|---|
 | 105 |   if (_instance.validator == NULL) throw ParameterValidatorException();
 | 
|---|
| [a9a8f9] | 106 |   bool status = true;
 | 
|---|
| [8f130c] | 107 |   status = status && (*validator == *_instance.validator);
 | 
|---|
| [a9a8f9] | 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>
 | 
|---|
| [9e6722] | 120 | inline const T & Value<T>::get() const throw(ParameterValueException)
 | 
|---|
| [a9a8f9] | 121 | {
 | 
|---|
| [0d4168] | 122 |   if (!isValid(value)) throw ParameterValueException();
 | 
|---|
| [e45c1d] | 123 |   if (!ValueSet) throw ParameterValueException();
 | 
|---|
| [a9a8f9] | 124 |   return value;
 | 
|---|
 | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
| [b56114] | 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 | 
 | 
|---|
| [a9a8f9] | 138 | /** Setter of value
 | 
|---|
 | 139 |  *
 | 
|---|
 | 140 |  * @param _value new value
 | 
|---|
 | 141 |  */
 | 
|---|
 | 142 | template <class T>
 | 
|---|
| [9e6722] | 143 | inline void Value<T>::set(const T & _value) throw(ParameterException)
 | 
|---|
| [a9a8f9] | 144 | {
 | 
|---|
| [0d4168] | 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();
 | 
|---|
| [a9a8f9] | 149 |   if (!ValueSet)
 | 
|---|
 | 150 |     ValueSet = true;
 | 
|---|
 | 151 |   value = _value;
 | 
|---|
 | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
| [047cad] | 154 | 
 | 
|---|
| [95f965] | 155 | /** Tests, if a value has been set
 | 
|---|
 | 156 |  *
 | 
|---|
 | 157 |  * @return true, if a value has been set
 | 
|---|
 | 158 |  */
 | 
|---|
 | 159 | template <class T>
 | 
|---|
| [9e6722] | 160 | inline bool Value<T>::isSet() const
 | 
|---|
| [95f965] | 161 | {
 | 
|---|
 | 162 |   return ValueSet;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | 
 | 
|---|
| [047cad] | 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>
 | 
|---|
| [b11f5e] | 172 | inline bool Value<T>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException)
 | 
|---|
| [047cad] | 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>
 | 
|---|
| [9e6722] | 184 | inline const std::string Value<T>::getAsString() const throw(ParameterValueException)
 | 
|---|
| [047cad] | 185 | {
 | 
|---|
| [0d4168] | 186 |   return toString(get());
 | 
|---|
| [047cad] | 187 | }
 | 
|---|
 | 188 | 
 | 
|---|
| [6d78b6f] | 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 | 
 | 
|---|
| [047cad] | 199 | /** Setter of value for string
 | 
|---|
 | 200 |  *
 | 
|---|
 | 201 |  * @param _value string containing new value
 | 
|---|
 | 202 |  */
 | 
|---|
 | 203 | template <class T>
 | 
|---|
| [b11f5e] | 204 | inline void Value<T>::setAsString(const std::string &_value) throw(ParameterException)
 | 
|---|
| [047cad] | 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 | 
 | 
|---|
| [ad6917] | 212 | /** Returns the validator as a const reference.
 | 
|---|
| [a9a8f9] | 213 |  *
 | 
|---|
 | 214 |  * @return the validator
 | 
|---|
 | 215 |  */
 | 
|---|
 | 216 | template <class T>
 | 
|---|
| [9e6722] | 217 | inline const Validator<T> &Value<T>::getValidator() const
 | 
|---|
| [a9a8f9] | 218 | {
 | 
|---|
| [e45c1d] | 219 |   if (validator == NULL) throw ParameterValidatorException();
 | 
|---|
| [a9a8f9] | 220 |   return *validator;
 | 
|---|
 | 221 | }
 | 
|---|
 | 222 | 
 | 
|---|
| [ad6917] | 223 | /** Returns the validator.
 | 
|---|
 | 224 |  *
 | 
|---|
 | 225 |  * @return the validator
 | 
|---|
 | 226 |  */
 | 
|---|
 | 227 | template <class T>
 | 
|---|
| [9e6722] | 228 | inline Validator<T> &Value<T>::getValidator()
 | 
|---|
| [ad6917] | 229 | {
 | 
|---|
| [e45c1d] | 230 |   if (validator == NULL) throw ParameterValidatorException();
 | 
|---|
| [ad6917] | 231 |   return *validator;
 | 
|---|
 | 232 | }
 | 
|---|
 | 233 | 
 | 
|---|
| [a9a8f9] | 234 | 
 | 
|---|
 | 235 | 
 | 
|---|
| [3c5ef5] | 236 | template <class T>
 | 
|---|
| [9e6722] | 237 | inline const range<T> & Value<T>::getValidRange() const throw(ParameterValidatorException)
 | 
|---|
| [3c5ef5] | 238 | {
 | 
|---|
| [50eff3] | 239 |   return dynamic_cast<const RangeValidator<T>&>(getValidator()).getValidRange();
 | 
|---|
| [3c5ef5] | 240 | }
 | 
|---|
 | 241 | 
 | 
|---|
 | 242 | /** Setter for the valid range.
 | 
|---|
 | 243 |  *
 | 
|---|
| [e45c1d] | 244 |  * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false.
 | 
|---|
| [3c5ef5] | 245 |  *
 | 
|---|
 | 246 |  * @param _range range (pair of values)
 | 
|---|
 | 247 |  */
 | 
|---|
 | 248 | template <class T>
 | 
|---|
| [9e6722] | 249 | inline void Value<T>::setValidRange(const range<T> &_range) throw(ParameterValueException)
 | 
|---|
| [3c5ef5] | 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
 | 
|---|
| [e45c1d] | 258 |       throw ParameterValueException() << ParameterValidValues(toString(_range));
 | 
|---|
| [3c5ef5] | 259 |     }
 | 
|---|
 | 260 |   }
 | 
|---|
 | 261 |   //  LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
 | 
|---|
 | 262 | }
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 | template <class T>
 | 
|---|
| [9e6722] | 265 | inline void Value<T>::appendValidValue(const T &_value) throw(ParameterValidatorException)
 | 
|---|
| [3c5ef5] | 266 | {
 | 
|---|
 | 267 |   dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
 | 
|---|
 | 268 | }
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 | template <class T>
 | 
|---|
| [9e6722] | 271 | inline const std::vector<T> &Value<T>::getValidValues() const throw(ParameterValidatorException)
 | 
|---|
| [3c5ef5] | 272 | {
 | 
|---|
| [50eff3] | 273 |   return dynamic_cast<const DiscreteValidator<T>&>(getValidator()).getValidValues();
 | 
|---|
| [3c5ef5] | 274 | }
 | 
|---|
 | 275 | 
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 | 
 | 
|---|
| [a9a8f9] | 278 | #endif /* VALUE_IMPL_HPP_ */
 | 
|---|