ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/io/ParamConstraint.hpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/io/ParamConstraint.hpp (file contents):
Revision 2366 by tim, Fri Oct 14 14:48:10 2005 UTC vs.
Revision 2371 by tim, Fri Oct 14 21:43:13 2005 UTC

# Line 39 | Line 39
39   * such damages.
40   */
41  
42 < #ifndef IO_PARAMCONSTRAINT_HPP
43 < #define IO_PARAMCONSTRAINT_HPP
44 <
45 < /**
46 <  * This class allows to recognize constraint predicates, so that they can be combined using
47 <  * composition operators. Every constraint predicate must be derived from this class
48 <  */
49 < template<typename Derived>
50 < struct ParamConstraintFacade {
51 < };
52 <
53 <
54 < struct NonConstraint : public ParamConstraintFacade<NonConstraint>{
55 <    template<typename DataType>
56 <    bool operator()( DataType data ) const {
57 <        return true;
58 <    }
59 < };
60 <
61 < struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{
62 <    bool operator()( const std::string data ) const {
63 <        return !data.empty();
64 <    }
65 < };
66 <
67 < struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{
68 <    template<typename DataType>
69 <    bool operator()( DataType data ) const
70 <    {
71 <        return data == 0;
72 <    }
73 < };
74 <
75 < struct NotZeroConstraint : public ParamConstraintFacade<NotZeroConstraint>{
76 <    template<typename DataType>
77 <    bool operator()( DataType data ) const
78 <    {
79 <        return data != 0;
80 <    }
81 < };
82 <
83 < struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{
84 <    template<typename DataType>
85 <    bool operator()( DataType data ) const
86 <    {
87 <        return data > 0;
88 <    }
89 < };
90 <
91 < struct NotPositiveConstraint : public ParamConstraintFacade<NotPositiveConstraint>{
92 <    template<typename DataType>
93 <    bool operator()( DataType data ) const
94 <    {
95 <        return data <= 0;
96 <    }
97 < };
98 < struct NotNegativeConstraint : public ParamConstraintFacade<NotNegativeConstraint>{
99 <    template<typename DataType>
100 <    bool operator()( DataType data ) const
101 <    {
102 <        return data >= 0;
103 <    }
104 < };
105 <
106 < struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{
107 <    template<typename DataType>
108 <    bool operator()( DataType data ) const
109 <    {
110 <        return data < 0;
111 <    }
112 < };
113 <
114 < struct NotNegativeConstraint : public ParamConstraintFacade<NotNegativeConstraint>{
115 <    template<typename DataType>
116 <    bool operator()( DataType data ) const
117 <    {
118 <        return true;
119 <    }
120 < };
121 <
122 < template<typename T>
123 < struct LessThanConstraint : public ParamConstraintFacade<LessThanConstraint> {
124 <    
125 <    LessThanConstraint(T rhs) : rhs_(rhs){}
126 <    template<typename DataType>
127 <    bool operator()( DataType data ) const {
128 <        return data < rhs_;
129 <    }
130 <    private:
131 <        T rhs_;        
132 < };
133 <
134 < template<typename T>
135 < struct LessEqualConstraint : public ParamConstraintFacade<LessEqualConstraint> {
136 <    
137 <    LessEqualConstraint(T rhs) : rhs_(rhs){}
138 <    template<typename DataType>
139 <    bool operator()( DataType data ) const {
140 <        return data <= rhs_;
141 <    }
142 <    private:
143 <        T rhs_;        
144 < };
145 <
146 < template<typename T>
147 < struct EqualConstraint : public ParamConstraintFacade<EqualConstraint> {
148 <    
149 <    EqualConstraint(T rhs) : rhs_(rhs){}
150 <    template<typename DataType>
151 <    bool operator()( DataType data ) const {
152 <        return data == rhs_;
153 <    }
154 <    private:
155 <        T rhs_;        
156 < };
157 <
158 < // class_and composition predicate
159 < template<typename Cons1T, typename Cons2T>
160 < struct AndParamConstraint:
161 <    public ParamConstraintFacade< AndParamConstraint<Cons1T,Cons2T> > {
162 < public:
163 <
164 <    AndParamConstraint( Cons1T cons1, Cons2T cons2 ) :
165 <        cons1_(cons1, cons2_(cons2) {}
166 <
167 <    template<typename DataType>
168 <    bool operator()( DataType data ) const {
169 <        return cons1_(data) && cons2_(data);
170 <    }
171 <
172 < private:
173 <    Cons1T cons1_;
174 <    Cons2T cons2_;
175 < };
176 <
177 <
178 <
179 < template<typename Cons1T, typename Cons2T>
180 < struct OrParamConstraint:
181 <    public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > {
182 < public:
183 <
184 <
185 <    OrParamConstraint( Cons1T cons1, Cons2T cons2 ) :
186 <        cons1_(cons1, cons2_(cons2) {}
187 <
188 <    template<typename DataType>
189 <    bool operator()( DataType data ) const {
190 <        return cons1_(data) || cons2_(data);
191 <    }
192 <
193 < private:
194 <    Cons1T cons1_;
195 <    Cons2T cons2_;
196 < };
197 <
198 < template<typename ConsT>
199 < struct NotParamConstraint:
200 <    public ParamConstraintFacade< NotParamConstraint<ConsT> >
201 < {
202 < public:
203 <
204 <
205 <    NotParamConstraint( ConsT cons) :
206 <        cons_(cons) {}
207 <
208 <    template<typename DataType>
209 <    bool operator()( DataType data ) const {
210 <        return !cons_(data);
211 <    }
212 <
213 < private:
214 <    ConsT cons_;
215 < };    
42 > #ifndef IO_PARAMCONSTRAINT_HPP
43 > #define IO_PARAMCONSTRAINT_HPP
44 > #include <sstream>
45  
46 <
47 < template<typename Cons1T, typename Cons2T>
48 < inline AndParamConstraint<Cons1T, Cons2T>
49 < operator &&(const ParamConstraintFacade<Cons1T>& cons1,  const ParamConstraintFacade<Cons2T>& cons2 ) {    
50 <
51 <    return AndParamConstraint<Cons1T,Cons2T>(
52 <        *static_cast<const Cons1T*>(&cons1),
53 <        *static_cast<const Cons2T*>(&cons2) );
54 < }
55 <
56 < template<typename Cons1T, typename Cons2T>
57 < inline OrParamConstraint<Cons1T, Cons2T>
58 < operator ||( const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) {    
59 <
60 <    return OrParamConstraint<Cons1T,Cons2T>(
61 <        *static_cast<const Cons1T*>(&cons1),
62 <        *static_cast<const Cons2T*>(&cons2) );
63 < }
64 <
65 <
66 < template<typename ConsT>
67 < inline NotParamConstraint<ConsT>
68 < operator !( const ParamConstraintFacade<ConsT>& cons ) {
69 <
70 <    return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons));
71 < }
72 <
73 <
74 <
75 < #endif
46 > /**
47 >  * This class allows to recognize constraint predicates, so that they can be combined using
48 >  * composition operators. Every constraint predicate must be derived from this class
49 >  */
50 > template<typename Derived>
51 > struct ParamConstraintFacade {
52 >    std::string getConstraintDescription() {return description_;}
53 >    protected:
54 >        std::string description_;
55 > };
56 >
57 > struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{
58 >
59 >    NotEmptyConstraint() { description_= "nonempty";}
60 >    bool operator()( const std::string data ) const {
61 >        return !data.empty();
62 >    }
63 > };
64 >
65 > struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{
66 >    
67 >    ZeroConstraint() {description_ = "zero";}
68 >    template<typename DataType>
69 >    bool operator()( DataType data ) const {
70 >        return data == 0;
71 >    }
72 > };
73 >
74 > struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{
75 >    NonZeroConstraint() {description_ = "nonzero";}
76 >
77 >    template<typename DataType>
78 >    bool operator()( DataType data ) const {
79 >        return data != 0;
80 >    }
81 > };
82 >
83 > struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{
84 >    PositiveConstraint() {description_ = "positive";}
85 >    template<typename DataType>
86 >    bool operator()( DataType data ) const
87 >    {
88 >        return data > 0;
89 >    }
90 > };
91 >
92 > struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{
93 >    NonPositiveConstraint() {description_ = "nonpositive";}
94 >    template<typename DataType>
95 >    bool operator()( DataType data ) const
96 >    {
97 >        return data <= 0;
98 >    }
99 > };
100 >
101 > struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{
102 >    NegativeConstraint() {description_ = "negative";}
103 >    template<typename DataType>
104 >    bool operator()( DataType data ) const
105 >    {
106 >        return data < 0;
107 >    }
108 > };
109 >
110 > struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{
111 >    NonNegativeConstraint() {description_ = "nonnegative";}
112 >    template<typename DataType>
113 >    bool operator()( DataType data ) const
114 >    {
115 >        return data >= 0;
116 >    }
117 > };
118 >
119 > template<typename T>
120 > struct LessThanConstraint : public ParamConstraintFacade<LessThanConstraint<T> > {
121 >    
122 >    LessThanConstraint(T rhs) : rhs_(rhs){
123 >        std::stringstream iss;
124 >        iss << "less than " << rhs;
125 >        description_ = iss.str();
126 >    }
127 >    template<typename DataType>
128 >    bool operator()( DataType data ) const {
129 >        return data < rhs_;
130 >    }
131 >    private:
132 >        T rhs_;        
133 > };
134 >
135 > template<typename T>
136 > struct LessThanOrEqualToConstraint : public ParamConstraintFacade<LessThanOrEqualToConstraint<T> > {
137 >    
138 >    LessThanOrEqualToConstraint(T rhs) : rhs_(rhs) {
139 >        std::stringstream iss;
140 >        iss << "less than or equal to" << rhs;
141 >        description_ = iss.str();
142 >    }
143 >    
144 >    template<typename DataType>
145 >    bool operator()( DataType data ) const {
146 >        return data <= rhs_;
147 >    }
148 >    
149 >    private:
150 >        T rhs_;        
151 > };
152 >
153 > template<typename T>
154 > struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T> > {
155 >    
156 >    EqualConstraint(T rhs) : rhs_(rhs){
157 >        std::stringstream iss;
158 >        iss << "equal to" << rhs;
159 >        description_ = iss.str();
160 >
161 >    }
162 >    template<typename DataType>
163 >    bool operator()( DataType data ) const {
164 >        return data == rhs_;
165 >    }
166 >    private:
167 >        T rhs_;        
168 > };
169 >
170 > struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
171 >    
172 >    EqualIgnoreCaseConstraint(std::string rhs) : rhs_(rhs){
173 >        std::stringstream iss;
174 >        iss << "equal to (case insensitive) " << rhs;
175 >        description_ = iss.str();
176 >    }
177 >    
178 >    bool operator()( std::string data ) const {
179 >        return data == rhs_;
180 >    }
181 >    
182 >    private:
183 >       std::string rhs_;        
184 > };
185 >
186 > template<typename T>
187 > struct GreaterThanConstraint : public ParamConstraintFacade<GreaterThanConstraint<T> > {
188 >    
189 >    GreaterThanConstraint(T rhs) : rhs_(rhs){
190 >        std::stringstream iss;
191 >        iss << "greater than" << rhs;
192 >        description_ = iss.str();
193 >    }
194 >    template<typename DataType>
195 >    bool operator()( DataType data ) const {
196 >        return data > rhs_;
197 >    }
198 >    private:
199 >        T rhs_;        
200 > };
201 >
202 > template<typename T>
203 > struct GreaterThanOrEqualTo : public ParamConstraintFacade<GreaterThanOrEqualTo<T> > {
204 >    
205 >    GreaterThanOrEqualTo(T rhs) : rhs_(rhs){
206 >        std::stringstream iss;
207 >        iss << "greater than or equal to" << rhs;
208 >        description_ = iss.str();
209 >    }
210 >    template<typename DataType>
211 >    bool operator()( DataType data ) const {
212 >        return data >= rhs_;
213 >    }
214 >    private:
215 >        T rhs_;        
216 > };
217 >
218 > // class_and composition predicate
219 > template<typename Cons1T, typename Cons2T>
220 > struct AndParamConstraint: public ParamConstraintFacade< AndParamConstraint<Cons1T,Cons2T> > {
221 > public:
222 >
223 >    AndParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) {
224 >        std::stringstream iss;
225 >        iss << "(" << cons1_.getConstraintDescription() << " and " << cons2_.getConstraintDescription() << ")";
226 >        description_ = iss.str();        
227 >    }
228 >
229 >    template<typename DataType>
230 >    bool operator()( DataType data ) const {
231 >        return cons1_(data) && cons2_(data);
232 >    }
233 >
234 > private:
235 >    Cons1T cons1_;
236 >    Cons2T cons2_;
237 > };
238 >
239 >
240 >
241 > template<typename Cons1T, typename Cons2T>
242 > struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > {
243 > public:
244 >
245 >
246 >    OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) {
247 >        std::stringstream iss;
248 >        iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << "";
249 >        description_ = iss.str();
250 >     }
251 >
252 >    template<typename DataType>
253 >    bool operator()( DataType data ) const {
254 >        return cons1_(data) || cons2_(data);
255 >    }
256 >
257 > private:
258 >    Cons1T cons1_;
259 >    Cons2T cons2_;
260 > };
261 >
262 > template<typename ConsT>
263 > struct NotParamConstraint: public ParamConstraintFacade< NotParamConstraint<ConsT> > {
264 > public:
265 >
266 >
267 >    NotParamConstraint( ConsT cons) : cons_(cons) {
268 >        std::stringstream iss;
269 >        iss << "(not" << cons1_.getConstraintDescription() << ")";
270 >        description_ = iss.str();
271 >    }
272 >
273 >    template<typename DataType>
274 >    bool operator()( DataType data ) const {
275 >        return !cons_(data);
276 >    }
277 >
278 > private:
279 >    ConsT cons_;
280 > };    
281 >
282 >
283 > template<typename Cons1T, typename Cons2T>
284 > inline AndParamConstraint<Cons1T, Cons2T>
285 > operator &&(const ParamConstraintFacade<Cons1T>& cons1,  const ParamConstraintFacade<Cons2T>& cons2 ) {    
286 >
287 >    return AndParamConstraint<Cons1T,Cons2T>(
288 >        *static_cast<const Cons1T*>(&cons1),
289 >        *static_cast<const Cons2T*>(&cons2) );
290 > }
291 >
292 > template<typename Cons1T, typename Cons2T>
293 > inline OrParamConstraint<Cons1T, Cons2T>
294 > operator ||( const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) {    
295 >
296 >    return OrParamConstraint<Cons1T,Cons2T>(
297 >        *static_cast<const Cons1T*>(&cons1),
298 >        *static_cast<const Cons2T*>(&cons2) );
299 > }
300 >
301 >
302 > template<typename ConsT>
303 > inline NotParamConstraint<ConsT>
304 > operator !( const ParamConstraintFacade<ConsT>& cons ) {
305 >
306 >    return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons));
307 > }
308 >
309 >
310 > NotEmptyConstraint isNotEmpty() {
311 >    return NotEmptyConstraint();
312 > }
313 >
314 > ZeroConstraint isZero() {
315 >    return ZeroConstraint();
316 > }
317 >
318 > ParamConstraintFacade<NonZeroConstraint> isNonZero() {
319 >    return ParamConstraintFacade<NonZeroConstraint>();
320 > }
321 >
322 > PositiveConstraint isPositive() {
323 >    return PositiveConstraint();
324 > }
325 >
326 > NonPositiveConstraint isNonPositive() {
327 >    return NonPositiveConstraint();
328 > }
329 >
330 > NegativeConstraint isNegative() {
331 >    return NegativeConstraint();
332 > }
333 >
334 > NonNegativeConstraint isNonNegative() {
335 >    return NonNegativeConstraint();
336 > }
337 >
338 > template<typename T>
339 > LessThanConstraint<T>isLessThan(T& v ) {
340 >    return LessThanConstraint<T>(v);
341 > }
342 >
343 > template<typename T>
344 > LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) {
345 >    return ParamConstraintFacade<LessThanOrEqualToConstraint<T> >(v);
346 > }
347 >
348 > template<typename T>
349 > EqualConstraint<T> isEqual(T& v ) {
350 >    return EqualConstraint<T>(v);
351 > }
352 >
353 > template<typename T>
354 > GreaterThanConstraint<T> isGreaterThan(T& v ) {
355 >    return GreaterThanConstraint<T>(v);
356 > }
357 >
358 > template<typename T>
359 > GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) {
360 >    return GreaterThanOrEqualTo<T>(v);
361 > }
362 >
363 > EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str) {
364 >    return EqualIgnoreCaseConstraint(str);
365 > }
366 >
367 > #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines