| 1 | /*
 | 
|---|
| 2 |  * STLVectorValidator.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 9, 2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef STLVECTORVALIDATOR_HPP_
 | 
|---|
| 9 | #define STLVECTORVALIDATOR_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include "DummyValidator.hpp"
 | 
|---|
| 18 | #include "RangeValidator.hpp"
 | 
|---|
| 19 | #include "Validator.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include <vector>
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "CodePatterns/Range.hpp"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /** This class is a combination of \ref Validator<size_t> on the number of entries in
 | 
|---|
| 26 |  * the vector while each element is checked with a given Validator<value_type>.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | template <class T>
 | 
|---|
| 29 | class STLVectorValidator : public Validator< T >
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   //!> this should throw an error at compile time if T is not a container
 | 
|---|
| 32 |   typedef typename T::value_type value_type;
 | 
|---|
| 33 | 
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |   /** Constructor for class STLVectorValidator, number of elements given by a range.
 | 
|---|
| 36 |    *
 | 
|---|
| 37 |    * @param validator validator for the single element
 | 
|---|
| 38 |    * @param range range of valid number of elements
 | 
|---|
| 39 |    */
 | 
|---|
| 40 |   STLVectorValidator(const range<size_t> &range, const Validator<value_type> &validator) :
 | 
|---|
| 41 |     ElementwiseValidator(validator.clone()),
 | 
|---|
| 42 |     NumberOfElementsValidator(new RangeValidator<size_t>(range))
 | 
|---|
| 43 |   {}
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   /** Constructor for class STLVectorValidator, number of elements given by a range.
 | 
|---|
| 46 |    *
 | 
|---|
| 47 |    * @param range range of valid number of elements
 | 
|---|
| 48 |    */
 | 
|---|
| 49 |   STLVectorValidator(const range<size_t> &range) :
 | 
|---|
| 50 |     ElementwiseValidator(new DummyValidator<value_type>()),
 | 
|---|
| 51 |     NumberOfElementsValidator(new RangeValidator<size_t>(range))
 | 
|---|
| 52 |   {}
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   /** Constructor for class STLVectorValidator, number of elements given by interval [\a min,\a max).
 | 
|---|
| 55 |    *
 | 
|---|
| 56 |    * @param validator validator for the single element
 | 
|---|
| 57 |    * @param min lower bound of interval for valid number of elements
 | 
|---|
| 58 |    * @param max upper bound of interval for valid number of elements
 | 
|---|
| 59 |    */
 | 
|---|
| 60 |   STLVectorValidator(const size_t min, const size_t max, const Validator<value_type> &validator) :
 | 
|---|
| 61 |     ElementwiseValidator(validator.clone()),
 | 
|---|
| 62 |     NumberOfElementsValidator(new RangeValidator<size_t>(min,max))
 | 
|---|
| 63 |   {}
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   /** Constructor for class STLVectorValidator, number of elements given by interval [\a min,\a max).
 | 
|---|
| 66 |    *
 | 
|---|
| 67 |    * @param min lower bound of interval for valid number of elements
 | 
|---|
| 68 |    * @param max upper bound of interval for valid number of elements
 | 
|---|
| 69 |    */
 | 
|---|
| 70 |   STLVectorValidator(const size_t min, const size_t max) :
 | 
|---|
| 71 |     ElementwiseValidator(new DummyValidator<value_type>()),
 | 
|---|
| 72 |     NumberOfElementsValidator(new RangeValidator<size_t>(min,max))
 | 
|---|
| 73 |   {}
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   /** Constructor for class STLVectorValidator, any number of elements is allowed.
 | 
|---|
| 76 |    *
 | 
|---|
| 77 |    * @param validator validator for the single element
 | 
|---|
| 78 |    */
 | 
|---|
| 79 |   STLVectorValidator(const Validator<value_type> &validator) :
 | 
|---|
| 80 |     ElementwiseValidator(validator.clone()),
 | 
|---|
| 81 |     NumberOfElementsValidator(new DummyValidator<size_t>())
 | 
|---|
| 82 |   {}
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   /** Constructor for class STLVectorValidator, any number of unrestricted elements is allowed.
 | 
|---|
| 85 |    *
 | 
|---|
| 86 |    */
 | 
|---|
| 87 |   STLVectorValidator() :
 | 
|---|
| 88 |     ElementwiseValidator(new DummyValidator<value_type>()),
 | 
|---|
| 89 |     NumberOfElementsValidator(new DummyValidator<size_t>())
 | 
|---|
| 90 |   {}
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   /** Destructor for class STLVectorValidator
 | 
|---|
| 93 |    *
 | 
|---|
| 94 |    */
 | 
|---|
| 95 |   ~STLVectorValidator()
 | 
|---|
| 96 |   {
 | 
|---|
| 97 |     delete ElementwiseValidator;
 | 
|---|
| 98 |     delete NumberOfElementsValidator;
 | 
|---|
| 99 |   }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   bool isValid(const T & _value) const
 | 
|---|
| 102 |   {
 | 
|---|
| 103 |     bool status = NumberOfElementsValidator->isValid(_value.size());
 | 
|---|
| 104 |     for (typename std::vector<value_type>::const_iterator iter = _value.begin();
 | 
|---|
| 105 |         iter != _value.end(); ++iter) {
 | 
|---|
| 106 |       status = status && ElementwiseValidator->isValid(*iter);
 | 
|---|
| 107 |     }
 | 
|---|
| 108 |     return status;
 | 
|---|
| 109 |   }
 | 
|---|
| 110 |   bool operator==(const Validator<T> &_instance) const
 | 
|---|
| 111 |   {
 | 
|---|
| 112 |     const STLVectorValidator<T> *inst = dynamic_cast<const STLVectorValidator<T> *>(&_instance);
 | 
|---|
| 113 |     bool status = *NumberOfElementsValidator == *inst->NumberOfElementsValidator;
 | 
|---|
| 114 |     status = status && *ElementwiseValidator == *inst->ElementwiseValidator;
 | 
|---|
| 115 |     return status;
 | 
|---|
| 116 |   }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   Validator<T>* clone() const
 | 
|---|
| 119 |   {
 | 
|---|
| 120 |     Validator<T> *inst = new STLVectorValidator<T>(*NumberOfElementsValidator, *ElementwiseValidator);
 | 
|---|
| 121 |     return inst;
 | 
|---|
| 122 |   }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   Validator<value_type> *getElementwiseValidator(){ return ElementwiseValidator;  }
 | 
|---|
| 125 |   Validator<size_t> *getNumberOfElementsValidator(){  return NumberOfElementsValidator; }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | private:
 | 
|---|
| 128 |   /** Constructor for class STLVectorValidator, number of elements given by \ref RangeValidator.
 | 
|---|
| 129 |    *
 | 
|---|
| 130 |    * \note Is only used in STLVectorValidator::clone() as it may lead to ambiguities
 | 
|---|
| 131 |    *  with a signature of two validators in case value_type equals size_t.
 | 
|---|
| 132 |    *
 | 
|---|
| 133 |    * @param validator validator for the single element
 | 
|---|
| 134 |    * @param rangevalidator validator for the valid number of elements
 | 
|---|
| 135 |    */
 | 
|---|
| 136 |   STLVectorValidator(const Validator<size_t> &rangevalidator, const Validator<value_type> &validator) :
 | 
|---|
| 137 |     ElementwiseValidator(validator.clone()),
 | 
|---|
| 138 |     NumberOfElementsValidator(dynamic_cast< Validator<size_t> *>(rangevalidator.clone()))
 | 
|---|
| 139 |   {}
 | 
|---|
| 140 | 
 | 
|---|
| 141 | 
 | 
|---|
| 142 | private:
 | 
|---|
| 143 |   Validator<value_type> *ElementwiseValidator;
 | 
|---|
| 144 |   Validator<size_t> *NumberOfElementsValidator;
 | 
|---|
| 145 | };
 | 
|---|
| 146 | 
 | 
|---|
| 147 | 
 | 
|---|
| 148 | #endif /* STLVECTORVALIDATOR_HPP_ */
 | 
|---|