diff options
author | lloyd <[email protected]> | 2010-09-26 16:44:01 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-09-26 16:44:01 +0000 |
commit | edd522e5ceb31180eed22c2f1bcae50e4f79c2ae (patch) | |
tree | e6bbf6742f1eebd68cfc014bd3b220eea450805b /src | |
parent | ecc8ad2244477830c802610016f02a9d5bba8d3e (diff) |
There is a pretty common pattern in the code for testing for internal
errors of the form
if(some_expr_indicating_failure)
throw Internal_Error("Some mildly informative message");
Make this simpiler with the addition of a BOTAN_ASSERT macro which will
throw an exception upon failure.
Diffstat (limited to 'src')
-rw-r--r-- | src/utils/assert.cpp | 35 | ||||
-rw-r--r-- | src/utils/assert.h | 49 | ||||
-rw-r--r-- | src/utils/info.txt | 2 |
3 files changed, 86 insertions, 0 deletions
diff --git a/src/utils/assert.cpp b/src/utils/assert.cpp new file mode 100644 index 000000000..29af831d8 --- /dev/null +++ b/src/utils/assert.cpp @@ -0,0 +1,35 @@ +/* +* Runtime assertion checking +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/internal/assert.h> +#include <botan/exceptn.h> +#include <sstream> + +namespace Botan { + +void assertion_failure(const char* expr_str, + const char* msg, + const char* func, + const char* file, + int line) + { + std::ostringstream format; + + format << "Assertion " << expr_str << " failed "; + + if(msg) + format << "(" << msg << ") "; + + if(func) + format << "in " << func << " "; + + format << "@" << file << ":" << line; + + throw Internal_Error(format.str()); + } + +} diff --git a/src/utils/assert.h b/src/utils/assert.h new file mode 100644 index 000000000..d84f5c7ad --- /dev/null +++ b/src/utils/assert.h @@ -0,0 +1,49 @@ +/* +* Runtime assertion checking +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ASSERTION_CHECKING_H__ +#define BOTAN_ASSERTION_CHECKING_H__ + +namespace Botan { + +void assertion_failure(const char* expr_str, + const char* msg, + const char* func, + const char* file, + int line); + +#define BOTAN_ASSERT(expr, msg) \ + do { \ + if(!(expr)) \ + Botan::assertion_failure(#expr, \ + msg, \ + BOTAN_ASSERT_FUNCTION, \ + __FILE__, \ + __LINE__); \ + } while(0) + +/* +* Unfortunately getting the function name from the preprocessor +* isn't standard in C++98 (C++0x uses C99's __func__) +*/ +#if defined(BOTAN_BUILD_COMPILER_IS_GCC) || \ + defined(BOTAN_BUILD_COMPILER_IS_CLANG) || \ + defined(BOTAN_BUILD_COMPILER_IS_INTEL) + + #define BOTAN_ASSERT_FUNCTION __PRETTY_FUNCTION__ + +#elif defined(BOTAN_BUILD_COMPILER_IS_MSVC) + + #define BOTAN_ASSERT_FUNCTION __FUNCTION__ + +#else + #define BOTAN_ASSERT_FUNCTION ((const char*)0) +#endif + +} + +#endif diff --git a/src/utils/info.txt b/src/utils/info.txt index 2fb3e79a5..9d8e6173c 100644 --- a/src/utils/info.txt +++ b/src/utils/info.txt @@ -3,6 +3,7 @@ define UTIL_FUNCTIONS load_on always <source> +assert.cpp charset.cpp cpuid.cpp mlock.cpp @@ -13,6 +14,7 @@ version.cpp </source> <header:internal> +assert.h bit_ops.h debug.h mlock.h |