[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"
|
---|
[7d1b6a] | 22 | #include "Range.hpp"
|
---|
[c68409] | 23 |
|
---|
| 24 | #include "ContinuousValue.hpp"
|
---|
| 25 |
|
---|
| 26 | /** Constructor of class DiscreteValue.
|
---|
| 27 | */
|
---|
| 28 | template <class T>
|
---|
| 29 | ContinuousValue<T>::ContinuousValue() :
|
---|
| 30 | ValueSet(false),
|
---|
| 31 | ValidRangeSet(range<bool>(false, false))
|
---|
| 32 | {}
|
---|
| 33 |
|
---|
| 34 | /** Constructor of class DiscreteValue with set of valid values.
|
---|
| 35 | *
|
---|
| 36 | * @param _ValidValues vector with all valid values
|
---|
| 37 | */
|
---|
| 38 | template <class T>
|
---|
| 39 | ContinuousValue<T>::ContinuousValue(const range<T> &_ValidRange) :
|
---|
| 40 | ValueSet(false),
|
---|
| 41 | ValidRangeSet(range<bool>(true, true)),
|
---|
| 42 | ValidRange(_ValidRange)
|
---|
| 43 | {
|
---|
| 44 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /** Destructor of class DiscreteValue.
|
---|
| 48 | */
|
---|
| 49 | template <class T>
|
---|
| 50 | ContinuousValue<T>::~ContinuousValue()
|
---|
| 51 | {}
|
---|
| 52 |
|
---|
| 53 | /** Checks whether \a _value is a valid value.
|
---|
| 54 | * \param _value value to check for validity.
|
---|
| 55 | * \return true - \a _value is valid, false - is not
|
---|
| 56 | */
|
---|
| 57 | template <class T>
|
---|
| 58 | bool ContinuousValue<T>::isValid(const T & _value) const
|
---|
| 59 | {
|
---|
| 60 | return isValidValue(_value);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Compares this continuous value against another \a _instance.
|
---|
| 64 | *
|
---|
| 65 | * @param _instance other value to compare to
|
---|
| 66 | * @return true - if value and valid ranges are the same, false - else
|
---|
| 67 | */
|
---|
| 68 | template <class T>
|
---|
| 69 | bool ContinuousValue<T>::operator==(const ContinuousValue<T> &_instance) const
|
---|
| 70 | {
|
---|
| 71 | bool status = true;
|
---|
| 72 | status = status && (ValidRange == _instance.ValidRange);
|
---|
| 73 | status = status && (ValueSet == _instance.ValueSet);
|
---|
| 74 | if (ValueSet && _instance.ValueSet)
|
---|
| 75 | status = status && (value == _instance.value);
|
---|
| 76 | return status;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /** Getter of value, returning string.
|
---|
| 80 | *
|
---|
| 81 | * @return string value
|
---|
| 82 | */
|
---|
| 83 | template <class T>
|
---|
[dbb533] | 84 | const T & ContinuousValue<T>::get() const
|
---|
[c68409] | 85 | {
|
---|
| 86 | ASSERT(ValueSet,
|
---|
| 87 | "ContinuousValue<T>::get() - requesting unset value.");
|
---|
[dbb533] | 88 | return value;
|
---|
[c68409] | 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Setter of value for string
|
---|
| 92 | *
|
---|
| 93 | * @param _value string containing new value
|
---|
| 94 | */
|
---|
| 95 | template <class T>
|
---|
[dbb533] | 96 | void ContinuousValue<T>::set(const T & _value)
|
---|
[c68409] | 97 | {
|
---|
[dbb533] | 98 | setValue(_value);
|
---|
[c68409] | 99 | // LOG(0, "STATUS: Value is now set to " << value << ".");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Setter for the valid range.
|
---|
| 103 | *
|
---|
| 104 | * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
|
---|
| 105 | *
|
---|
| 106 | * @param _range range (pair of values)
|
---|
| 107 | */
|
---|
| 108 | template <class T>
|
---|
| 109 | void ContinuousValue<T>::setValidRange(const range<T> &_range)
|
---|
| 110 | {
|
---|
| 111 |
|
---|
| 112 | ValidRangeSet = range<bool>(true, true);
|
---|
| 113 | ValidRange = _range;
|
---|
| 114 | if (ValueSet) {
|
---|
| 115 | //std::cout << "Checking whether " << value << " is in range " << ValidRange << "." << std::endl;
|
---|
| 116 | if (!((ValidRange.isInRange(value)) || (value == ValidRange.last))) {
|
---|
| 117 | //std::cout << "ValueSet to false." << std::endl;
|
---|
| 118 | ValueSet = false;
|
---|
| 119 | // have full check again in assert such that it appears in output, too
|
---|
| 120 | ASSERT(ValidRange.isInRange(value) || (value == ValidRange.last),
|
---|
| 121 | "ContinuousValue<T>::setValidRange() - new range "
|
---|
| 122 | +toString(_range)+" invalidates current value "+toString(value)+".");
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /** Getter for the valid range.
|
---|
| 129 | *
|
---|
| 130 | * @return _range range (pair of values)
|
---|
| 131 | */
|
---|
| 132 | template <class T>
|
---|
| 133 | const range<T> & ContinuousValue<T>::getValidRange() const
|
---|
| 134 | {
|
---|
| 135 | ASSERT(ValidRangeSet.first && ValidRangeSet.last,
|
---|
| 136 | "ContinuousValue<T>::getValidRange() called though no valid range set so far.");
|
---|
| 137 | return ValidRange;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /** Checks whether \a _value is a valid value.
|
---|
| 141 | * \param _value value to check for validity.
|
---|
| 142 | * \return true - \a _value is valid, false - is not
|
---|
| 143 | */
|
---|
| 144 | template <class T>
|
---|
| 145 | bool ContinuousValue<T>::isValidValue(const T &_value) const
|
---|
| 146 | {
|
---|
| 147 | bool isBefore = true;
|
---|
| 148 | bool isBeyond = true;
|
---|
| 149 | // check left boundary
|
---|
| 150 | isBefore = !((!ValidRangeSet.first) || (!ValidRange.isBefore(_value)));
|
---|
| 151 | // if (isBefore)
|
---|
| 152 | // LOG(0, "INFO: " << _value << " is before " << ValidRange.first << ".");
|
---|
| 153 | // check right boundary
|
---|
| 154 | isBeyond = !((!ValidRangeSet.last) || (!ValidRange.isBeyond(_value)) || (_value == ValidRange.last));
|
---|
| 155 | // if (isBeyond)
|
---|
| 156 | // LOG(0, "INFO: " << _value << " is beyond " << ValidRange.last << ".");
|
---|
| 157 | return (!isBefore) && (!isBeyond);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | /** Sets the value.
|
---|
| 162 | *
|
---|
| 163 | * We check for its validity, otherwise we throw an Assert::AssertionFailure.
|
---|
| 164 | *
|
---|
| 165 | * @param _value const reference of value to set
|
---|
| 166 | */
|
---|
| 167 | template <class T>
|
---|
| 168 | void ContinuousValue<T>::setValue(const T &_value)
|
---|
| 169 | {
|
---|
| 170 | ASSERT(isValidValue(_value),
|
---|
| 171 | "ContinuousValue<T>::setValue() - trying to set invalid value "+toString(_value)+".");
|
---|
| 172 | if (!ValueSet)
|
---|
| 173 | ValueSet = true;
|
---|
| 174 | value = _value;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[992bd5] | 177 | /** Getter for the set value.
|
---|
[c68409] | 178 | *
|
---|
| 179 | * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
|
---|
| 180 | *
|
---|
| 181 | * @return set value
|
---|
| 182 | */
|
---|
| 183 | template <class T>
|
---|
| 184 | const T &ContinuousValue<T>::getValue() const
|
---|
| 185 | {
|
---|
| 186 | ASSERT(ValueSet,
|
---|
| 187 | "ContinuousValue<T>::get() - value has never been set.");
|
---|
| 188 | return value;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | #endif /* CONTINUOUSVALUE_IMPL_HPP_ */
|
---|