[e09d67] | 1 | /*
|
---|
| 2 | * DiscreteValues_impl.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Sep 28, 2011
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef DISCRETEVALUE_IMPL_HPP_
|
---|
| 9 | #define DISCRETEVALUE_IMPL_HPP_
|
---|
| 10 |
|
---|
| 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
[a7d753] | 16 | #include <algorithm>
|
---|
[e09d67] | 17 | #include <vector>
|
---|
| 18 |
|
---|
[a7d753] | 19 | #include <boost/any.hpp>
|
---|
| 20 |
|
---|
[e09d67] | 21 | #include "CodePatterns/Assert.hpp"
|
---|
| 22 |
|
---|
[a7d753] | 23 | #include "CodePatterns/Log.hpp"
|
---|
| 24 |
|
---|
| 25 | // static member
|
---|
| 26 | template <class T> ConvertTo<T> DiscreteValue<T>::Converter;
|
---|
| 27 |
|
---|
[e09d67] | 28 | /** Constructor of class DiscreteValue.
|
---|
| 29 | */
|
---|
| 30 | template <class T>
|
---|
| 31 | DiscreteValue<T>::DiscreteValue() :
|
---|
| 32 | ValueSet(false)
|
---|
| 33 | {}
|
---|
| 34 |
|
---|
[a7d753] | 35 | /** Constructor of class DiscreteValue with set of valid values.
|
---|
| 36 | *
|
---|
| 37 | * @param _ValidValues vector with all valid values
|
---|
[e09d67] | 38 | */
|
---|
| 39 | template <class T>
|
---|
| 40 | DiscreteValue<T>::DiscreteValue(const std::vector<T> &_ValidValues) :
|
---|
| 41 | ValueSet(false),
|
---|
| 42 | ValidValues(_ValidValues)
|
---|
| 43 | {}
|
---|
| 44 |
|
---|
| 45 | /** Destructor of class DiscreteValue.
|
---|
| 46 | */
|
---|
| 47 | template <class T>
|
---|
| 48 | DiscreteValue<T>::~DiscreteValue()
|
---|
| 49 | {}
|
---|
| 50 |
|
---|
| 51 | /** Checks whether \a _value is a valid value.
|
---|
| 52 | * \param _value value to check for validity.
|
---|
| 53 | * \return true - \a _value is valid, false - is not
|
---|
| 54 | */
|
---|
| 55 | template <class T>
|
---|
[a7d753] | 56 | bool DiscreteValue<T>::isValid(const std::string _value) const
|
---|
[e09d67] | 57 | {
|
---|
[a7d753] | 58 | const T castvalue = Converter(_value);
|
---|
| 59 | return isValidValue(castvalue);
|
---|
[e09d67] | 60 | }
|
---|
| 61 |
|
---|
[498ddd] | 62 | /** Compares this discrete value against another \a _instance.
|
---|
| 63 | *
|
---|
| 64 | * @param _instance other value to compare to
|
---|
| 65 | * @return true - if value and valid ranges are the same, false - else
|
---|
| 66 | */
|
---|
| 67 | template <class T>
|
---|
| 68 | bool DiscreteValue<T>::operator==(const DiscreteValue<T> &_instance) const
|
---|
| 69 | {
|
---|
| 70 | bool status = true;
|
---|
| 71 | status = status && (ValidValues == _instance.ValidValues);
|
---|
| 72 | status = status && (ValueSet == _instance.ValueSet);
|
---|
| 73 | if (ValueSet && _instance.ValueSet)
|
---|
| 74 | status = status && (value == _instance.value);
|
---|
| 75 | return status;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
[a7d753] | 79 | /** Getter of value, returning string.
|
---|
| 80 | *
|
---|
| 81 | * @return string value
|
---|
| 82 | */
|
---|
| 83 | template <class T>
|
---|
| 84 | const std::string DiscreteValue<T>::get() const
|
---|
| 85 | {
|
---|
| 86 | ASSERT(ValueSet,
|
---|
| 87 | "DiscreteValue<T>::get() - requesting unset value.");
|
---|
| 88 | return toString(getValue());
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Setter of value for string
|
---|
| 92 | *
|
---|
| 93 | * @param _value string containing new value
|
---|
| 94 | */
|
---|
| 95 | template <class T>
|
---|
| 96 | void DiscreteValue<T>::set(const std::string _value)
|
---|
| 97 | {
|
---|
| 98 | const T castvalue = Converter(_value);
|
---|
| 99 | setValue(castvalue);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[e09d67] | 103 | /** Internal function for finding the index of a desired value.
|
---|
| 104 | *
|
---|
| 105 | * \note As this is internal, we do not ASSERT value's presence, but return -1
|
---|
| 106 | * such that other functions may ASSERT on that.
|
---|
| 107 | *
|
---|
| 108 | * \param _value value to get the index of
|
---|
| 109 | * \return index such that ValidValues[index] == _value
|
---|
| 110 | */
|
---|
| 111 | template <class T>
|
---|
| 112 | const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
|
---|
| 113 | {
|
---|
| 114 | size_t index = 0;
|
---|
| 115 | const size_t max = ValidValues.size();
|
---|
| 116 | for (; index < max; ++index) {
|
---|
| 117 | if (ValidValues[index] == _value)
|
---|
| 118 | break;
|
---|
| 119 | }
|
---|
| 120 | if (index == max)
|
---|
| 121 | return (size_t)-1;
|
---|
| 122 | else
|
---|
| 123 | return index;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Adds another value to the valid ones.
|
---|
| 127 | *
|
---|
| 128 | * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
|
---|
| 129 | *
|
---|
| 130 | * @param _value value to add
|
---|
| 131 | */
|
---|
| 132 | template <class T>
|
---|
| 133 | void DiscreteValue<T>::appendValidValue(const T &_value)
|
---|
| 134 | {
|
---|
[a7d753] | 135 | ASSERT(!isValidValue(_value),
|
---|
[e09d67] | 136 | "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
|
---|
| 137 | ValidValues.push_back(_value);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /** Returns all possible valid values.
|
---|
| 141 | *
|
---|
| 142 | * @return vector with all allowed values
|
---|
| 143 | */
|
---|
| 144 | template <class T>
|
---|
| 145 | const std::vector<T> &DiscreteValue<T>::getValidValues() const
|
---|
| 146 | {
|
---|
| 147 | return ValidValues;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** Sets the value.
|
---|
| 151 | *
|
---|
| 152 | * We check for its validity, otherwise we throw an Assert::AssertionFailure.
|
---|
| 153 | *
|
---|
| 154 | * @param _value const reference of value to set
|
---|
| 155 | */
|
---|
| 156 | template <class T>
|
---|
[a7d753] | 157 | void DiscreteValue<T>::setValue(const T &_value)
|
---|
[e09d67] | 158 | {
|
---|
| 159 | const size_t index = findIndexOfValue(_value);
|
---|
| 160 | ASSERT(index != (size_t)-1,
|
---|
| 161 | "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
|
---|
| 162 | if (!ValueSet)
|
---|
| 163 | ValueSet = true;
|
---|
| 164 | value = index;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[a7d753] | 167 | /** Getter for the set value.
|
---|
[e09d67] | 168 | *
|
---|
| 169 | * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
|
---|
| 170 | *
|
---|
| 171 | * @return set value
|
---|
| 172 | */
|
---|
| 173 | template <class T>
|
---|
[a7d753] | 174 | const T & DiscreteValue<T>::getValue() const
|
---|
[e09d67] | 175 | {
|
---|
| 176 | ASSERT(ValueSet,
|
---|
| 177 | "DiscreteValue<>::get() - value has never been set.");
|
---|
| 178 | return ValidValues[value];
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[a7d753] | 181 | /** Checks whether \a _value is a valid value.
|
---|
| 182 | * \param _value value to check for validity.
|
---|
| 183 | * \return true - \a _value is valid, false - is not
|
---|
| 184 | */
|
---|
| 185 | template <class T>
|
---|
| 186 | bool DiscreteValue<T>::isValidValue(const T &_value) const
|
---|
| 187 | {
|
---|
| 188 | typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
|
---|
| 189 | if (iter != ValidValues.end()) {
|
---|
| 190 | //std::cout << "Found " << _value << ":" << *iter << std::endl;
|
---|
| 191 | return true;
|
---|
| 192 | } else {
|
---|
| 193 | //std::cout << "Did not find " << _value << "." << std::endl;
|
---|
| 194 | return false;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
[e09d67] | 197 |
|
---|
| 198 | #endif /* DISCRETEVALUE_IMPL_HPP_ */
|
---|