/* * Value_impl.hpp * * Created on: Apr 13, 2012 * Author: ankele */ #ifndef VALUE_IMPL_HPP_ #define VALUE_IMPL_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include "CodePatterns/Assert.hpp" #include "CodePatterns/Log.hpp" #include "Validators/DummyValidator.hpp" #include "Validators/DiscreteValidator.hpp" #include "Validators/RangeValidator.hpp" #include "ParameterExceptions.hpp" // static member template ConvertTo Value::Converter; /** Constructor of class Value. */ template Value::Value() : ValueSet(false), validator(new DummyValidator) {} /** Constructor of class Value with a validator. * * @param _validator general validator to use */ template Value::Value(const Validator &_validator) : ValueSet(false), validator(_validator.clone()) {} /** Constructor of class Value with a discrete validator. * * @param _ValidValues vector with all valid values */ template Value::Value(const std::vector &_ValidValues) : ValueSet(false), validator(NULL) { validator = new DiscreteValidator(_ValidValues); } /** Constructor of class Value with a range validator. * * @param _ValidRange range of valid values */ template Value::Value(const range &_ValidRange) : ValueSet(false), validator(NULL) { validator = new RangeValidator(_ValidRange); } /** Destructor of class Value. */ template Value::~Value() { ASSERT(validator, "Value::~Value() - validator missing."); delete(validator); } /** Checks whether \a _value is a valid value. * \param _value value to check for validity. * \return true - \a _value is valid, false - is not */ template bool Value::isValid(const T & _value) const throw(ParameterValidatorException) { if (validator == NULL) throw ParameterValidatorException(); return (*validator)(_value); } /** Compares this discrete value against another \a _instance. * * @param _instance other value to compare to * @return true - if value and valid ranges are the same, false - else */ template bool Value::operator==(const Value &_instance) const throw(ParameterValidatorException) { if (validator == NULL) throw ParameterValidatorException(); if (_instance.validator == NULL) throw ParameterValidatorException(); bool status = true; status = status && (*validator == *_instance.validator); status = status && (ValueSet == _instance.ValueSet); if (ValueSet && _instance.ValueSet) status = status && (value == _instance.value); return status; } /** Getter of value * * @return value */ template const T & Value::get() const throw(ParameterValueException) { if (!ValueSet) throw ParameterValueException(); return value; } /** Setter of value * * @param _value new value */ template void Value::set(const T & _value) throw(ParameterException) { if (!isValid(_value)) throw ParameterValueException(); if (!ValueSet) ValueSet = true; value = _value; } /** Tests, if a value has been set * * @return true, if a value has been set */ template bool Value::isSet() const { return ValueSet; } /** Checks whether \a _value is a valid value. * \param _value value to check for validity. * \return true - \a _value is valid, false - is not */ template bool Value::isValidAsString(const std::string _value) const throw(ParameterValidatorException) { const T castvalue = Converter(_value); // LOG(0, "Converted value reads " << castvalue <<"."); return isValid(castvalue); } template <> inline bool Value::isValidAsString(const std::string _value) const throw(ParameterValidatorException) { return isValid(_value); } /** Getter of value, returning string. * * @return string value */ template const std::string Value::getAsString() const throw(ParameterValueException) { if (!ValueSet) throw ParameterValueException(); return toString(value); } /** Setter of value for string * * @param _value string containing new value */ template void Value::setAsString(const std::string _value) throw(ParameterException) { const T castvalue = Converter(_value); // LOG(0, "Converted value reads " << castvalue <<"."); set(castvalue); // LOG(0, "STATUS: Value is now set to " << value << "."); } template <> inline void Value::setAsString(const std::string _value) throw(ParameterException) { set(_value); // LOG(0, "STATUS: Value is now set to " << value << "."); } /** Returns the validator as a const reference. * * @return the validator */ template const Validator &Value::getValidator() const { if (validator == NULL) throw ParameterValidatorException(); return *validator; } /** Returns the validator. * * @return the validator */ template Validator &Value::getValidator() { if (validator == NULL) throw ParameterValidatorException(); return *validator; } template const range & Value::getValidRange() const throw(ParameterValidatorException) { dynamic_cast&>(getValidator()).getValidRange(); } /** Setter for the valid range. * * If value is invalid in new range, we throw ParameterValueException and set ValueSet to false. * * @param _range range (pair of values) */ template void Value::setValidRange(const range &_range) throw(ParameterValueException) { dynamic_cast&>(getValidator()).setValidRange(_range); if (ValueSet) { //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl; if (!isValid(value)){ //std::cout << "ValueSet to false." << std::endl; ValueSet = false; // have full check again in assert such that it appears in output, too throw ParameterValueException() << ParameterValidValues(toString(_range)); } } // LOG(0, "STATUS: Valid range is now " << ValidRange << "."); } template void Value::appendValidValue(const T &_value) throw(ParameterValidatorException) { dynamic_cast&>(getValidator()).appendValidValue(_value); } template const std::vector &Value::getValidValues() const throw(ParameterValidatorException) { dynamic_cast&>(getValidator()).getValidValues(); } #endif /* VALUE_IMPL_HPP_ */