feisty meow concerns codebase  2.140
guards.h
Go to the documentation of this file.
1 #ifndef GUARDS_GROUP
2 #define GUARDS_GROUP
3 
5 // Name : guards
6 // Author : Chris Koeritz
8 // Copyright (c) 1989-$now By Author. This program is free software; you can
9 // redistribute it and/or modify it under the terms of the GNU General Public
10 // License as published by the Free Software Foundation:
11 // http://www.gnu.org/licenses/gpl.html
12 // or under the terms of the GNU Library license:
13 // http://www.gnu.org/licenses/lgpl.html
14 // at your preference. Those licenses describe your legal rights to this
15 // software, and no other rights or warranties apply.
16 // Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
18 
20 
26 namespace basis {
27 
28 // forward declaration.
29 class astring;
30 class base_string;
31 
33 
34 // simpler guards first...
35 
37 template <class contents>
38 bool in_range(const contents &value, const contents &low, const contents &high)
39 { return !( (high < low) || (value < low) || (value > high) ); }
40 
42 
44 
48 #define bounds_return(value, low, high, to_return) \
49  { if (!basis::in_range(value, low, high)) return to_return; }
50 
52 
54 
59 #ifdef CATCH_ERRORS
60  #define bounds_halt(value, low, high, to_return) { \
61  if (((value) < (low)) || ((value) > (high))) { \
62  throw_error(basis::astring(static_class_name()), basis::astring(func), \
63  basis::astring("value ") + #value \
64  + " was not in range " + #low + " to " + #high \
65  + " at " + __WHERE__); \
66  return to_return; \
67  } \
68  }
69 #else
70  #define bounds_halt(a, b, c, d) bounds_return(a, b, c, d)
71 #endif
72 
74 
76 void format_error(const base_string &class_name, const base_string &func_name,
77  const base_string &error_message, base_string &to_fill);
78 
80 void throw_error(const base_string &class_name, const base_string &func_name,
81  const base_string &error_message);
82 
84 void throw_error(const astring &class_name, const astring &func_name,
85  const astring &error_message);
86 
88 
89 } // namespace.
90 
91 #endif
92 
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void throw_error(const base_string &class_name, const base_string &func_name, const base_string &error_message)
throws an error that incorporates the class name and function name.
Definition: guards.cpp:32
bool in_range(const contents &value, const contents &low, const contents &high)
Returns true if the value is within the range specified.
Definition: guards.h:38
void format_error(const base_string &class_name, const base_string &func_name, const base_string &error_message, base_string &to_fill)
writes a string "to_fill" in a nicely formatted manner using the class and function names.
Definition: guards.cpp:21