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