/* * DiscreteValidator_impl.hpp * * Created on: Apr 16, 2012 * Author: ankele */ #ifndef DISCRETEVALIDATOR_IMPL_HPP_ #define DISCRETEVALIDATOR_IMPL_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Parameters/ParameterExceptions.hpp" #include template bool DiscreteValidator::isValid(const T & _value) const { typename std::vector::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value); if (iter != ValidValues.end()) { //std::cout << "Found " << _value << ":" << *iter << std::endl; return true; } else { //std::cout << "Did not find " << _value << "." << std::endl; return false; } } template Validator* DiscreteValidator::clone() const { return new DiscreteValidator(ValidValues); }; template bool DiscreteValidator::operator==(const Validator &_instance) const { const DiscreteValidator *inst = dynamic_cast *>(&_instance); if (inst) return ValidValues == inst->ValidValues; return false; }; template void DiscreteValidator::appendValidValue(const T &_value) throw(ParameterValidatorException) { if (isValid(_value)) throw ParameterValidatorException(); ValidValues.push_back(_value); } template const std::vector &DiscreteValidator::getValidValues() const { return ValidValues; } /** Internal function for finding the index of a desired value. * * \note As this is internal, we do not ASSERT value's presence, but return -1 * such that other functions may ASSERT on that. * * \param _value value to get the index of * \return index such that ValidValues[index] == _value */ template const size_t DiscreteValidator::findIndexOfValue(const T &_value) const { size_t index = 0; const size_t max = ValidValues.size(); for (; index < max; ++index) { if (ValidValues[index] == _value) break; } if (index == max) return (size_t)-1; else return index; } #endif /* DISCRETEVALIDATOR_IMPL_HPP_ */