| 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 |   Validator<T> *getA(){ return a; }
 | 
|---|
| 53 |   Validator<T> *getB(){ return b; }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | private:
 | 
|---|
| 56 |   Validator<T> *a;
 | 
|---|
| 57 |   Validator<T> *b;
 | 
|---|
| 58 | };
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /** Logical OR composition of two Validators.
 | 
|---|
| 61 |  *
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | template <class T>
 | 
|---|
| 64 | class Or_Validator : public Validator<T>
 | 
|---|
| 65 | {
 | 
|---|
| 66 | public:
 | 
|---|
| 67 |   Or_Validator(const Validator<T> &_a, const Validator<T> &_b) :
 | 
|---|
| 68 |     a(_a.clone()), b(_b.clone())
 | 
|---|
| 69 |   {}
 | 
|---|
| 70 |   virtual ~Or_Validator()
 | 
|---|
| 71 |   {
 | 
|---|
| 72 |     delete(a);
 | 
|---|
| 73 |     delete(b);
 | 
|---|
| 74 |   }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   bool isValid(const T & _value) const {
 | 
|---|
| 77 |     return (*a)(_value) || (*b)(_value);
 | 
|---|
| 78 |   }
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   Validator<T>* clone() const
 | 
|---|
| 81 |   { return new Or_Validator(*a, *b);  }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   // comparator
 | 
|---|
| 84 |   bool operator==(const Validator<T> &_instance) const
 | 
|---|
| 85 |   {
 | 
|---|
| 86 |     const Or_Validator<T> *inst = dynamic_cast<const Or_Validator<T> *>(&_instance);
 | 
|---|
| 87 |     if (inst)
 | 
|---|
| 88 |       return (*a == *inst->a) && (*b == *inst->b);
 | 
|---|
| 89 |     return false;
 | 
|---|
| 90 |   }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | private:
 | 
|---|
| 93 |   Validator<T> *a;
 | 
|---|
| 94 |   Validator<T> *b;
 | 
|---|
| 95 | };
 | 
|---|
| 96 | 
 | 
|---|
| 97 | /** Logical NOT composition of a Validator.
 | 
|---|
| 98 |  *
 | 
|---|
| 99 |  */
 | 
|---|
| 100 | template <class T>
 | 
|---|
| 101 | class Not_Validator : public Validator<T>
 | 
|---|
| 102 | {
 | 
|---|
| 103 | public:
 | 
|---|
| 104 |   Not_Validator(const Validator<T> &_a) :
 | 
|---|
| 105 |     a(_a.clone())
 | 
|---|
| 106 |   {}
 | 
|---|
| 107 |   virtual ~Not_Validator()
 | 
|---|
| 108 |   {
 | 
|---|
| 109 |     delete(a);
 | 
|---|
| 110 |   }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   bool isValid(const T & _value) const {
 | 
|---|
| 113 |     return !(*a)(_value);
 | 
|---|
| 114 |   }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   Validator<T>* clone() const
 | 
|---|
| 117 |   { return new Not_Validator(*a);  }
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   // comparator
 | 
|---|
| 120 |   bool operator==(const Validator<T> &_instance) const
 | 
|---|
| 121 |   {
 | 
|---|
| 122 |     const Not_Validator<T> *inst = dynamic_cast<const Not_Validator<T> *>(&_instance);
 | 
|---|
| 123 |     if (inst)
 | 
|---|
| 124 |       return (*a == *inst->a);
 | 
|---|
| 125 |     return false;
 | 
|---|
| 126 |   }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | private:
 | 
|---|
| 129 |   Validator<T> *a;
 | 
|---|
| 130 | };
 | 
|---|
| 131 | 
 | 
|---|
| 132 | 
 | 
|---|
| 133 | 
 | 
|---|
| 134 | template <class T>
 | 
|---|
| 135 | And_Validator<T> operator&&(const Validator<T> &a, const Validator<T> &b)
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   return And_Validator<T>(a, b);
 | 
|---|
| 138 | }
 | 
|---|
| 139 | 
 | 
|---|
| 140 | template <class T>
 | 
|---|
| 141 | Or_Validator<T> operator||(const Validator<T> &a, const Validator<T> &b)
 | 
|---|
| 142 | {
 | 
|---|
| 143 |   return Or_Validator<T>(a, b);
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | template <class T>
 | 
|---|
| 147 | Not_Validator<T> operator!(const Validator<T> &a)
 | 
|---|
| 148 | {
 | 
|---|
| 149 |   return Not_Validator<T>(a);
 | 
|---|
| 150 | }
 | 
|---|
| 151 | 
 | 
|---|
| 152 | 
 | 
|---|
| 153 | #endif /* OPS_VALIDATOR_IMPL_HPP_ */
 | 
|---|