| 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
 | 
|---|
| 30 | template <class T> ConvertTo<T> Value<T>::Converter;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /** Constructor of class Value.
 | 
|---|
| 33 |  */
 | 
|---|
| 34 | template <class T>
 | 
|---|
| 35 | Value<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 |  */
 | 
|---|
| 44 | template <class T>
 | 
|---|
| 45 | Value<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 |  */
 | 
|---|
| 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 | 
 | 
|---|
| 74 | /** Destructor of class Value.
 | 
|---|
| 75 |  */
 | 
|---|
| 76 | template <class T>
 | 
|---|
| 77 | Value<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 |  */
 | 
|---|
| 88 | template <class T>
 | 
|---|
| 89 | inline 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 |  */
 | 
|---|
| 100 | template <class T>
 | 
|---|
| 101 | bool 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 |  */
 | 
|---|
| 118 | template <class T>
 | 
|---|
| 119 | inline 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 | /** Setter of value
 | 
|---|
| 127 |  *
 | 
|---|
| 128 |  * @param _value new value
 | 
|---|
| 129 |  */
 | 
|---|
| 130 | template <class T>
 | 
|---|
| 131 | inline void Value<T>::set(const T & _value) throw(ParameterException)
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   // any value may be set, this allows Actions to have invalid parameters
 | 
|---|
| 134 |   // (e.g. because the given atom id does not yet exist) that are checked
 | 
|---|
| 135 |   // on performCall()
 | 
|---|
| 136 | //  if (!isValid(_value)) throw ParameterValueException();
 | 
|---|
| 137 |   if (!ValueSet)
 | 
|---|
| 138 |     ValueSet = true;
 | 
|---|
| 139 |   value = _value;
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /** Tests, if a value has been set
 | 
|---|
| 144 |  *
 | 
|---|
| 145 |  * @return true, if a value has been set
 | 
|---|
| 146 |  */
 | 
|---|
| 147 | template <class T>
 | 
|---|
| 148 | inline bool Value<T>::isSet() const
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   return ValueSet;
 | 
|---|
| 151 | }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | 
 | 
|---|
| 154 | 
 | 
|---|
| 155 | /** Checks whether \a _value is a valid value.
 | 
|---|
| 156 |  * \param _value value to check for validity.
 | 
|---|
| 157 |  * \return true - \a _value is valid, false - is not
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | template <class T>
 | 
|---|
| 160 | inline bool Value<T>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException)
 | 
|---|
| 161 | {
 | 
|---|
| 162 |   const T castvalue = Converter(_value);
 | 
|---|
| 163 | //  LOG(0, "Converted value reads " << castvalue <<".");
 | 
|---|
| 164 |   return isValid(castvalue);
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | /** Getter of value, returning string.
 | 
|---|
| 168 |  *
 | 
|---|
| 169 |  * @return string value
 | 
|---|
| 170 |  */
 | 
|---|
| 171 | template <class T>
 | 
|---|
| 172 | inline const std::string Value<T>::getAsString() const throw(ParameterValueException)
 | 
|---|
| 173 | {
 | 
|---|
| 174 |   return toString(get());
 | 
|---|
| 175 | }
 | 
|---|
| 176 | 
 | 
|---|
| 177 | /** Setter of value for string
 | 
|---|
| 178 |  *
 | 
|---|
| 179 |  * @param _value string containing new value
 | 
|---|
| 180 |  */
 | 
|---|
| 181 | template <class T>
 | 
|---|
| 182 | inline void Value<T>::setAsString(const std::string &_value) throw(ParameterException)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   const T castvalue = Converter(_value);
 | 
|---|
| 185 | //  LOG(0, "Converted value reads " << castvalue <<".");
 | 
|---|
| 186 |   set(castvalue);
 | 
|---|
| 187 | //  LOG(0, "STATUS: Value is now set to " << value << ".");
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | /** Returns the validator as a const reference.
 | 
|---|
| 191 |  *
 | 
|---|
| 192 |  * @return the validator
 | 
|---|
| 193 |  */
 | 
|---|
| 194 | template <class T>
 | 
|---|
| 195 | inline const Validator<T> &Value<T>::getValidator() const
 | 
|---|
| 196 | {
 | 
|---|
| 197 |   if (validator == NULL) throw ParameterValidatorException();
 | 
|---|
| 198 |   return *validator;
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | /** Returns the validator.
 | 
|---|
| 202 |  *
 | 
|---|
| 203 |  * @return the validator
 | 
|---|
| 204 |  */
 | 
|---|
| 205 | template <class T>
 | 
|---|
| 206 | inline Validator<T> &Value<T>::getValidator()
 | 
|---|
| 207 | {
 | 
|---|
| 208 |   if (validator == NULL) throw ParameterValidatorException();
 | 
|---|
| 209 |   return *validator;
 | 
|---|
| 210 | }
 | 
|---|
| 211 | 
 | 
|---|
| 212 | 
 | 
|---|
| 213 | 
 | 
|---|
| 214 | template <class T>
 | 
|---|
| 215 | inline const range<T> & Value<T>::getValidRange() const throw(ParameterValidatorException)
 | 
|---|
| 216 | {
 | 
|---|
| 217 |   return dynamic_cast<const RangeValidator<T>&>(getValidator()).getValidRange();
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | /** Setter for the valid range.
 | 
|---|
| 221 |  *
 | 
|---|
| 222 |  * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false.
 | 
|---|
| 223 |  *
 | 
|---|
| 224 |  * @param _range range (pair of values)
 | 
|---|
| 225 |  */
 | 
|---|
| 226 | template <class T>
 | 
|---|
| 227 | inline void Value<T>::setValidRange(const range<T> &_range) throw(ParameterValueException)
 | 
|---|
| 228 | {
 | 
|---|
| 229 |   dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
 | 
|---|
| 230 |   if (ValueSet) {
 | 
|---|
| 231 |       //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
 | 
|---|
| 232 |     if (!isValid(value)){
 | 
|---|
| 233 |       //std::cout << "ValueSet to false." << std::endl;
 | 
|---|
| 234 |       ValueSet = false;
 | 
|---|
| 235 |       // have full check again in assert such that it appears in output, too
 | 
|---|
| 236 |       throw ParameterValueException() << ParameterValidValues(toString(_range));
 | 
|---|
| 237 |     }
 | 
|---|
| 238 |   }
 | 
|---|
| 239 |   //  LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
 | 
|---|
| 240 | }
 | 
|---|
| 241 | 
 | 
|---|
| 242 | template <class T>
 | 
|---|
| 243 | inline void Value<T>::appendValidValue(const T &_value) throw(ParameterValidatorException)
 | 
|---|
| 244 | {
 | 
|---|
| 245 |   dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
 | 
|---|
| 246 | }
 | 
|---|
| 247 | 
 | 
|---|
| 248 | template <class T>
 | 
|---|
| 249 | inline const std::vector<T> &Value<T>::getValidValues() const throw(ParameterValidatorException)
 | 
|---|
| 250 | {
 | 
|---|
| 251 |   return dynamic_cast<const DiscreteValidator<T>&>(getValidator()).getValidValues();
 | 
|---|
| 252 | }
 | 
|---|
| 253 | 
 | 
|---|
| 254 | 
 | 
|---|
| 255 | 
 | 
|---|
| 256 | #endif /* VALUE_IMPL_HPP_ */
 | 
|---|