diff options
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 |