/* * DiscreteValidator.hpp * * Created on: Apr 13, 2012 * Author: ankele */ #ifndef DISCRETEVALIDATOR_HPP_ #define DISCRETEVALIDATOR_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Validator.hpp" #include /** A validator with a discrete list of valid values. * */ template class DiscreteValidator : public Validator { public: DiscreteValidator(const std::vector &_ValidValues) : ValidValues(_ValidValues) {}; virtual ~DiscreteValidator() {}; bool 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; } } Validator* clone() const { return new DiscreteValidator(ValidValues); }; // comparator bool operator==(const Validator &_instance) const { const DiscreteValidator *inst = dynamic_cast *>(&_instance); if (inst) return ValidValues == inst->ValidValues; return false; }; void appendValidValue(const T &_value) { ASSERT(!isValid(_value), "DiscreteValidator<>::appendValidValue() - value "+toString(_value)+" is already among the valid"); ValidValues.push_back(_value); } private: //!> list of valid values std::vector ValidValues; }; #endif /* DISCRETEVALIDATOR_HPP_ */