[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 | {
|
---|
[047cad] | 60 | bool isBefore = true;
|
---|
| 61 | bool isBeyond = true;
|
---|
| 62 | // check left boundary
|
---|
| 63 | isBefore = !((!ValidRangeSet.first) || (!ValidRange.isBefore(_value)));
|
---|
| 64 | // if (isBefore)
|
---|
| 65 | // LOG(0, "INFO: " << _value << " is before " << ValidRange.first << ".");
|
---|
| 66 | // check right boundary
|
---|
| 67 | isBeyond = !((!ValidRangeSet.last) || (!ValidRange.isBeyond(_value)) || (_value == ValidRange.last));
|
---|
| 68 | // if (isBeyond)
|
---|
| 69 | // LOG(0, "INFO: " << _value << " is beyond " << ValidRange.last << ".");
|
---|
| 70 | return (!isBefore) && (!isBeyond);
|
---|
[c68409] | 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Compares this continuous value against another \a _instance.
|
---|
| 74 | *
|
---|
| 75 | * @param _instance other value to compare to
|
---|
| 76 | * @return true - if value and valid ranges are the same, false - else
|
---|
| 77 | */
|
---|
| 78 | template <class T>
|
---|
| 79 | bool ContinuousValue<T>::operator==(const ContinuousValue<T> &_instance) const
|
---|
| 80 | {
|
---|
| 81 | bool status = true;
|
---|
| 82 | status = status && (ValidRange == _instance.ValidRange);
|
---|
| 83 | status = status && (ValueSet == _instance.ValueSet);
|
---|
| 84 | if (ValueSet && _instance.ValueSet)
|
---|
| 85 | status = status && (value == _instance.value);
|
---|
| 86 | return status;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Getter of value, returning string.
|
---|
| 90 | *
|
---|
| 91 | * @return string value
|
---|
| 92 | */
|
---|
| 93 | template <class T>
|
---|
[dbb533] | 94 | const T & ContinuousValue<T>::get() const
|
---|
[c68409] | 95 | {
|
---|
| 96 | ASSERT(ValueSet,
|
---|
| 97 | "ContinuousValue<T>::get() - requesting unset value.");
|
---|
[dbb533] | 98 | return value;
|
---|
[c68409] | 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Setter of value for string
|
---|
| 102 | *
|
---|
| 103 | * @param _value string containing new value
|
---|
| 104 | */
|
---|
| 105 | template <class T>
|
---|
[dbb533] | 106 | void ContinuousValue<T>::set(const T & _value)
|
---|
[c68409] | 107 | {
|
---|
[047cad] | 108 | ASSERT(isValid(_value),
|
---|
| 109 | "ContinuousValue<T>::setValue() - trying to set invalid value "+toString(_value)+".");
|
---|
| 110 | if (!ValueSet)
|
---|
| 111 | ValueSet = true;
|
---|
| 112 | value = _value;
|
---|
[c68409] | 113 | // LOG(0, "STATUS: Value is now set to " << value << ".");
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Setter for the valid range.
|
---|
| 117 | *
|
---|
| 118 | * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
|
---|
| 119 | *
|
---|
| 120 | * @param _range range (pair of values)
|
---|
| 121 | */
|
---|
| 122 | template <class T>
|
---|
| 123 | void ContinuousValue<T>::setValidRange(const range<T> &_range)
|
---|
| 124 | {
|
---|
| 125 |
|
---|
| 126 | ValidRangeSet = range<bool>(true, true);
|
---|
| 127 | ValidRange = _range;
|
---|
| 128 | if (ValueSet) {
|
---|
| 129 | //std::cout << "Checking whether " << value << " is in range " << ValidRange << "." << std::endl;
|
---|
| 130 | if (!((ValidRange.isInRange(value)) || (value == ValidRange.last))) {
|
---|
| 131 | //std::cout << "ValueSet to false." << std::endl;
|
---|
| 132 | ValueSet = false;
|
---|
| 133 | // have full check again in assert such that it appears in output, too
|
---|
| 134 | ASSERT(ValidRange.isInRange(value) || (value == ValidRange.last),
|
---|
| 135 | "ContinuousValue<T>::setValidRange() - new range "
|
---|
| 136 | +toString(_range)+" invalidates current value "+toString(value)+".");
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** Getter for the valid range.
|
---|
| 143 | *
|
---|
| 144 | * @return _range range (pair of values)
|
---|
| 145 | */
|
---|
| 146 | template <class T>
|
---|
| 147 | const range<T> & ContinuousValue<T>::getValidRange() const
|
---|
| 148 | {
|
---|
| 149 | ASSERT(ValidRangeSet.first && ValidRangeSet.last,
|
---|
| 150 | "ContinuousValue<T>::getValidRange() called though no valid range set so far.");
|
---|
| 151 | return ValidRange;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Checks whether \a _value is a valid value.
|
---|
| 155 | * \param _value value to check for validity.
|
---|
| 156 | * \return true - \a _value is valid, false - is not
|
---|
| 157 | */
|
---|
| 158 | template <class T>
|
---|
[047cad] | 159 | bool ContinuousValue<T>::isValidAsString(const std::string _value) const
|
---|
[c68409] | 160 | {
|
---|
[047cad] | 161 | /*bool isBefore = true;
|
---|
[c68409] | 162 | bool isBeyond = true;
|
---|
| 163 | // check left boundary
|
---|
| 164 | isBefore = !((!ValidRangeSet.first) || (!ValidRange.isBefore(_value)));
|
---|
| 165 | // if (isBefore)
|
---|
| 166 | // LOG(0, "INFO: " << _value << " is before " << ValidRange.first << ".");
|
---|
| 167 | // check right boundary
|
---|
| 168 | isBeyond = !((!ValidRangeSet.last) || (!ValidRange.isBeyond(_value)) || (_value == ValidRange.last));
|
---|
| 169 | // if (isBeyond)
|
---|
| 170 | // LOG(0, "INFO: " << _value << " is beyond " << ValidRange.last << ".");
|
---|
[047cad] | 171 | return (!isBefore) && (!isBeyond);*/
|
---|
| 172 | return true;
|
---|
[c68409] | 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | /** Sets the value.
|
---|
| 177 | *
|
---|
| 178 | * We check for its validity, otherwise we throw an Assert::AssertionFailure.
|
---|
| 179 | *
|
---|
| 180 | * @param _value const reference of value to set
|
---|
| 181 | */
|
---|
| 182 | template <class T>
|
---|
[047cad] | 183 | void ContinuousValue<T>::setAsString(const std::string _value)
|
---|
[c68409] | 184 | {
|
---|
[047cad] | 185 | /*ASSERT(isValidAsString(_value),
|
---|
[c68409] | 186 | "ContinuousValue<T>::setValue() - trying to set invalid value "+toString(_value)+".");
|
---|
| 187 | if (!ValueSet)
|
---|
| 188 | ValueSet = true;
|
---|
[047cad] | 189 | value = _value;*/
|
---|
[c68409] | 190 | }
|
---|
| 191 |
|
---|
[992bd5] | 192 | /** Getter for the set value.
|
---|
[c68409] | 193 | *
|
---|
| 194 | * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
|
---|
| 195 | *
|
---|
| 196 | * @return set value
|
---|
| 197 | */
|
---|
| 198 | template <class T>
|
---|
[047cad] | 199 | const std::string ContinuousValue<T>::getAsString() const
|
---|
[c68409] | 200 | {
|
---|
| 201 | ASSERT(ValueSet,
|
---|
| 202 | "ContinuousValue<T>::get() - value has never been set.");
|
---|
[047cad] | 203 | return toString(value);
|
---|
[c68409] | 204 | }
|
---|
| 205 |
|
---|
| 206 | #endif /* CONTINUOUSVALUE_IMPL_HPP_ */
|
---|