| 1 | /* | 
|---|
| 2 | * DiscreteValidator_impl.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Apr 16, 2012 | 
|---|
| 5 | *      Author: ankele | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef DISCRETEVALIDATOR_IMPL_HPP_ | 
|---|
| 9 | #define DISCRETEVALIDATOR_IMPL_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | // include config.h | 
|---|
| 13 | #ifdef HAVE_CONFIG_H | 
|---|
| 14 | #include <config.h> | 
|---|
| 15 | #endif | 
|---|
| 16 |  | 
|---|
| 17 | template <class T> | 
|---|
| 18 | bool DiscreteValidator<T>::isValid(const T & _value) const | 
|---|
| 19 | { | 
|---|
| 20 | typename std::vector<T>::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value); | 
|---|
| 21 | if (iter != ValidValues.end()) { | 
|---|
| 22 | //std::cout << "Found " << _value << ":" << *iter << std::endl; | 
|---|
| 23 | return true; | 
|---|
| 24 | } else { | 
|---|
| 25 | //std::cout << "Did not find " << _value << "." << std::endl; | 
|---|
| 26 | return false; | 
|---|
| 27 | } | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | template <class T> | 
|---|
| 31 | Validator<T>* DiscreteValidator<T>::clone() const | 
|---|
| 32 | { | 
|---|
| 33 | return new DiscreteValidator<T>(ValidValues); | 
|---|
| 34 | }; | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | template <class T> | 
|---|
| 38 | bool DiscreteValidator<T>::operator==(const Validator<T> &_instance) const | 
|---|
| 39 | { | 
|---|
| 40 | const DiscreteValidator<T> *inst = dynamic_cast<const DiscreteValidator<T> *>(&_instance); | 
|---|
| 41 | if (inst) | 
|---|
| 42 | return ValidValues == inst->ValidValues; | 
|---|
| 43 | return false; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | template <class T> | 
|---|
| 47 | void DiscreteValidator<T>::appendValidValue(const T &_value) | 
|---|
| 48 | { | 
|---|
| 49 | ASSERT(!isValid(_value), | 
|---|
| 50 | "DiscreteValidator<>::appendValidValue() - value "+toString(_value)+" is already among the valid"); | 
|---|
| 51 | ValidValues.push_back(_value); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | template <class T> | 
|---|
| 56 | const std::vector<T> &DiscreteValidator<T>::getValidValues() const | 
|---|
| 57 | { | 
|---|
| 58 | return ValidValues; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | /** Internal function for finding the index of a desired value. | 
|---|
| 62 | * | 
|---|
| 63 | * \note As this is internal, we do not ASSERT value's presence, but return -1 | 
|---|
| 64 | * such that other functions may ASSERT on that. | 
|---|
| 65 | * | 
|---|
| 66 | * \param _value value to get the index of | 
|---|
| 67 | * \return index such that ValidValues[index] == _value | 
|---|
| 68 | */ | 
|---|
| 69 | template <class T> | 
|---|
| 70 | const size_t DiscreteValidator<T>::findIndexOfValue(const T &_value) const | 
|---|
| 71 | { | 
|---|
| 72 | size_t index = 0; | 
|---|
| 73 | const size_t max = ValidValues.size(); | 
|---|
| 74 | for (; index < max; ++index) { | 
|---|
| 75 | if (ValidValues[index] == _value) | 
|---|
| 76 | break; | 
|---|
| 77 | } | 
|---|
| 78 | if (index == max) | 
|---|
| 79 | return (size_t)-1; | 
|---|
| 80 | else | 
|---|
| 81 | return index; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | #endif /* DISCRETEVALIDATOR_IMPL_HPP_ */ | 
|---|