| 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 |
| 66 |
|
|
| 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; |
| 74 |
|
}; |
| 75 |
|
|
| 76 |
|
struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{ |
| 77 |
< |
NonZeroConstraint() {description_ = "nonzero";} |
| 77 |
> |
NonZeroConstraint() {this->description_ = "nonzero";} |
| 78 |
|
|
| 79 |
|
template<typename DataType> |
| 80 |
|
bool operator()( DataType data ) const { |
| 83 |
|
}; |
| 84 |
|
|
| 85 |
|
struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{ |
| 86 |
< |
PositiveConstraint() {description_ = "positive";} |
| 86 |
> |
PositiveConstraint() {this->description_ = "positive";} |
| 87 |
|
template<typename DataType> |
| 88 |
|
bool operator()( DataType data ) const |
| 89 |
|
{ |
| 92 |
|
}; |
| 93 |
|
|
| 94 |
|
struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{ |
| 95 |
< |
NonPositiveConstraint() {description_ = "nonpositive";} |
| 95 |
> |
NonPositiveConstraint() {this->description_ = "nonpositive";} |
| 96 |
|
template<typename DataType> |
| 97 |
|
bool operator()( DataType data ) const |
| 98 |
|
{ |
| 101 |
|
}; |
| 102 |
|
|
| 103 |
|
struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{ |
| 104 |
< |
NegativeConstraint() {description_ = "negative";} |
| 104 |
> |
NegativeConstraint() {this->description_ = "negative";} |
| 105 |
|
template<typename DataType> |
| 106 |
|
bool operator()( DataType data ) const |
| 107 |
|
{ |
| 110 |
|
}; |
| 111 |
|
|
| 112 |
|
struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{ |
| 113 |
< |
NonNegativeConstraint() {description_ = "nonnegative";} |
| 113 |
> |
NonNegativeConstraint() {this->description_ = "nonnegative";} |
| 114 |
|
template<typename DataType> |
| 115 |
|
bool operator()( DataType data ) const |
| 116 |
|
{ |
| 124 |
|
LessThanConstraint(T rhs) : rhs_(rhs){ |
| 125 |
|
std::stringstream iss; |
| 126 |
|
iss << "less than " << rhs; |
| 127 |
< |
description_ = iss.str(); |
| 127 |
> |
this->description_ = iss.str(); |
| 128 |
|
} |
| 129 |
|
template<typename DataType> |
| 130 |
|
bool operator()( DataType data ) const { |
| 140 |
|
LessThanOrEqualToConstraint(T rhs) : rhs_(rhs) { |
| 141 |
|
std::stringstream iss; |
| 142 |
|
iss << "less than or equal to" << rhs; |
| 143 |
< |
description_ = iss.str(); |
| 143 |
> |
this->description_ = iss.str(); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
template<typename DataType> |
| 158 |
|
EqualConstraint(T rhs) : rhs_(rhs){ |
| 159 |
|
std::stringstream iss; |
| 160 |
|
iss << "equal to" << rhs; |
| 161 |
< |
description_ = iss.str(); |
| 161 |
> |
this->description_ = iss.str(); |
| 162 |
|
|
| 163 |
|
} |
| 164 |
|
template<typename DataType> |
| 174 |
|
EqualIgnoreCaseConstraint(std::string rhs) : rhs_(oopse::toUpperCopy(rhs)){ |
| 175 |
|
std::stringstream iss; |
| 176 |
|
iss << "equal to (case insensitive) " << rhs; |
| 177 |
< |
description_ = iss.str(); |
| 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> |
| 214 |
|
GreaterThanConstraint(T rhs) : rhs_(rhs){ |
| 215 |
|
std::stringstream iss; |
| 216 |
|
iss << "greater than" << rhs; |
| 217 |
< |
description_ = iss.str(); |
| 217 |
> |
this->description_ = iss.str(); |
| 218 |
|
} |
| 219 |
|
template<typename DataType> |
| 220 |
|
bool operator()( DataType data ) const { |
| 230 |
|
GreaterThanOrEqualTo(T rhs) : rhs_(rhs){ |
| 231 |
|
std::stringstream iss; |
| 232 |
|
iss << "greater than or equal to" << rhs; |
| 233 |
< |
description_ = iss.str(); |
| 233 |
> |
this->description_ = iss.str(); |
| 234 |
|
} |
| 235 |
|
template<typename DataType> |
| 236 |
|
bool operator()( DataType data ) const { |
| 248 |
|
AndParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { |
| 249 |
|
std::stringstream iss; |
| 250 |
|
iss << "(" << cons1_.getConstraintDescription() << " and " << cons2_.getConstraintDescription() << ")"; |
| 251 |
< |
description_ = iss.str(); |
| 251 |
> |
this->description_ = iss.str(); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
|
template<typename DataType> |
| 267 |
|
struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > { |
| 268 |
|
public: |
| 269 |
|
|
| 245 |
– |
|
| 270 |
|
OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { |
| 271 |
|
std::stringstream iss; |
| 272 |
|
iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << ""; |
| 273 |
< |
description_ = iss.str(); |
| 273 |
> |
this->description_ = iss.str(); |
| 274 |
|
} |
| 275 |
|
|
| 276 |
|
template<typename DataType> |
| 290 |
|
|
| 291 |
|
NotParamConstraint( ConsT cons) : cons_(cons) { |
| 292 |
|
std::stringstream iss; |
| 293 |
< |
iss << "(not" << cons1_.getConstraintDescription() << ")"; |
| 294 |
< |
description_ = iss.str(); |
| 293 |
> |
iss << "(not" << cons_.getConstraintDescription() << ")"; |
| 294 |
> |
this->description_ = iss.str(); |
| 295 |
|
} |
| 296 |
|
|
| 297 |
|
template<typename DataType> |
| 331 |
|
} |
| 332 |
|
|
| 333 |
|
|
| 334 |
< |
NotEmptyConstraint isNotEmpty() { |
| 335 |
< |
return NotEmptyConstraint(); |
| 312 |
< |
} |
| 334 |
> |
NotEmptyConstraint isNotEmpty(); |
| 335 |
> |
ZeroConstraint isZero(); |
| 336 |
|
|
| 337 |
< |
ZeroConstraint isZero() { |
| 338 |
< |
return ZeroConstraint(); |
| 339 |
< |
} |
| 337 |
> |
ParamConstraintFacade<NonZeroConstraint> isNonZero(); |
| 338 |
> |
PositiveConstraint isPositive(); |
| 339 |
> |
NonPositiveConstraint isNonPositive(); |
| 340 |
|
|
| 341 |
< |
ParamConstraintFacade<NonZeroConstraint> isNonZero() { |
| 319 |
< |
return ParamConstraintFacade<NonZeroConstraint>(); |
| 320 |
< |
} |
| 341 |
> |
NegativeConstraint isNegative(); |
| 342 |
|
|
| 343 |
< |
PositiveConstraint isPositive() { |
| 323 |
< |
return PositiveConstraint(); |
| 324 |
< |
} |
| 343 |
> |
NonNegativeConstraint isNonNegative(); |
| 344 |
|
|
| 326 |
– |
NonPositiveConstraint isNonPositive() { |
| 327 |
– |
return NonPositiveConstraint(); |
| 328 |
– |
} |
| 329 |
– |
|
| 330 |
– |
NegativeConstraint isNegative() { |
| 331 |
– |
return NegativeConstraint(); |
| 332 |
– |
} |
| 333 |
– |
|
| 334 |
– |
NonNegativeConstraint isNonNegative() { |
| 335 |
– |
return NonNegativeConstraint(); |
| 336 |
– |
} |
| 337 |
– |
|
| 345 |
|
template<typename T> |
| 346 |
< |
LessThanConstraint<T>isLessThan(T& v ) { |
| 346 |
> |
inline LessThanConstraint<T>isLessThan(T& v ) { |
| 347 |
|
return LessThanConstraint<T>(v); |
| 348 |
|
} |
| 349 |
|
|
| 350 |
|
template<typename T> |
| 351 |
< |
LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) { |
| 351 |
> |
inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) { |
| 352 |
|
return ParamConstraintFacade<LessThanOrEqualToConstraint<T> >(v); |
| 353 |
|
} |
| 354 |
|
|
| 355 |
|
template<typename T> |
| 356 |
< |
EqualConstraint<T> isEqual(T& v ) { |
| 356 |
> |
inline EqualConstraint<T> isEqual(T& v ) { |
| 357 |
|
return EqualConstraint<T>(v); |
| 358 |
|
} |
| 359 |
|
|
| 360 |
|
template<typename T> |
| 361 |
< |
GreaterThanConstraint<T> isGreaterThan(T& v ) { |
| 361 |
> |
inline GreaterThanConstraint<T> isGreaterThan(T& v ) { |
| 362 |
|
return GreaterThanConstraint<T>(v); |
| 363 |
|
} |
| 364 |
|
|
| 365 |
|
template<typename T> |
| 366 |
< |
GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) { |
| 366 |
> |
inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) { |
| 367 |
|
return GreaterThanOrEqualTo<T>(v); |
| 368 |
|
} |
| 369 |
|
|
| 370 |
< |
EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str) { |
| 364 |
< |
return EqualIgnoreCaseConstraint(str); |
| 370 |
> |
EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str); |
| 371 |
|
} |
| 366 |
– |
|
| 372 |
|
#endif |