ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/io/ParamConstraint.hpp
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines