Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


validator.h
1 /*
2  Evocosm is a C++ framework for implementing evolutionary algorithms.
3 
4  Copyright 2011 Scott Robert Ladd. All rights reserved.
5 
6  Evocosm is user-supported open source software. Its continued development is dependent
7  on financial support from the community. You can provide funding by visiting the Evocosm
8  website at:
9 
10  http://www.coyotegulch.com
11 
12  You may license Evocosm in one of two fashions:
13 
14  1) Simplified BSD License (FreeBSD License)
15 
16  Redistribution and use in source and binary forms, with or without modification, are
17  permitted provided that the following conditions are met:
18 
19  1. Redistributions of source code must retain the above copyright notice, this list of
20  conditions and the following disclaimer.
21 
22  2. Redistributions in binary form must reproduce the above copyright notice, this list
23  of conditions and the following disclaimer in the documentation and/or other materials
24  provided with the distribution.
25 
26  THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
29  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36  The views and conclusions contained in the software and documentation are those of the
37  authors and should not be interpreted as representing official policies, either expressed
38  or implied, of Scott Robert Ladd.
39 
40  2) Closed-Source Proprietary License
41 
42  If your project is a closed-source or proprietary project, the Simplified BSD License may
43  not be appropriate or desirable. In such cases, contact the Evocosm copyright holder to
44  arrange your purchase of an appropriate license.
45 
46  The author can be contacted at:
47 
48  scott.ladd@coyotegulch.com
49  scott.ladd@gmail.com
50  http:www.coyotegulch.com
51 */
52 
53 #if !defined(LIBEVOCOSM_VALIDATOR_H)
54 #define LIBEVOCOSM_VALIDATOR_H
55 
56 #include <sstream>
57 #include <stdexcept>
58 #include <typeinfo>
59 
60 namespace libevocosm
61 {
62  using std::stringstream;
63  using std::string;
64  using std::runtime_error;
65 
67 
75  template <typename Type>
76  class validation_error : public runtime_error
77  {
78  private:
79  static string build_error_string(const Type & object,
80  const string & details)
81  {
82  stringstream message;
83 
84  message << "validation error: "
85  << typeid(object).name() << " " << object
86  << details;
87 
88  return message.str();
89  }
90 
91  public:
93 
104  validation_error(const Type & object,
105  const string & details = string())
106  : runtime_error(build_error_string(object,details))
107  {
108  // nada
109  }
110  };
111 
113 
121  template <typename Type>
122  void validate_equals(const Type & object,
123  const Type & constraint,
124  const string & message = string())
125  {
126  if (object != constraint)
127  {
128  stringstream details;
129  details << " must equal " << constraint << " " << message;
130  throw validation_error<Type>(object,details.str());
131  }
132  }
133 
135 
143  template <typename Type>
144  void validate_not(const Type & object,
145  const Type & constraint,
146  const string & message = string())
147  {
148  if (object == constraint)
149  {
150  stringstream details;
151  details << " must not equal " << constraint << " " << message;
152  throw validation_error<Type>(object,details.str());
153  }
154  }
155 
157 
165  template <typename Type>
166  void validate_less(const Type & object,
167  const Type & constraint,
168  const string & message = string())
169  {
170  if (object >= constraint)
171  {
172  stringstream details;
173  details << " must be less than " << constraint << " " << message;
174  throw validation_error<Type>(object,details.str());
175  }
176  }
177 
179 
187  template <typename Type>
188  void validate_less_eq(const Type & object,
189  const Type & constraint,
190  const string & message = string())
191  {
192  if (object > constraint)
193  {
194  stringstream details;
195  details << " must be less than " << constraint << " " << message;
196  throw validation_error<Type>(object,details.str());
197  }
198  }
199 
201 
209  template <typename Type>
210  void validate_greater(const Type & object,
211  const Type & constraint,
212  const string & message = string())
213  {
214  if (object <= constraint)
215  {
216  stringstream details;
217  details << " must be greater than " << constraint << " " << message;
218  throw validation_error<Type>(object,details.str());
219  }
220  }
221 
223 
231  template <typename Type>
232  void validate_greater_eq(const Type & object,
233  const Type & constraint,
234  const string & message = string())
235  {
236  if (object < constraint)
237  {
238  stringstream details;
239  details << " must be greater than " << constraint << " " << message;
240  throw validation_error<Type>(object,details.str());
241  }
242  }
243 
245 
256  template <typename Type>
257  void validate_range(const Type & object,
258  const Type & low_bound,
259  const Type & high_bound,
260  const string & message = string())
261  {
262  if ((object < low_bound) || (object > high_bound))
263  {
264  stringstream details;
265  details << " must be between " << low_bound << " and "
266  << high_bound << " " << message;
267  throw validation_error<Type>(object,details.str());
268  }
269  }
270 
272 
283  template <typename Type, typename Predicate>
284  void validate_with(const Type & object,
285  const Predicate & constraint,
286  const string & message = string())
287  {
288  if (!constraint(object))
289  {
290  stringstream details;
291  details << " failed test " << typeid(constraint).name() << " " << message;
292  throw validation_error<Type>(object,details.str());
293  }
294  }
295 
297 
303  template <typename Type>
304  void enforce_lower_limit(Type & object,
305  const Type & low_value)
306  {
307  if (object < low_value)
308  object = low_value;
309  }
310 
312 
318  template <typename Type>
319  void enforce_upper_limit(Type & object,
320  const Type & high_value)
321  {
322  if (object > high_value)
323  object = high_value;
324  }
325 
327 
333  template <typename Type>
334  void enforce_index(Type & object,
335  const Type & array_length)
336  {
337  if (object >= array_length)
338  object = array_length - 1;
339 
340  if (object < 0)
341  object = 0;
342  }
343 
345 
354  template <typename Type>
355  void enforce_range(Type & object,
356  const Type & low_value,
357  const Type & high_value)
358  {
359  if (object < low_value)
360  object = low_value;
361  else if (object > high_value)
362  object = high_value;
363  }
364 
366 
375  inline string build_location_string(const char * filename, long line_no)
376  {
377  stringstream text;
378  text << "in " << filename << ", line " << line_no;
379  return text.str();
380  }
381 }
382 
383 // These macros allow validation to be included on a per-compile basis, based on the settings
384 // of the DEBUG and NDEBUG preprocessor macros.
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__)
395 #else
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()
405 #endif
406 
407 #endif
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

© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.