53 #if !defined(LIBEVOCOSM_VALIDATOR_H)
54 #define LIBEVOCOSM_VALIDATOR_H
62 using std::stringstream;
64 using std::runtime_error;
75 template <
typename Type>
79 static string build_error_string(
const Type &
object,
80 const string & details)
84 message <<
"validation error: "
85 <<
typeid(object).name() <<
" " <<
object
105 const string & details =
string())
106 : runtime_error(build_error_string(object,details))
121 template <
typename Type>
123 const Type & constraint,
124 const string & message =
string())
126 if (
object != constraint)
128 stringstream details;
129 details <<
" must equal " << constraint <<
" " << message;
143 template <
typename Type>
145 const Type & constraint,
146 const string & message =
string())
148 if (
object == constraint)
150 stringstream details;
151 details <<
" must not equal " << constraint <<
" " << message;
165 template <
typename Type>
167 const Type & constraint,
168 const string & message =
string())
170 if (
object >= constraint)
172 stringstream details;
173 details <<
" must be less than " << constraint <<
" " << message;
187 template <
typename Type>
189 const Type & constraint,
190 const string & message =
string())
192 if (
object > constraint)
194 stringstream details;
195 details <<
" must be less than " << constraint <<
" " << message;
209 template <
typename Type>
211 const Type & constraint,
212 const string & message =
string())
214 if (
object <= constraint)
216 stringstream details;
217 details <<
" must be greater than " << constraint <<
" " << message;
231 template <
typename Type>
233 const Type & constraint,
234 const string & message =
string())
236 if (
object < constraint)
238 stringstream details;
239 details <<
" must be greater than " << constraint <<
" " << message;
256 template <
typename Type>
258 const Type & low_bound,
259 const Type & high_bound,
260 const string & message =
string())
262 if ((
object < low_bound) || (
object > high_bound))
264 stringstream details;
265 details <<
" must be between " << low_bound <<
" and "
266 << high_bound <<
" " << message;
283 template <
typename Type,
typename Predicate>
285 const Predicate & constraint,
286 const string & message =
string())
288 if (!constraint(
object))
290 stringstream details;
291 details <<
" failed test " <<
typeid(constraint).name() <<
" " << message;
303 template <
typename Type>
305 const Type & low_value)
307 if (
object < low_value)
318 template <
typename Type>
320 const Type & high_value)
322 if (
object > high_value)
333 template <
typename Type>
335 const Type & array_length)
337 if (
object >= array_length)
338 object = array_length - 1;
354 template <
typename Type>
356 const Type & low_value,
357 const Type & high_value)
359 if (
object < low_value)
361 else if (
object > high_value)
378 text <<
"in " << filename <<
", line " << line_no;
385 #if defined(_DEBUG) && !defined(NDEBUG)
386 #define LIBEVOCOSM_VALIDATE_EQUALS(object,constraint,details) libevocosm::validate_equals(object,constraint,details)
387 #define LIBEVOCOSM_VALIDATE_NOT(object,constraint,details) libevocosm::validate_not(object,constraint,details)
388 #define LIBEVOCOSM_VALIDATE_LESS(object,constraint,details) libevocosm::validate_less(object,constraint,details)
389 #define LIBEVOCOSM_VALIDATE_LESS_EQ(object,constraint,details) libevocosm::validate_less_eq(object,constraint,details)
390 #define LIBEVOCOSM_VALIDATE_GREATER(object,constraint,details) libevocosm::validate_greater(object,constraint,details)
391 #define LIBEVOCOSM_VALIDATE_GREATER_EQ(object,constraint,details) libevocosm::validate_greater_eq(object,constraint,details)
392 #define LIBEVOCOSM_VALIDATE_RANGE(object,low_bound,high_bound,details) libevocosm::validate_range(object,low_bound,high_bound,details)
393 #define LIBEVOCOSM_VALIDATE_WITH(object,constraint,details) libevocosm::validate_with(object,constraint,details)
394 #define LIBEVOCOSM_LOCATION libevocosm::build_location_string(__FILE__,__LINE__)
396 #define LIBEVOCOSM_VALIDATE_EQUALS(object,constraint,details)
397 #define LIBEVOCOSM_VALIDATE_NOT(object,constraint,details)
398 #define LIBEVOCOSM_VALIDATE_LESS(object,constraint,details)
399 #define LIBEVOCOSM_VALIDATE_LESS_EQ(object,constraint,details)
400 #define LIBEVOCOSM_VALIDATE_GREATER(object,constraint,details)
401 #define LIBEVOCOSM_VALIDATE_GREATER_EQ(object,constraint,details)
402 #define LIBEVOCOSM_VALIDATE_RANGE(object,low_bound,high_bound,details)
403 #define LIBEVOCOSM_VALIDATE_WITH(object,constraint,details)
404 #define LIBEVOCOSM_LOCATION std::string()
void enforce_index(Type &object, const Type &array_length)
Enforce an maximum index on the value of an object.
Definition: validator.h:334
void validate_not(const Type &object, const Type &constraint, const string &message=string())
Validates that an object does not have a specific value.
Definition: validator.h:144
void validate_range(const Type &object, const Type &low_bound, const Type &high_bound, const string &message=string())
Validates that an object has a value in a specified range.
Definition: validator.h:257
void validate_less(const Type &object, const Type &constraint, const string &message=string())
Validates that an object is less than constraint.
Definition: validator.h:166
void enforce_range(Type &object, const Type &low_value, const Type &high_value)
Enforce an range limit on the value of an object.
Definition: validator.h:355
void validate_less_eq(const Type &object, const Type &constraint, const string &message=string())
Validates that an object is less than or equal to constraint.
Definition: validator.h:188
void validate_equals(const Type &object, const Type &constraint, const string &message=string())
Validates that an object has a specific value.
Definition: validator.h:122
void enforce_lower_limit(Type &object, const Type &low_value)
Enforce a lower limit on the value of an object.
Definition: validator.h:304
A toolkit and framework for implementing evolutionary algorithms.
Definition: analyzer.h:60
void validate_greater_eq(const Type &object, const Type &constraint, const string &message=string())
Validates that an object is greater than or equal to constraint.
Definition: validator.h:232
void validate_with(const Type &object, const Predicate &constraint, const string &message=string())
Validates an object with a given predicate.
Definition: validator.h:284
void enforce_upper_limit(Type &object, const Type &high_value)
Enforce an upper limit on the value of an object.
Definition: validator.h:319
void validate_greater(const Type &object, const Type &constraint, const string &message=string())
Validates that an object is greater than constraint.
Definition: validator.h:210
Standard validation exception.
Definition: validator.h:76
string build_location_string(const char *filename, long line_no)
Utility function to create a location string.
Definition: validator.h:375
validation_error(const Type &object, const string &details=string())
Constructor.
Definition: validator.h:104