36 #if !defined(LIBCOYOTL_VALIDATOR_H)
37 #define LIBCOYOTL_VALIDATOR_H
45 using std::stringstream;
47 using std::runtime_error;
58 template <
typename Type>
62 static string build_error_string(
const Type &
object,
63 const string & details)
67 message <<
"validation error: "
68 <<
typeid(object).name() <<
" " <<
object
88 const string & details =
string())
89 : runtime_error(build_error_string(object,details))
104 template <
typename Type>
105 void validate_equals(
const Type &
object,
106 const Type & constraint,
107 const string & message =
string())
109 if (
object != constraint)
111 stringstream details;
112 details <<
" must equal " << constraint <<
" " << message;
113 throw validation_error<Type>(object,details.str());
126 template <
typename Type>
127 void validate_not(
const Type &
object,
128 const Type & constraint,
129 const string & message =
string())
131 if (
object == constraint)
133 stringstream details;
134 details <<
" must not equal " << constraint <<
" " << message;
135 throw validation_error<Type>(object,details.str());
148 template <
typename Type>
149 void validate_less(
const Type &
object,
150 const Type & constraint,
151 const string & message =
string())
153 if (
object >= constraint)
155 stringstream details;
156 details <<
" must be less than " << constraint <<
" " << message;
157 throw validation_error<Type>(object,details.str());
170 template <
typename Type>
171 void validate_less_eq(
const Type &
object,
172 const Type & constraint,
173 const string & message =
string())
175 if (
object > constraint)
177 stringstream details;
178 details <<
" must be less than " << constraint <<
" " << message;
179 throw validation_error<Type>(object,details.str());
192 template <
typename Type>
193 void validate_greater(
const Type &
object,
194 const Type & constraint,
195 const string & message =
string())
197 if (
object <= constraint)
199 stringstream details;
200 details <<
" must be greater than " << constraint <<
" " << message;
201 throw validation_error<Type>(object,details.str());
214 template <
typename Type>
215 void validate_greater_eq(
const Type &
object,
216 const Type & constraint,
217 const string & message =
string())
219 if (
object < constraint)
221 stringstream details;
222 details <<
" must be greater than " << constraint <<
" " << message;
223 throw validation_error<Type>(object,details.str());
239 template <
typename Type>
240 void validate_range(
const Type &
object,
241 const Type & low_bound,
242 const Type & high_bound,
243 const string & message =
string())
245 if ((
object < low_bound) || (
object > high_bound))
247 stringstream details;
248 details <<
" must be between " << low_bound <<
" and "
249 << high_bound <<
" " << message;
250 throw validation_error<Type>(object,details.str());
266 template <
typename Type,
typename Predicate>
267 void validate_with(
const Type &
object,
268 const Predicate & constraint,
269 const string & message =
string())
271 if (!constraint(
object))
273 stringstream details;
274 details <<
" failed test " <<
typeid(constraint).name() <<
" " << message;
275 throw validation_error<Type>(object,details.str());
286 template <
typename Type>
287 void enforce_lower_limit(Type &
object,
288 const Type & low_value)
290 if (
object < low_value)
301 template <
typename Type>
302 void enforce_upper_limit(Type &
object,
303 const Type & high_value)
305 if (
object > high_value)
319 template <
typename Type>
320 void enforce_range(Type &
object,
321 const Type & low_value,
322 const Type & high_value)
324 if (
object < low_value)
326 else if (
object > high_value)
340 inline string build_location_string(
const char * filename,
long line_no)
343 text <<
"in " << filename <<
", line " << line_no;
350 #if defined(_DEBUG) && !defined(NDEBUG)
351 #define LIBCOYOTL_VALIDATE_EQUALS(object,constraint,details) libcoyotl::validate_equals(object,constraint,details)
352 #define LIBCOYOTL_VALIDATE_NOT(object,constraint,details) libcoyotl::validate_not(object,constraint,details)
353 #define LIBCOYOTL_VALIDATE_LESS(object,constraint,details) libcoyotl::validate_less(object,constraint,details)
354 #define LIBCOYOTL_VALIDATE_LESS_EQ(object,constraint,details) libcoyotl::validate_less_eq(object,constraint,details)
355 #define LIBCOYOTL_VALIDATE_GREATER(object,constraint,details) libcoyotl::validate_greater(object,constraint,details)
356 #define LIBCOYOTL_VALIDATE_GREATER_EQ(object,constraint,details) libcoyotl::validate_greater_eq(object,constraint,details)
357 #define LIBCOYOTL_VALIDATE_RANGE(object,low_bound,high_bound,details) libcoyotl::validate_range(object,low_bound,high_bound,details)
358 #define LIBCOYOTL_VALIDATE_WITH(object,constraint,details) libcoyotl::validate_with(object,constraint,details)
359 #define LIBCOYOTL_LOCATION libcoyotl::build_location_string(__FILE__,__LINE__)
361 #define LIBCOYOTL_VALIDATE_EQUALS(object,constraint,details)
362 #define LIBCOYOTL_VALIDATE_NOT(object,constraint,details)
363 #define LIBCOYOTL_VALIDATE_LESS(object,constraint,details)
364 #define LIBCOYOTL_VALIDATE_LESS_EQ(object,constraint,details)
365 #define LIBCOYOTL_VALIDATE_GREATER(object,constraint,details)
366 #define LIBCOYOTL_VALIDATE_GREATER_EQ(object,constraint,details)
367 #define LIBCOYOTL_VALIDATE_RANGE(object,low_bound,high_bound,details)
368 #define LIBCOYOTL_VALIDATE_WITH(object,constraint,details)
369 #define LIBCOYOTL_LOCATION std::string()