ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/io/ParamConstraint.hpp
Revision: 2372
Committed: Mon Oct 17 00:51:16 2005 UTC (18 years, 9 months ago) by tim
File size: 10675 byte(s)
Log Message:
Change error message in Globals

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 2371 /**
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 tim 2372 EqualIgnoreCaseConstraint(std::string rhs) : rhs_(oopse::toUpperCopy(rhs)){
173 tim 2371 std::stringstream iss;
174     iss << "equal to (case insensitive) " << rhs;
175     description_ = iss.str();
176     }
177    
178     bool operator()( std::string data ) const {
179 tim 2372 return oopse::toUpperCopy(data) == rhs_;
180 tim 2371 }
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