/* * ContinuousValue_impl.hpp * * Created on: Sep 29, 2011 * Author: heber */ #ifndef CONTINUOUSVALUE_IMPL_HPP_ #define CONTINUOUSVALUE_IMPL_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include "CodePatterns/Assert.hpp" #include "CodePatterns/Log.hpp" #include "CodePatterns/Range.hpp" #include "ContinuousValue.hpp" #include "ParameterExceptions.hpp" /** Constructor of class DiscreteValue. */ template ContinuousValue::ContinuousValue() : ValueSet(false), ValidRangeSet(range(false, false)) {} /** Constructor of class DiscreteValue with set of valid values. * * @param _ValidValues vector with all valid values */ template ContinuousValue::ContinuousValue(const range &_ValidRange) : ValueSet(false), ValidRangeSet(range(true, true)), ValidRange(_ValidRange) { // LOG(0, "STATUS: Valid range is now " << ValidRange << "."); } /** Destructor of class DiscreteValue. */ template ContinuousValue::~ContinuousValue() {} /** 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 ContinuousValue::isValid(const T & _value) const throw(ParameterValidatorException) { bool isBefore = true; bool isBeyond = true; // check left boundary if ((!ValidRangeSet.first) || (!ValidRangeSet.last)) throw ParameterValidatorException(); isBefore = !((!ValidRange.isBefore(_value))); // if (isBefore) // LOG(0, "INFO: " << _value << " is before " << ValidRange.first << "."); // check right boundary isBeyond = !((!ValidRange.isBeyond(_value)) || (_value == ValidRange.last)); // if (isBeyond) // LOG(0, "INFO: " << _value << " is beyond " << ValidRange.last << "."); return (!isBefore) && (!isBeyond); } /** Compares this continuous 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 ContinuousValue::operator==(const ContinuousValue &_instance) const const throw(ParameterException) { bool status = true; if ((!ValueSet) || (!_instance.ValueSet)) throw ParameterValueException(); if ((!ValidRangeSet.first) || ((!ValidRangeSet.first)) || (!_instance.ValidRangeSet.first) || ((!_instance.ValidRangeSet.first))) throw ParameterValidatorException(); status = status && (ValidRange == _instance.ValidRange); status = status && (ValueSet == _instance.ValueSet); status = status && (value == _instance.value); return status; } /** Setter for the valid range. * * If value is invalid in new range, we throw AssertFailure and set ValueSet to false. * * @param _range range (pair of values) */ template void ContinuousValue::setValidRange(const range &_range) throw(ParameterValueException) { ValidRangeSet = range(true, true); ValidRange = _range; if (ValueSet) { //std::cout << "Checking whether " << value << " is in range " << ValidRange << "." << std::endl; if (!((ValidRange.isInRange(value)) || (value == ValidRange.last))) { //std::cout << "ValueSet to false." << std::endl; ValueSet = false; // have full check again in assert such that it appears in output, too if (!ValidRange.isInRange(value) && (value != ValidRange.last)) throw ParameterValueException() << ParameterValidValues(toString(_range)); } } // LOG(0, "STATUS: Valid range is now " << ValidRange << "."); } /** Getter for the valid range. * * @return _range range (pair of values) */ template const range & ContinuousValue::getValidRange() const throw(ParameterValidatorException) { if (!ValidRangeSet.first || !ValidRangeSet.last) throw ParameterValidatorException(); return ValidRange; } #endif /* CONTINUOUSVALUE_IMPL_HPP_ */