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