[c68409] | 1 | /*
|
---|
| 2 | * ContinuousValue_impl.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Sep 29, 2011
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef CONTINUOUSVALUE_IMPL_HPP_
|
---|
| 9 | #define CONTINUOUSVALUE_IMPL_HPP_
|
---|
| 10 |
|
---|
| 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
| 16 | #include <string>
|
---|
| 17 |
|
---|
| 18 | #include <boost/any.hpp>
|
---|
| 19 |
|
---|
| 20 | #include "CodePatterns/Assert.hpp"
|
---|
| 21 | #include "CodePatterns/Log.hpp"
|
---|
[3c5ef5] | 22 | #include "CodePatterns/Range.hpp"
|
---|
[c68409] | 23 |
|
---|
| 24 | #include "ContinuousValue.hpp"
|
---|
| 25 |
|
---|
[e45c1d] | 26 | #include "ParameterExceptions.hpp"
|
---|
| 27 |
|
---|
[c68409] | 28 | /** Constructor of class DiscreteValue.
|
---|
| 29 | */
|
---|
| 30 | template <class T>
|
---|
| 31 | ContinuousValue<T>::ContinuousValue() :
|
---|
| 32 | ValueSet(false),
|
---|
| 33 | ValidRangeSet(range<bool>(false, false))
|
---|
| 34 | {}
|
---|
| 35 |
|
---|
| 36 | /** Constructor of class DiscreteValue with set of valid values.
|
---|
| 37 | *
|
---|
| 38 | * @param _ValidValues vector with all valid values
|
---|
| 39 | */
|
---|
| 40 | template <class T>
|
---|
| 41 | ContinuousValue<T>::ContinuousValue(const range<T> &_ValidRange) :
|
---|
| 42 | ValueSet(false),
|
---|
| 43 | ValidRangeSet(range<bool>(true, true)),
|
---|
| 44 | ValidRange(_ValidRange)
|
---|
| 45 | {
|
---|
| 46 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /** Destructor of class DiscreteValue.
|
---|
| 50 | */
|
---|
| 51 | template <class T>
|
---|
| 52 | ContinuousValue<T>::~ContinuousValue()
|
---|
| 53 | {}
|
---|
| 54 |
|
---|
| 55 | /** Checks whether \a _value is a valid value.
|
---|
| 56 | * \param _value value to check for validity.
|
---|
| 57 | * \return true - \a _value is valid, false - is not
|
---|
| 58 | */
|
---|
| 59 | template <class T>
|
---|
[e45c1d] | 60 | bool ContinuousValue<T>::isValid(const T & _value) const throw(ParameterValidatorException)
|
---|
[c68409] | 61 | {
|
---|
[047cad] | 62 | bool isBefore = true;
|
---|
| 63 | bool isBeyond = true;
|
---|
| 64 | // check left boundary
|
---|
[e45c1d] | 65 | if ((!ValidRangeSet.first) || (!ValidRangeSet.last))
|
---|
| 66 | throw ParameterValidatorException();
|
---|
| 67 | isBefore = !((!ValidRange.isBefore(_value)));
|
---|
[047cad] | 68 | // if (isBefore)
|
---|
| 69 | // LOG(0, "INFO: " << _value << " is before " << ValidRange.first << ".");
|
---|
| 70 | // check right boundary
|
---|
[e45c1d] | 71 | isBeyond = !((!ValidRange.isBeyond(_value)) || (_value == ValidRange.last));
|
---|
[047cad] | 72 | // if (isBeyond)
|
---|
| 73 | // LOG(0, "INFO: " << _value << " is beyond " << ValidRange.last << ".");
|
---|
| 74 | return (!isBefore) && (!isBeyond);
|
---|
[c68409] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /** Compares this continuous value against another \a _instance.
|
---|
| 78 | *
|
---|
| 79 | * @param _instance other value to compare to
|
---|
| 80 | * @return true - if value and valid ranges are the same, false - else
|
---|
| 81 | */
|
---|
| 82 | template <class T>
|
---|
[e45c1d] | 83 | bool ContinuousValue<T>::operator==(const ContinuousValue<T> &_instance) const const throw(ParameterException)
|
---|
[c68409] | 84 | {
|
---|
| 85 | bool status = true;
|
---|
[e45c1d] | 86 | if ((!ValueSet) || (!_instance.ValueSet))
|
---|
| 87 | throw ParameterValueException();
|
---|
| 88 | if ((!ValidRangeSet.first) || ((!ValidRangeSet.first))
|
---|
| 89 | || (!_instance.ValidRangeSet.first) || ((!_instance.ValidRangeSet.first)))
|
---|
| 90 | throw ParameterValidatorException();
|
---|
[c68409] | 91 | status = status && (ValidRange == _instance.ValidRange);
|
---|
| 92 | status = status && (ValueSet == _instance.ValueSet);
|
---|
[e45c1d] | 93 | status = status && (value == _instance.value);
|
---|
[c68409] | 94 | return status;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Setter for the valid range.
|
---|
| 98 | *
|
---|
| 99 | * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
|
---|
| 100 | *
|
---|
| 101 | * @param _range range (pair of values)
|
---|
| 102 | */
|
---|
| 103 | template <class T>
|
---|
[e45c1d] | 104 | void ContinuousValue<T>::setValidRange(const range<T> &_range) throw(ParameterValueException)
|
---|
[c68409] | 105 | {
|
---|
| 106 | ValidRangeSet = range<bool>(true, true);
|
---|
| 107 | ValidRange = _range;
|
---|
| 108 | if (ValueSet) {
|
---|
| 109 | //std::cout << "Checking whether " << value << " is in range " << ValidRange << "." << std::endl;
|
---|
| 110 | if (!((ValidRange.isInRange(value)) || (value == ValidRange.last))) {
|
---|
| 111 | //std::cout << "ValueSet to false." << std::endl;
|
---|
| 112 | ValueSet = false;
|
---|
| 113 | // have full check again in assert such that it appears in output, too
|
---|
[e45c1d] | 114 | if (!ValidRange.isInRange(value) && (value != ValidRange.last))
|
---|
| 115 | throw ParameterValueException() << ParameterValidValues(toString(_range));
|
---|
[c68409] | 116 | }
|
---|
| 117 | }
|
---|
| 118 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /** Getter for the valid range.
|
---|
| 122 | *
|
---|
| 123 | * @return _range range (pair of values)
|
---|
| 124 | */
|
---|
| 125 | template <class T>
|
---|
[e45c1d] | 126 | const range<T> & ContinuousValue<T>::getValidRange() const throw(ParameterValidatorException)
|
---|
[c68409] | 127 | {
|
---|
[e45c1d] | 128 | if (!ValidRangeSet.first || !ValidRangeSet.last)
|
---|
| 129 | throw ParameterValidatorException();
|
---|
[c68409] | 130 | return ValidRange;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | #endif /* CONTINUOUSVALUE_IMPL_HPP_ */
|
---|