ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/io/ParamConstraint.hpp
Revision: 2448
Committed: Wed Nov 16 23:10:02 2005 UTC (18 years, 8 months ago) by tim
File size: 11178 byte(s)
Log Message:
OptionSectionParser get compiled

File Contents

# User Rev Content
1 tim 2366 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42 tim 2371 #ifndef IO_PARAMCONSTRAINT_HPP
43     #define IO_PARAMCONSTRAINT_HPP
44     #include <sstream>
45 tim 2372 #include "utils/CaseConversion.hpp"
46 tim 2376 #include "utils/StringTokenizer.hpp"
47 tim 2448 namespace oopse {
48 tim 2371 /**
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 tim 2376 ZeroConstraint() {this->description_ = "zero";}
70 tim 2371 template<typename DataType>
71     bool operator()( DataType data ) const {
72     return data == 0;
73     }
74     };
75    
76     struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{
77 tim 2376 NonZeroConstraint() {this->description_ = "nonzero";}
78 tim 2371
79     template<typename DataType>
80     bool operator()( DataType data ) const {
81     return data != 0;
82     }
83     };
84    
85     struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{
86 tim 2376 PositiveConstraint() {this->description_ = "positive";}
87 tim 2371 template<typename DataType>
88     bool operator()( DataType data ) const
89     {
90     return data > 0;
91     }
92     };
93    
94     struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{
95 tim 2376 NonPositiveConstraint() {this->description_ = "nonpositive";}
96 tim 2371 template<typename DataType>
97     bool operator()( DataType data ) const
98     {
99     return data <= 0;
100     }
101     };
102    
103     struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{
104 tim 2376 NegativeConstraint() {this->description_ = "negative";}
105 tim 2371 template<typename DataType>
106     bool operator()( DataType data ) const
107     {
108     return data < 0;
109     }
110     };
111    
112     struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{
113 tim 2376 NonNegativeConstraint() {this->description_ = "nonnegative";}
114 tim 2371 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 tim 2376 this->description_ = iss.str();
128 tim 2371 }
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 tim 2376 this->description_ = iss.str();
144 tim 2371 }
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 tim 2376 this->description_ = iss.str();
162 tim 2371
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 tim 2372 EqualIgnoreCaseConstraint(std::string rhs) : rhs_(oopse::toUpperCopy(rhs)){
175 tim 2371 std::stringstream iss;
176     iss << "equal to (case insensitive) " << rhs;
177 tim 2376 this->description_ = iss.str();
178 tim 2371 }
179    
180     bool operator()( std::string data ) const {
181 tim 2372 return oopse::toUpperCopy(data) == rhs_;
182 tim 2371 }
183    
184     private:
185     std::string rhs_;
186     };
187    
188 tim 2376 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 tim 2371 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 tim 2376 this->description_ = iss.str();
218 tim 2371 }
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 tim 2376 this->description_ = iss.str();
234 tim 2371 }
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 tim 2376 this->description_ = iss.str();
252 tim 2371 }
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 tim 2376 this->description_ = iss.str();
274 tim 2371 }
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 tim 2377 iss << "(not" << cons_.getConstraintDescription() << ")";
294 tim 2376 this->description_ = iss.str();
295 tim 2371 }
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 tim 2448 NotEmptyConstraint isNotEmpty();
335     ZeroConstraint isZero();
336 tim 2371
337 tim 2448 ParamConstraintFacade<NonZeroConstraint> isNonZero();
338     PositiveConstraint isPositive();
339     NonPositiveConstraint isNonPositive();
340 tim 2371
341 tim 2448 NegativeConstraint isNegative();
342 tim 2371
343 tim 2448 NonNegativeConstraint isNonNegative();
344 tim 2371
345     template<typename T>
346 tim 2448 inline LessThanConstraint<T>isLessThan(T& v ) {
347 tim 2371 return LessThanConstraint<T>(v);
348     }
349    
350     template<typename T>
351 tim 2448 inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) {
352 tim 2371 return ParamConstraintFacade<LessThanOrEqualToConstraint<T> >(v);
353     }
354    
355     template<typename T>
356 tim 2448 inline EqualConstraint<T> isEqual(T& v ) {
357 tim 2371 return EqualConstraint<T>(v);
358     }
359    
360     template<typename T>
361 tim 2448 inline GreaterThanConstraint<T> isGreaterThan(T& v ) {
362 tim 2371 return GreaterThanConstraint<T>(v);
363     }
364    
365     template<typename T>
366 tim 2448 inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) {
367 tim 2371 return GreaterThanOrEqualTo<T>(v);
368     }
369    
370 tim 2448 EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str);
371 tim 2371 }
372     #endif