1 |
/* |
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 |
#ifndef IO_PARAMCONSTRAINT_HPP |
43 |
#define IO_PARAMCONSTRAINT_HPP |
44 |
#include <sstream> |
45 |
#include "utils/CaseConversion.hpp" |
46 |
#include "utils/StringTokenizer.hpp" |
47 |
namespace oopse { |
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 |
|
122 |
struct EvenConstraint : public ParamConstraintFacade<EvenConstraint>{ |
123 |
EvenConstraint() {this->description_ = "even";} |
124 |
template<typename DataType> |
125 |
bool operator()( DataType data ) const |
126 |
{ |
127 |
return data % 2 == 0; |
128 |
} |
129 |
}; |
130 |
|
131 |
template<typename T> |
132 |
struct LessThanConstraint : public ParamConstraintFacade<LessThanConstraint<T> > { |
133 |
|
134 |
LessThanConstraint(T rhs) : rhs_(rhs){ |
135 |
std::stringstream iss; |
136 |
iss << "less than " << rhs; |
137 |
this->description_ = iss.str(); |
138 |
} |
139 |
template<typename DataType> |
140 |
bool operator()( DataType data ) const { |
141 |
return data < rhs_; |
142 |
} |
143 |
private: |
144 |
T rhs_; |
145 |
}; |
146 |
|
147 |
template<typename T> |
148 |
struct LessThanOrEqualToConstraint : public ParamConstraintFacade<LessThanOrEqualToConstraint<T> > { |
149 |
|
150 |
LessThanOrEqualToConstraint(T rhs) : rhs_(rhs) { |
151 |
std::stringstream iss; |
152 |
iss << "less than or equal to" << rhs; |
153 |
this->description_ = iss.str(); |
154 |
} |
155 |
|
156 |
template<typename DataType> |
157 |
bool operator()( DataType data ) const { |
158 |
return data <= rhs_; |
159 |
} |
160 |
|
161 |
private: |
162 |
T rhs_; |
163 |
}; |
164 |
|
165 |
template<typename T> |
166 |
struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T> > { |
167 |
|
168 |
EqualConstraint(T rhs) : rhs_(rhs){ |
169 |
std::stringstream iss; |
170 |
iss << "equal to" << rhs; |
171 |
this->description_ = iss.str(); |
172 |
|
173 |
} |
174 |
template<typename DataType> |
175 |
bool operator()( DataType data ) const { |
176 |
return data == rhs_; |
177 |
} |
178 |
private: |
179 |
T rhs_; |
180 |
}; |
181 |
|
182 |
struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> { |
183 |
|
184 |
EqualIgnoreCaseConstraint(std::string rhs) : rhs_(oopse::toUpperCopy(rhs)){ |
185 |
std::stringstream iss; |
186 |
iss << "equal to (case insensitive) " << rhs; |
187 |
this->description_ = iss.str(); |
188 |
} |
189 |
|
190 |
bool operator()( std::string data ) const { |
191 |
return oopse::toUpperCopy(data) == rhs_; |
192 |
} |
193 |
|
194 |
private: |
195 |
std::string rhs_; |
196 |
}; |
197 |
|
198 |
struct ContainsConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> { |
199 |
ContainsConstraint(std::string rhs) : rhs_(oopse::toUpperCopy(rhs)){ |
200 |
std::stringstream iss; |
201 |
iss << "contains " << rhs; |
202 |
this->description_ = iss.str(); |
203 |
} |
204 |
|
205 |
bool operator()( std::string data ) const { |
206 |
oopse::StringTokenizer tokenizer(oopse::toUpperCopy(data), " ,;|\t\n\r"); |
207 |
while (tokenizer.hasMoreTokens()) { |
208 |
if (tokenizer.nextToken() == rhs_) { |
209 |
return true; |
210 |
} |
211 |
} |
212 |
|
213 |
return false; |
214 |
} |
215 |
|
216 |
private: |
217 |
std::string rhs_; |
218 |
|
219 |
}; |
220 |
|
221 |
template<typename T> |
222 |
struct GreaterThanConstraint : public ParamConstraintFacade<GreaterThanConstraint<T> > { |
223 |
|
224 |
GreaterThanConstraint(T rhs) : rhs_(rhs){ |
225 |
std::stringstream iss; |
226 |
iss << "greater than" << rhs; |
227 |
this->description_ = iss.str(); |
228 |
} |
229 |
template<typename DataType> |
230 |
bool operator()( DataType data ) const { |
231 |
return data > rhs_; |
232 |
} |
233 |
private: |
234 |
T rhs_; |
235 |
}; |
236 |
|
237 |
template<typename T> |
238 |
struct GreaterThanOrEqualTo : public ParamConstraintFacade<GreaterThanOrEqualTo<T> > { |
239 |
|
240 |
GreaterThanOrEqualTo(T rhs) : rhs_(rhs){ |
241 |
std::stringstream iss; |
242 |
iss << "greater than or equal to" << rhs; |
243 |
this->description_ = iss.str(); |
244 |
} |
245 |
template<typename DataType> |
246 |
bool operator()( DataType data ) const { |
247 |
return data >= rhs_; |
248 |
} |
249 |
private: |
250 |
T rhs_; |
251 |
}; |
252 |
|
253 |
// class_and composition predicate |
254 |
template<typename Cons1T, typename Cons2T> |
255 |
struct AndParamConstraint: public ParamConstraintFacade< AndParamConstraint<Cons1T,Cons2T> > { |
256 |
public: |
257 |
|
258 |
AndParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { |
259 |
std::stringstream iss; |
260 |
iss << "(" << cons1_.getConstraintDescription() << " and " << cons2_.getConstraintDescription() << ")"; |
261 |
this->description_ = iss.str(); |
262 |
} |
263 |
|
264 |
template<typename DataType> |
265 |
bool operator()( DataType data ) const { |
266 |
return cons1_(data) && cons2_(data); |
267 |
} |
268 |
|
269 |
private: |
270 |
Cons1T cons1_; |
271 |
Cons2T cons2_; |
272 |
}; |
273 |
|
274 |
|
275 |
|
276 |
template<typename Cons1T, typename Cons2T> |
277 |
struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > { |
278 |
public: |
279 |
|
280 |
OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { |
281 |
std::stringstream iss; |
282 |
iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << ""; |
283 |
this->description_ = iss.str(); |
284 |
} |
285 |
|
286 |
template<typename DataType> |
287 |
bool operator()( DataType data ) const { |
288 |
return cons1_(data) || cons2_(data); |
289 |
} |
290 |
|
291 |
private: |
292 |
Cons1T cons1_; |
293 |
Cons2T cons2_; |
294 |
}; |
295 |
|
296 |
template<typename ConsT> |
297 |
struct NotParamConstraint: public ParamConstraintFacade< NotParamConstraint<ConsT> > { |
298 |
public: |
299 |
|
300 |
|
301 |
NotParamConstraint( ConsT cons) : cons_(cons) { |
302 |
std::stringstream iss; |
303 |
iss << "(not" << cons_.getConstraintDescription() << ")"; |
304 |
this->description_ = iss.str(); |
305 |
} |
306 |
|
307 |
template<typename DataType> |
308 |
bool operator()( DataType data ) const { |
309 |
return !cons_(data); |
310 |
} |
311 |
|
312 |
private: |
313 |
ConsT cons_; |
314 |
}; |
315 |
|
316 |
|
317 |
template<typename Cons1T, typename Cons2T> |
318 |
inline AndParamConstraint<Cons1T, Cons2T> |
319 |
operator &&(const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) { |
320 |
|
321 |
return AndParamConstraint<Cons1T,Cons2T>( |
322 |
*static_cast<const Cons1T*>(&cons1), |
323 |
*static_cast<const Cons2T*>(&cons2) ); |
324 |
} |
325 |
|
326 |
template<typename Cons1T, typename Cons2T> |
327 |
inline OrParamConstraint<Cons1T, Cons2T> |
328 |
operator ||( const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) { |
329 |
|
330 |
return OrParamConstraint<Cons1T,Cons2T>( |
331 |
*static_cast<const Cons1T*>(&cons1), |
332 |
*static_cast<const Cons2T*>(&cons2) ); |
333 |
} |
334 |
|
335 |
|
336 |
template<typename ConsT> |
337 |
inline NotParamConstraint<ConsT> |
338 |
operator !( const ParamConstraintFacade<ConsT>& cons ) { |
339 |
|
340 |
return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons)); |
341 |
} |
342 |
|
343 |
|
344 |
NotEmptyConstraint isNotEmpty(); |
345 |
ZeroConstraint isZero(); |
346 |
|
347 |
ParamConstraintFacade<NonZeroConstraint> isNonZero(); |
348 |
PositiveConstraint isPositive(); |
349 |
NonPositiveConstraint isNonPositive(); |
350 |
|
351 |
NegativeConstraint isNegative(); |
352 |
|
353 |
NonNegativeConstraint isNonNegative(); |
354 |
EvenConstraint isEven(); |
355 |
|
356 |
template<typename T> |
357 |
inline LessThanConstraint<T>isLessThan(T& v ) { |
358 |
return LessThanConstraint<T>(v); |
359 |
} |
360 |
|
361 |
template<typename T> |
362 |
inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) { |
363 |
return ParamConstraintFacade<LessThanOrEqualToConstraint<T> >(v); |
364 |
} |
365 |
|
366 |
template<typename T> |
367 |
inline EqualConstraint<T> isEqual(T& v ) { |
368 |
return EqualConstraint<T>(v); |
369 |
} |
370 |
|
371 |
template<typename T> |
372 |
inline GreaterThanConstraint<T> isGreaterThan(T& v ) { |
373 |
return GreaterThanConstraint<T>(v); |
374 |
} |
375 |
|
376 |
template<typename T> |
377 |
inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) { |
378 |
return GreaterThanOrEqualTo<T>(v); |
379 |
} |
380 |
|
381 |
EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str); |
382 |
} |
383 |
#endif |