[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 |
|
---|
[a7d753] | 62 | /** Getter of value, returning string.
|
---|
| 63 | *
|
---|
| 64 | * @return string value
|
---|
| 65 | */
|
---|
| 66 | template <class T>
|
---|
| 67 | const std::string DiscreteValue<T>::get() const
|
---|
| 68 | {
|
---|
| 69 | ASSERT(ValueSet,
|
---|
| 70 | "DiscreteValue<T>::get() - requesting unset value.");
|
---|
| 71 | return toString(getValue());
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Setter of value for string
|
---|
| 75 | *
|
---|
| 76 | * @param _value string containing new value
|
---|
| 77 | */
|
---|
| 78 | template <class T>
|
---|
| 79 | void DiscreteValue<T>::set(const std::string _value)
|
---|
| 80 | {
|
---|
| 81 | const T castvalue = Converter(_value);
|
---|
| 82 | setValue(castvalue);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
[e09d67] | 86 | /** Internal function for finding the index of a desired value.
|
---|
| 87 | *
|
---|
| 88 | * \note As this is internal, we do not ASSERT value's presence, but return -1
|
---|
| 89 | * such that other functions may ASSERT on that.
|
---|
| 90 | *
|
---|
| 91 | * \param _value value to get the index of
|
---|
| 92 | * \return index such that ValidValues[index] == _value
|
---|
| 93 | */
|
---|
| 94 | template <class T>
|
---|
| 95 | const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
|
---|
| 96 | {
|
---|
| 97 | size_t index = 0;
|
---|
| 98 | const size_t max = ValidValues.size();
|
---|
| 99 | for (; index < max; ++index) {
|
---|
| 100 | if (ValidValues[index] == _value)
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 | if (index == max)
|
---|
| 104 | return (size_t)-1;
|
---|
| 105 | else
|
---|
| 106 | return index;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Adds another value to the valid ones.
|
---|
| 110 | *
|
---|
| 111 | * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
|
---|
| 112 | *
|
---|
| 113 | * @param _value value to add
|
---|
| 114 | */
|
---|
| 115 | template <class T>
|
---|
| 116 | void DiscreteValue<T>::appendValidValue(const T &_value)
|
---|
| 117 | {
|
---|
[a7d753] | 118 | ASSERT(!isValidValue(_value),
|
---|
[e09d67] | 119 | "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
|
---|
| 120 | ValidValues.push_back(_value);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /** Returns all possible valid values.
|
---|
| 124 | *
|
---|
| 125 | * @return vector with all allowed values
|
---|
| 126 | */
|
---|
| 127 | template <class T>
|
---|
| 128 | const std::vector<T> &DiscreteValue<T>::getValidValues() const
|
---|
| 129 | {
|
---|
| 130 | return ValidValues;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /** Sets the value.
|
---|
| 134 | *
|
---|
| 135 | * We check for its validity, otherwise we throw an Assert::AssertionFailure.
|
---|
| 136 | *
|
---|
| 137 | * @param _value const reference of value to set
|
---|
| 138 | */
|
---|
| 139 | template <class T>
|
---|
[a7d753] | 140 | void DiscreteValue<T>::setValue(const T &_value)
|
---|
[e09d67] | 141 | {
|
---|
| 142 | const size_t index = findIndexOfValue(_value);
|
---|
| 143 | ASSERT(index != (size_t)-1,
|
---|
| 144 | "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
|
---|
| 145 | if (!ValueSet)
|
---|
| 146 | ValueSet = true;
|
---|
| 147 | value = index;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[a7d753] | 150 | /** Getter for the set value.
|
---|
[e09d67] | 151 | *
|
---|
| 152 | * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
|
---|
| 153 | *
|
---|
| 154 | * @return set value
|
---|
| 155 | */
|
---|
| 156 | template <class T>
|
---|
[a7d753] | 157 | const T & DiscreteValue<T>::getValue() const
|
---|
[e09d67] | 158 | {
|
---|
| 159 | ASSERT(ValueSet,
|
---|
| 160 | "DiscreteValue<>::get() - value has never been set.");
|
---|
| 161 | return ValidValues[value];
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[a7d753] | 164 | /** Checks whether \a _value is a valid value.
|
---|
| 165 | * \param _value value to check for validity.
|
---|
| 166 | * \return true - \a _value is valid, false - is not
|
---|
| 167 | */
|
---|
| 168 | template <class T>
|
---|
| 169 | bool DiscreteValue<T>::isValidValue(const T &_value) const
|
---|
| 170 | {
|
---|
| 171 | typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
|
---|
| 172 | if (iter != ValidValues.end()) {
|
---|
| 173 | //std::cout << "Found " << _value << ":" << *iter << std::endl;
|
---|
| 174 | return true;
|
---|
| 175 | } else {
|
---|
| 176 | //std::cout << "Did not find " << _value << "." << std::endl;
|
---|
| 177 | return false;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[e09d67] | 180 |
|
---|
| 181 | #endif /* DISCRETEVALUE_IMPL_HPP_ */
|
---|