# | Line 42 | Line 42 | |
---|---|---|
42 | #ifndef IO_PARAMCONSTRAINT_HPP | |
43 | #define IO_PARAMCONSTRAINT_HPP | |
44 | #include <sstream> | |
45 | < | |
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 { |
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 | < | }; |
55 | > | protected: |
56 | > | std::string description_; |
57 | > | }; |
58 | ||
59 | < | struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{ |
60 | < | |
59 | > | struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{ |
60 | > | |
61 | NotEmptyConstraint() { description_= "nonempty";} | |
62 | bool operator()( const std::string data ) const { | |
63 | < | return !data.empty(); |
63 | > | return !data.empty(); |
64 | } | |
65 | < | }; |
65 | > | }; |
66 | ||
67 | < | struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{ |
67 | > | struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{ |
68 | ||
69 | < | ZeroConstraint() {description_ = "zero";} |
69 | > | ZeroConstraint() {this->description_ = "zero";} |
70 | template<typename DataType> | |
71 | bool operator()( DataType data ) const { | |
72 | < | return data == 0; |
72 | > | return data == 0; |
73 | } | |
74 | < | }; |
74 | > | }; |
75 | ||
76 | < | struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{ |
77 | < | NonZeroConstraint() {description_ = "nonzero";} |
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; |
81 | > | return data != 0; |
82 | } | |
83 | < | }; |
83 | > | }; |
84 | ||
85 | < | struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{ |
86 | < | PositiveConstraint() {description_ = "positive";} |
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; |
90 | > | return data > 0; |
91 | } | |
92 | < | }; |
92 | > | }; |
93 | ||
94 | < | struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{ |
95 | < | NonPositiveConstraint() {description_ = "nonpositive";} |
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; |
99 | > | return data <= 0; |
100 | } | |
101 | < | }; |
101 | > | }; |
102 | ||
103 | < | struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{ |
104 | < | NegativeConstraint() {description_ = "negative";} |
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; |
108 | > | return data < 0; |
109 | } | |
110 | < | }; |
110 | > | }; |
111 | ||
112 | < | struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{ |
113 | < | NonNegativeConstraint() {description_ = "nonnegative";} |
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; |
117 | > | return data >= 0; |
118 | } | |
119 | < | }; |
119 | > | }; |
120 | ||
121 | < | template<typename T> |
122 | < | struct LessThanConstraint : public ParamConstraintFacade<LessThanConstraint<T> > { |
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 | < | description_ = iss.str(); |
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_; |
141 | > | return data < rhs_; |
142 | } | |
143 | < | private: |
144 | < | T rhs_; |
145 | < | }; |
143 | > | private: |
144 | > | T rhs_; |
145 | > | }; |
146 | ||
147 | < | template<typename T> |
148 | < | struct LessThanOrEqualToConstraint : public ParamConstraintFacade<LessThanOrEqualToConstraint<T> > { |
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 | < | description_ = iss.str(); |
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_; |
158 | > | return data <= rhs_; |
159 | } | |
160 | ||
161 | < | private: |
162 | < | T rhs_; |
163 | < | }; |
161 | > | private: |
162 | > | T rhs_; |
163 | > | }; |
164 | ||
165 | < | template<typename T> |
166 | < | struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T> > { |
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 | < | description_ = iss.str(); |
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_; |
176 | > | return data == rhs_; |
177 | } | |
178 | < | private: |
179 | < | T rhs_; |
180 | < | }; |
178 | > | private: |
179 | > | T rhs_; |
180 | > | }; |
181 | ||
182 | < | struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> { |
182 | > | struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> { |
183 | ||
184 | < | EqualIgnoreCaseConstraint(std::string rhs) : rhs_(rhs){ |
185 | < | std::stringstream iss; |
186 | < | iss << "equal to (case insensitive) " << rhs; |
187 | < | description_ = iss.str(); |
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 data == rhs_; |
191 | > | return oopse::toUpperCopy(data) == rhs_; |
192 | } | |
193 | ||
194 | < | private: |
195 | < | std::string rhs_; |
196 | < | }; |
194 | > | private: |
195 | > | std::string rhs_; |
196 | > | }; |
197 | ||
198 | < | template<typename T> |
199 | < | struct GreaterThanConstraint : public ParamConstraintFacade<GreaterThanConstraint<T> > { |
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 | < | description_ = iss.str(); |
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_; |
231 | > | return data > rhs_; |
232 | } | |
233 | < | private: |
234 | < | T rhs_; |
235 | < | }; |
233 | > | private: |
234 | > | T rhs_; |
235 | > | }; |
236 | ||
237 | < | template<typename T> |
238 | < | struct GreaterThanOrEqualTo : public ParamConstraintFacade<GreaterThanOrEqualTo<T> > { |
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 | < | description_ = iss.str(); |
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_; |
247 | > | return data >= rhs_; |
248 | } | |
249 | < | private: |
250 | < | T rhs_; |
251 | < | }; |
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: |
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 | < | description_ = iss.str(); |
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); |
266 | > | return cons1_(data) && cons2_(data); |
267 | } | |
268 | ||
269 | < | private: |
269 | > | private: |
270 | Cons1T cons1_; | |
271 | Cons2T cons2_; | |
272 | < | }; |
272 | > | }; |
273 | ||
274 | ||
275 | ||
276 | < | template<typename Cons1T, typename Cons2T> |
277 | < | struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > { |
278 | < | public: |
276 | > | template<typename Cons1T, typename Cons2T> |
277 | > | struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > { |
278 | > | public: |
279 | ||
245 | – | |
280 | OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { | |
281 | < | std::stringstream iss; |
282 | < | iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << ""; |
283 | < | description_ = iss.str(); |
284 | < | } |
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); |
288 | > | return cons1_(data) || cons2_(data); |
289 | } | |
290 | ||
291 | < | private: |
291 | > | private: |
292 | Cons1T cons1_; | |
293 | Cons2T cons2_; | |
294 | < | }; |
294 | > | }; |
295 | ||
296 | < | template<typename ConsT> |
297 | < | struct NotParamConstraint: public ParamConstraintFacade< NotParamConstraint<ConsT> > { |
298 | < | public: |
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" << cons1_.getConstraintDescription() << ")"; |
304 | < | description_ = iss.str(); |
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); |
309 | > | return !cons_(data); |
310 | } | |
311 | ||
312 | < | private: |
312 | > | private: |
313 | ConsT cons_; | |
314 | < | }; |
314 | > | }; |
315 | ||
316 | ||
317 | < | template<typename Cons1T, typename Cons2T> |
318 | < | inline AndParamConstraint<Cons1T, Cons2T> |
319 | < | operator &&(const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) { |
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 | < | } |
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 ) { |
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 | < | } |
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 ) { |
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 | < | } |
341 | > | } |
342 | ||
343 | ||
344 | < | NotEmptyConstraint isNotEmpty() { |
345 | < | return NotEmptyConstraint(); |
312 | < | } |
344 | > | NotEmptyConstraint isNotEmpty(); |
345 | > | ZeroConstraint isZero(); |
346 | ||
347 | < | ZeroConstraint isZero() { |
348 | < | return ZeroConstraint(); |
349 | < | } |
347 | > | ParamConstraintFacade<NonZeroConstraint> isNonZero(); |
348 | > | PositiveConstraint isPositive(); |
349 | > | NonPositiveConstraint isNonPositive(); |
350 | ||
351 | < | ParamConstraintFacade<NonZeroConstraint> isNonZero() { |
319 | < | return ParamConstraintFacade<NonZeroConstraint>(); |
320 | < | } |
351 | > | NegativeConstraint isNegative(); |
352 | ||
353 | < | PositiveConstraint isPositive() { |
354 | < | return PositiveConstraint(); |
324 | < | } |
353 | > | NonNegativeConstraint isNonNegative(); |
354 | > | EvenConstraint isEven(); |
355 | ||
356 | < | NonPositiveConstraint isNonPositive() { |
357 | < | 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 ) { |
356 | > | template<typename T> |
357 | > | inline LessThanConstraint<T>isLessThan(T& v ) { |
358 | return LessThanConstraint<T>(v); | |
359 | < | } |
359 | > | } |
360 | ||
361 | < | template<typename T> |
362 | < | LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) { |
361 | > | template<typename T> |
362 | > | inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) { |
363 | return ParamConstraintFacade<LessThanOrEqualToConstraint<T> >(v); | |
364 | < | } |
364 | > | } |
365 | ||
366 | < | template<typename T> |
367 | < | EqualConstraint<T> isEqual(T& v ) { |
366 | > | template<typename T> |
367 | > | inline EqualConstraint<T> isEqual(T& v ) { |
368 | return EqualConstraint<T>(v); | |
369 | < | } |
369 | > | } |
370 | ||
371 | < | template<typename T> |
372 | < | GreaterThanConstraint<T> isGreaterThan(T& v ) { |
371 | > | template<typename T> |
372 | > | inline GreaterThanConstraint<T> isGreaterThan(T& v ) { |
373 | return GreaterThanConstraint<T>(v); | |
374 | < | } |
374 | > | } |
375 | ||
376 | < | template<typename T> |
377 | < | GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) { |
376 | > | template<typename T> |
377 | > | inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) { |
378 | return GreaterThanOrEqualTo<T>(v); | |
379 | < | } |
379 | > | } |
380 | ||
381 | < | EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str) { |
364 | < | return EqualIgnoreCaseConstraint(str); |
381 | > | EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str); |
382 | } | |
366 | – | |
383 | #endif |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |