| [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 |  | 
|---|
| [6c05d8] | 24 | #include "Validators/DummyValidator.hpp" | 
|---|
| [4892c3] | 25 | #include "Validators/DiscreteValidator.hpp" | 
|---|
|  | 26 | #include "Validators/RangeValidator.hpp" | 
|---|
| [e45c1d] | 27 | #include "ParameterExceptions.hpp" | 
|---|
| [f10b0c] | 28 |  | 
|---|
| [047cad] | 29 | // static member | 
|---|
|  | 30 | template <class T> ConvertTo<T> Value<T>::Converter; | 
|---|
|  | 31 |  | 
|---|
| [a9a8f9] | 32 | /** Constructor of class Value. | 
|---|
|  | 33 | */ | 
|---|
|  | 34 | template <class T> | 
|---|
|  | 35 | Value<T>::Value() : | 
|---|
|  | 36 | ValueSet(false), | 
|---|
| [6c05d8] | 37 | validator(new DummyValidator<T>) | 
|---|
| [a9a8f9] | 38 | {} | 
|---|
|  | 39 |  | 
|---|
|  | 40 | /** Constructor of class Value with a validator. | 
|---|
|  | 41 | * | 
|---|
| [4892c3] | 42 | * @param _validator general validator to use | 
|---|
| [a9a8f9] | 43 | */ | 
|---|
|  | 44 | template <class T> | 
|---|
|  | 45 | Value<T>::Value(const Validator<T> &_validator) : | 
|---|
|  | 46 | ValueSet(false), | 
|---|
| [4892c3] | 47 | validator(_validator.clone()) | 
|---|
| [a9a8f9] | 48 | {} | 
|---|
|  | 49 |  | 
|---|
| [4892c3] | 50 | /** Constructor of class Value with a discrete validator. | 
|---|
|  | 51 | * | 
|---|
|  | 52 | * @param _ValidValues vector with all valid values | 
|---|
|  | 53 | */ | 
|---|
|  | 54 | template <class T> | 
|---|
|  | 55 | Value<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 | */ | 
|---|
|  | 66 | template <class T> | 
|---|
|  | 67 | Value<T>::Value(const range<T> &_ValidRange) : | 
|---|
|  | 68 | ValueSet(false), | 
|---|
|  | 69 | validator(NULL) | 
|---|
|  | 70 | { | 
|---|
|  | 71 | validator = new RangeValidator<T>(_ValidRange); | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
| [a9a8f9] | 74 | /** Destructor of class Value. | 
|---|
|  | 75 | */ | 
|---|
|  | 76 | template <class T> | 
|---|
|  | 77 | Value<T>::~Value() | 
|---|
|  | 78 | { | 
|---|
| [6c05d8] | 79 | ASSERT(validator, | 
|---|
|  | 80 | "Value<T>::~Value() - validator missing."); | 
|---|
|  | 81 | delete(validator); | 
|---|
| [a9a8f9] | 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 | */ | 
|---|
|  | 88 | template <class T> | 
|---|
| [9e6722] | 89 | inline bool Value<T>::isValid(const T & _value) const throw(ParameterValidatorException) | 
|---|
| [a9a8f9] | 90 | { | 
|---|
| [e45c1d] | 91 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
| [6c05d8] | 92 | return (*validator)(_value); | 
|---|
| [a9a8f9] | 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> | 
|---|
| [e45c1d] | 101 | bool Value<T>::operator==(const Value<T> &_instance) const throw(ParameterValidatorException) | 
|---|
| [a9a8f9] | 102 | { | 
|---|
| [e45c1d] | 103 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
|  | 104 | if (_instance.validator == NULL) throw ParameterValidatorException(); | 
|---|
| [a9a8f9] | 105 | bool status = true; | 
|---|
| [8f130c] | 106 | status = status && (*validator == *_instance.validator); | 
|---|
| [a9a8f9] | 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 | */ | 
|---|
|  | 118 | template <class T> | 
|---|
| [9e6722] | 119 | inline const T & Value<T>::get() const throw(ParameterValueException) | 
|---|
| [a9a8f9] | 120 | { | 
|---|
| [0d4168] | 121 | if (!isValid(value)) throw ParameterValueException(); | 
|---|
| [e45c1d] | 122 | if (!ValueSet) throw ParameterValueException(); | 
|---|
| [a9a8f9] | 123 | return value; | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
| [b56114] | 126 | /** Getter of value without any validation | 
|---|
|  | 127 | * | 
|---|
|  | 128 | * @return value | 
|---|
|  | 129 | */ | 
|---|
|  | 130 | template <class T> | 
|---|
|  | 131 | inline const T & Value<T>::getUnvalidated() const throw(ParameterValueException) | 
|---|
|  | 132 | { | 
|---|
|  | 133 | if (!ValueSet) throw ParameterValueException(); | 
|---|
|  | 134 | return value; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [a9a8f9] | 137 | /** Setter of value | 
|---|
|  | 138 | * | 
|---|
|  | 139 | * @param _value new value | 
|---|
|  | 140 | */ | 
|---|
|  | 141 | template <class T> | 
|---|
| [9e6722] | 142 | inline void Value<T>::set(const T & _value) throw(ParameterException) | 
|---|
| [a9a8f9] | 143 | { | 
|---|
| [0d4168] | 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(); | 
|---|
| [a9a8f9] | 148 | if (!ValueSet) | 
|---|
|  | 149 | ValueSet = true; | 
|---|
|  | 150 | value = _value; | 
|---|
|  | 151 | } | 
|---|
|  | 152 |  | 
|---|
| [047cad] | 153 |  | 
|---|
| [95f965] | 154 | /** Tests, if a value has been set | 
|---|
|  | 155 | * | 
|---|
|  | 156 | * @return true, if a value has been set | 
|---|
|  | 157 | */ | 
|---|
|  | 158 | template <class T> | 
|---|
| [9e6722] | 159 | inline bool Value<T>::isSet() const | 
|---|
| [95f965] | 160 | { | 
|---|
|  | 161 | return ValueSet; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
|  | 164 |  | 
|---|
| [047cad] | 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 | */ | 
|---|
|  | 170 | template <class T> | 
|---|
| [b11f5e] | 171 | inline bool Value<T>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException) | 
|---|
| [047cad] | 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 | */ | 
|---|
|  | 182 | template <class T> | 
|---|
| [9e6722] | 183 | inline const std::string Value<T>::getAsString() const throw(ParameterValueException) | 
|---|
| [047cad] | 184 | { | 
|---|
| [0d4168] | 185 | return toString(get()); | 
|---|
| [047cad] | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | /** Setter of value for string | 
|---|
|  | 189 | * | 
|---|
|  | 190 | * @param _value string containing new value | 
|---|
|  | 191 | */ | 
|---|
|  | 192 | template <class T> | 
|---|
| [b11f5e] | 193 | inline void Value<T>::setAsString(const std::string &_value) throw(ParameterException) | 
|---|
| [047cad] | 194 | { | 
|---|
|  | 195 | const T castvalue = Converter(_value); | 
|---|
|  | 196 | //  LOG(0, "Converted value reads " << castvalue <<"."); | 
|---|
|  | 197 | set(castvalue); | 
|---|
|  | 198 | //  LOG(0, "STATUS: Value is now set to " << value << "."); | 
|---|
|  | 199 | } | 
|---|
|  | 200 |  | 
|---|
| [ad6917] | 201 | /** Returns the validator as a const reference. | 
|---|
| [a9a8f9] | 202 | * | 
|---|
|  | 203 | * @return the validator | 
|---|
|  | 204 | */ | 
|---|
|  | 205 | template <class T> | 
|---|
| [9e6722] | 206 | inline const Validator<T> &Value<T>::getValidator() const | 
|---|
| [a9a8f9] | 207 | { | 
|---|
| [e45c1d] | 208 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
| [a9a8f9] | 209 | return *validator; | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
| [ad6917] | 212 | /** Returns the validator. | 
|---|
|  | 213 | * | 
|---|
|  | 214 | * @return the validator | 
|---|
|  | 215 | */ | 
|---|
|  | 216 | template <class T> | 
|---|
| [9e6722] | 217 | inline Validator<T> &Value<T>::getValidator() | 
|---|
| [ad6917] | 218 | { | 
|---|
| [e45c1d] | 219 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
| [ad6917] | 220 | return *validator; | 
|---|
|  | 221 | } | 
|---|
|  | 222 |  | 
|---|
| [a9a8f9] | 223 |  | 
|---|
|  | 224 |  | 
|---|
| [3c5ef5] | 225 | template <class T> | 
|---|
| [9e6722] | 226 | inline const range<T> & Value<T>::getValidRange() const throw(ParameterValidatorException) | 
|---|
| [3c5ef5] | 227 | { | 
|---|
| [50eff3] | 228 | return dynamic_cast<const RangeValidator<T>&>(getValidator()).getValidRange(); | 
|---|
| [3c5ef5] | 229 | } | 
|---|
|  | 230 |  | 
|---|
|  | 231 | /** Setter for the valid range. | 
|---|
|  | 232 | * | 
|---|
| [e45c1d] | 233 | * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false. | 
|---|
| [3c5ef5] | 234 | * | 
|---|
|  | 235 | * @param _range range (pair of values) | 
|---|
|  | 236 | */ | 
|---|
|  | 237 | template <class T> | 
|---|
| [9e6722] | 238 | inline void Value<T>::setValidRange(const range<T> &_range) throw(ParameterValueException) | 
|---|
| [3c5ef5] | 239 | { | 
|---|
|  | 240 | dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range); | 
|---|
|  | 241 | if (ValueSet) { | 
|---|
|  | 242 | //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl; | 
|---|
|  | 243 | if (!isValid(value)){ | 
|---|
|  | 244 | //std::cout << "ValueSet to false." << std::endl; | 
|---|
|  | 245 | ValueSet = false; | 
|---|
|  | 246 | // have full check again in assert such that it appears in output, too | 
|---|
| [e45c1d] | 247 | throw ParameterValueException() << ParameterValidValues(toString(_range)); | 
|---|
| [3c5ef5] | 248 | } | 
|---|
|  | 249 | } | 
|---|
|  | 250 | //  LOG(0, "STATUS: Valid range is now " << ValidRange << "."); | 
|---|
|  | 251 | } | 
|---|
|  | 252 |  | 
|---|
|  | 253 | template <class T> | 
|---|
| [9e6722] | 254 | inline void Value<T>::appendValidValue(const T &_value) throw(ParameterValidatorException) | 
|---|
| [3c5ef5] | 255 | { | 
|---|
|  | 256 | dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value); | 
|---|
|  | 257 | } | 
|---|
|  | 258 |  | 
|---|
|  | 259 | template <class T> | 
|---|
| [9e6722] | 260 | inline const std::vector<T> &Value<T>::getValidValues() const throw(ParameterValidatorException) | 
|---|
| [3c5ef5] | 261 | { | 
|---|
| [50eff3] | 262 | return dynamic_cast<const DiscreteValidator<T>&>(getValidator()).getValidValues(); | 
|---|
| [3c5ef5] | 263 | } | 
|---|
|  | 264 |  | 
|---|
|  | 265 |  | 
|---|
|  | 266 |  | 
|---|
| [a9a8f9] | 267 | #endif /* VALUE_IMPL_HPP_ */ | 
|---|