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