1 | /*
|
---|
2 | * Value_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Apr 13, 2012
|
---|
5 | * Author: ankele
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef VALUE_IMPL_HPP_
|
---|
9 | #define VALUE_IMPL_HPP_
|
---|
10 |
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 |
|
---|
18 | #include <boost/any.hpp>
|
---|
19 |
|
---|
20 | #include "CodePatterns/Assert.hpp"
|
---|
21 |
|
---|
22 | #include "CodePatterns/Log.hpp"
|
---|
23 |
|
---|
24 | #include "Validators/DiscreteValidator.hpp"
|
---|
25 | #include "Validators/RangeValidator.hpp"
|
---|
26 |
|
---|
27 | /** Constructor of class Value.
|
---|
28 | */
|
---|
29 | template <class T>
|
---|
30 | Value<T>::Value() :
|
---|
31 | ValueSet(false),
|
---|
32 | validator(NULL)
|
---|
33 | {}
|
---|
34 |
|
---|
35 | /** Constructor of class Value with a validator.
|
---|
36 | *
|
---|
37 | * @param _validator general validator to use
|
---|
38 | */
|
---|
39 | template <class T>
|
---|
40 | Value<T>::Value(const Validator<T> &_validator) :
|
---|
41 | ValueSet(false),
|
---|
42 | validator(_validator.clone())
|
---|
43 | {}
|
---|
44 |
|
---|
45 | /** Constructor of class Value with a discrete validator.
|
---|
46 | *
|
---|
47 | * @param _ValidValues vector with all valid values
|
---|
48 | */
|
---|
49 | template <class T>
|
---|
50 | Value<T>::Value(const std::vector<T> &_ValidValues) :
|
---|
51 | ValueSet(false),
|
---|
52 | validator(NULL)
|
---|
53 | {
|
---|
54 | validator = new DiscreteValidator<T>(_ValidValues);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Constructor of class Value with a range validator.
|
---|
58 | *
|
---|
59 | * @param _ValidRange range of valid values
|
---|
60 | */
|
---|
61 | template <class T>
|
---|
62 | Value<T>::Value(const range<T> &_ValidRange) :
|
---|
63 | ValueSet(false),
|
---|
64 | validator(NULL)
|
---|
65 | {
|
---|
66 | validator = new RangeValidator<T>(_ValidRange);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Destructor of class Value.
|
---|
70 | */
|
---|
71 | template <class T>
|
---|
72 | Value<T>::~Value()
|
---|
73 | {
|
---|
74 | if (validator)
|
---|
75 | delete(validator);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Checks whether \a _value is a valid value.
|
---|
79 | * \param _value value to check for validity.
|
---|
80 | * \return true - \a _value is valid, false - is not
|
---|
81 | */
|
---|
82 | template <class T>
|
---|
83 | bool Value<T>::isValid(const T & _value) const
|
---|
84 | {
|
---|
85 | if (validator)
|
---|
86 | return (*validator)(_value);
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Compares this discrete value against another \a _instance.
|
---|
91 | *
|
---|
92 | * @param _instance other value to compare to
|
---|
93 | * @return true - if value and valid ranges are the same, false - else
|
---|
94 | */
|
---|
95 | template <class T>
|
---|
96 | bool Value<T>::operator==(const Value<T> &_instance) const
|
---|
97 | {
|
---|
98 | bool status = true;
|
---|
99 | status = status && (*validator == *_instance.validator);
|
---|
100 | status = status && (ValueSet == _instance.ValueSet);
|
---|
101 | if (ValueSet && _instance.ValueSet)
|
---|
102 | status = status && (value == _instance.value);
|
---|
103 | return status;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /** Getter of value
|
---|
108 | *
|
---|
109 | * @return value
|
---|
110 | */
|
---|
111 | template <class T>
|
---|
112 | const T & Value<T>::get() const
|
---|
113 | {
|
---|
114 | ASSERT(ValueSet,
|
---|
115 | "Value<T>::get() - value has never been set.");
|
---|
116 | return value;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Setter of value
|
---|
120 | *
|
---|
121 | * @param _value new value
|
---|
122 | */
|
---|
123 | template <class T>
|
---|
124 | void Value<T>::set(const T & _value)
|
---|
125 | {
|
---|
126 | ASSERT(isValid(_value),
|
---|
127 | "Value<T>::setValue() - trying to set invalid value "+toString(_value)+".");
|
---|
128 | if (!ValueSet)
|
---|
129 | ValueSet = true;
|
---|
130 | value = _value;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Returns the validator as a const reference.
|
---|
134 | *
|
---|
135 | * @return the validator
|
---|
136 | */
|
---|
137 | template <class T>
|
---|
138 | const Validator<T> &Value<T>::getValidator() const
|
---|
139 | {
|
---|
140 | return *validator;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Returns the validator.
|
---|
144 | *
|
---|
145 | * @return the validator
|
---|
146 | */
|
---|
147 | template <class T>
|
---|
148 | Validator<T> &Value<T>::getValidator()
|
---|
149 | {
|
---|
150 | return *validator;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 | #endif /* VALUE_IMPL_HPP_ */
|
---|