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 <vector>
|
---|
17 |
|
---|
18 | #include "CodePatterns/Assert.hpp"
|
---|
19 |
|
---|
20 | /** Constructor of class DiscreteValue.
|
---|
21 | */
|
---|
22 | template <class T>
|
---|
23 | DiscreteValue<T>::DiscreteValue() :
|
---|
24 | ValueSet(false)
|
---|
25 | {}
|
---|
26 |
|
---|
27 | /** Constructor of class DiscreteValue.
|
---|
28 | */
|
---|
29 | template <class T>
|
---|
30 | DiscreteValue<T>::DiscreteValue(const std::vector<T> &_ValidValues) :
|
---|
31 | ValueSet(false),
|
---|
32 | ValidValues(_ValidValues)
|
---|
33 | {}
|
---|
34 |
|
---|
35 | /** Destructor of class DiscreteValue.
|
---|
36 | */
|
---|
37 | template <class T>
|
---|
38 | DiscreteValue<T>::~DiscreteValue()
|
---|
39 | {}
|
---|
40 |
|
---|
41 | /** Checks whether \a _value is a valid value.
|
---|
42 | * \param _value value to check for validity.
|
---|
43 | * \return true - \a _value is valid, false - is not
|
---|
44 | */
|
---|
45 | template <class T>
|
---|
46 | bool DiscreteValue<T>::isValid(const T &_value) const
|
---|
47 | {
|
---|
48 | const size_t index = findIndexOfValue(_value);
|
---|
49 | if (index == (size_t)-1)
|
---|
50 | return false;
|
---|
51 | else
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Internal function for finding the index of a desired value.
|
---|
56 | *
|
---|
57 | * \note As this is internal, we do not ASSERT value's presence, but return -1
|
---|
58 | * such that other functions may ASSERT on that.
|
---|
59 | *
|
---|
60 | * \param _value value to get the index of
|
---|
61 | * \return index such that ValidValues[index] == _value
|
---|
62 | */
|
---|
63 | template <class T>
|
---|
64 | const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
|
---|
65 | {
|
---|
66 | size_t index = 0;
|
---|
67 | const size_t max = ValidValues.size();
|
---|
68 | for (; index < max; ++index) {
|
---|
69 | if (ValidValues[index] == _value)
|
---|
70 | break;
|
---|
71 | }
|
---|
72 | if (index == max)
|
---|
73 | return (size_t)-1;
|
---|
74 | else
|
---|
75 | return index;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Adds another value to the valid ones.
|
---|
79 | *
|
---|
80 | * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
|
---|
81 | *
|
---|
82 | * @param _value value to add
|
---|
83 | */
|
---|
84 | template <class T>
|
---|
85 | void DiscreteValue<T>::appendValidValue(const T &_value)
|
---|
86 | {
|
---|
87 | ASSERT(!isValid(_value),
|
---|
88 | "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
|
---|
89 | ValidValues.push_back(_value);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Returns all possible valid values.
|
---|
93 | *
|
---|
94 | * @return vector with all allowed values
|
---|
95 | */
|
---|
96 | template <class T>
|
---|
97 | const std::vector<T> &DiscreteValue<T>::getValidValues() const
|
---|
98 | {
|
---|
99 | return ValidValues;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Sets the value.
|
---|
103 | *
|
---|
104 | * We check for its validity, otherwise we throw an Assert::AssertionFailure.
|
---|
105 | *
|
---|
106 | * @param _value const reference of value to set
|
---|
107 | */
|
---|
108 | template <class T>
|
---|
109 | void DiscreteValue<T>::set(const T &_value)
|
---|
110 | {
|
---|
111 | const size_t index = findIndexOfValue(_value);
|
---|
112 | ASSERT(index != (size_t)-1,
|
---|
113 | "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
|
---|
114 | if (!ValueSet)
|
---|
115 | ValueSet = true;
|
---|
116 | value = index;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Getter fot the set value.
|
---|
120 | *
|
---|
121 | * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
|
---|
122 | *
|
---|
123 | * @return set value
|
---|
124 | */
|
---|
125 | template <class T>
|
---|
126 | const T & DiscreteValue<T>::get() const
|
---|
127 | {
|
---|
128 | ASSERT(ValueSet,
|
---|
129 | "DiscreteValue<>::get() - value has never been set.");
|
---|
130 | return ValidValues[value];
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | #endif /* DISCRETEVALUE_IMPL_HPP_ */
|
---|