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