| 1 | /*
 | 
|---|
| 2 |  * Ops_Validator_impl.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 9, 2012
 | 
|---|
| 5 |  *      Author: ankele
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef OPS_VALIDATOR_IMPL_HPP_
 | 
|---|
| 9 | #define OPS_VALIDATOR_IMPL_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | 
 | 
|---|
| 13 | // include config.h
 | 
|---|
| 14 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 15 | #include <config.h>
 | 
|---|
| 16 | #endif
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "Validator.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /** Logical AND composition of two Validators.
 | 
|---|
| 21 |  *
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | template <class T>
 | 
|---|
| 24 | class And_Validator : public Validator<T>
 | 
|---|
| 25 | {
 | 
|---|
| 26 | public:
 | 
|---|
| 27 |   And_Validator(const Validator<T> &_a, const Validator<T> &_b) :
 | 
|---|
| 28 |     a(_a.clone()), b(_b.clone())
 | 
|---|
| 29 |   {}
 | 
|---|
| 30 |   virtual ~And_Validator()
 | 
|---|
| 31 |   {
 | 
|---|
| 32 |     delete(a);
 | 
|---|
| 33 |     delete(b);
 | 
|---|
| 34 |   }
 | 
|---|
| 35 | 
 | 
|---|
| 36 |   bool isValid(const T & _value) const {
 | 
|---|
| 37 |     return (*a)(_value) && (*b)(_value);
 | 
|---|
| 38 |   }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |   Validator<T>* clone() const
 | 
|---|
| 41 |   { return new And_Validator(*a, *b);  }
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   // comparator
 | 
|---|
| 44 |   bool operator==(const Validator<T> &_instance) const
 | 
|---|
| 45 |   {
 | 
|---|
| 46 |     const And_Validator<T> *inst = dynamic_cast<const And_Validator<T> *>(&_instance);
 | 
|---|
| 47 |     if (inst)
 | 
|---|
| 48 |       return (*a == *inst->a) && (*b == *inst->b);
 | 
|---|
| 49 |     return false;
 | 
|---|
| 50 |   }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | private:
 | 
|---|
| 53 |   Validator<T> *a;
 | 
|---|
| 54 |   Validator<T> *b;
 | 
|---|
| 55 | };
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /** Logical OR composition of two Validators.
 | 
|---|
| 58 |  *
 | 
|---|
| 59 |  */
 | 
|---|
| 60 | template <class T>
 | 
|---|
| 61 | class Or_Validator : public Validator<T>
 | 
|---|
| 62 | {
 | 
|---|
| 63 | public:
 | 
|---|
| 64 |   Or_Validator(const Validator<T> &_a, const Validator<T> &_b) :
 | 
|---|
| 65 |     a(_a.clone()), b(_b.clone())
 | 
|---|
| 66 |   {}
 | 
|---|
| 67 |   virtual ~Or_Validator()
 | 
|---|
| 68 |   {
 | 
|---|
| 69 |     delete(a);
 | 
|---|
| 70 |     delete(b);
 | 
|---|
| 71 |   }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   bool isValid(const T & _value) const {
 | 
|---|
| 74 |     return (*a)(_value) || (*b)(_value);
 | 
|---|
| 75 |   }
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   Validator<T>* clone() const
 | 
|---|
| 78 |   { return new Or_Validator(*a, *b);  }
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   // comparator
 | 
|---|
| 81 |   bool operator==(const Validator<T> &_instance) const
 | 
|---|
| 82 |   {
 | 
|---|
| 83 |     const Or_Validator<T> *inst = dynamic_cast<const Or_Validator<T> *>(&_instance);
 | 
|---|
| 84 |     if (inst)
 | 
|---|
| 85 |       return (*a == *inst->a) && (*b == *inst->b);
 | 
|---|
| 86 |     return false;
 | 
|---|
| 87 |   }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | private:
 | 
|---|
| 90 |   Validator<T> *a;
 | 
|---|
| 91 |   Validator<T> *b;
 | 
|---|
| 92 | };
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /** Logical NOT composition of a Validator.
 | 
|---|
| 95 |  *
 | 
|---|
| 96 |  */
 | 
|---|
| 97 | template <class T>
 | 
|---|
| 98 | class Not_Validator : public Validator<T>
 | 
|---|
| 99 | {
 | 
|---|
| 100 | public:
 | 
|---|
| 101 |   Not_Validator(const Validator<T> &_a) :
 | 
|---|
| 102 |     a(_a.clone())
 | 
|---|
| 103 |   {}
 | 
|---|
| 104 |   virtual ~Not_Validator()
 | 
|---|
| 105 |   {
 | 
|---|
| 106 |     delete(a);
 | 
|---|
| 107 |   }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   bool isValid(const T & _value) const {
 | 
|---|
| 110 |     return !(*a)(_value);
 | 
|---|
| 111 |   }
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   Validator<T>* clone() const
 | 
|---|
| 114 |   { return new Not_Validator(*a);  }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   // comparator
 | 
|---|
| 117 |   bool operator==(const Validator<T> &_instance) const
 | 
|---|
| 118 |   {
 | 
|---|
| 119 |     const Not_Validator<T> *inst = dynamic_cast<const Not_Validator<T> *>(&_instance);
 | 
|---|
| 120 |     if (inst)
 | 
|---|
| 121 |       return (*a == *inst->a);
 | 
|---|
| 122 |     return false;
 | 
|---|
| 123 |   }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | private:
 | 
|---|
| 126 |   Validator<T> *a;
 | 
|---|
| 127 | };
 | 
|---|
| 128 | 
 | 
|---|
| 129 | 
 | 
|---|
| 130 | 
 | 
|---|
| 131 | template <class T>
 | 
|---|
| 132 | And_Validator<T> operator&&(const Validator<T> &a, const Validator<T> &b)
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   return And_Validator<T>(a, b);
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | template <class T>
 | 
|---|
| 138 | Or_Validator<T> operator||(const Validator<T> &a, const Validator<T> &b)
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   return Or_Validator<T>(a, b);
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | template <class T>
 | 
|---|
| 144 | Not_Validator<T> operator!(const Validator<T> &a)
 | 
|---|
| 145 | {
 | 
|---|
| 146 |   return Not_Validator<T>(a);
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | 
 | 
|---|
| 150 | #endif /* OPS_VALIDATOR_IMPL_HPP_ */
 | 
|---|