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