From 4f7a6256a9fbbc17aadc5b28c3a30961f6a42b77 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 29 Dec 2012 00:11:23 +0000 Subject: [PATCH] The throwing of exceptions can now be disabled, and in this case a handler function is called instead. --- library/PolyVoxCore/CMakeLists.txt | 1 + .../include/PolyVoxCore/Impl/ErrorHandling.h | 25 ++++++++++++++++++- library/PolyVoxCore/source/Impl/Utility.cpp | 12 ++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index 34deabef..f964a8d5 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -94,6 +94,7 @@ SET(CORE_INC_FILES ) SET(IMPL_SRC_FILES + source/Impl/ErrorHandling.cpp source/Impl/MarchingCubesTables.cpp source/Impl/RandomUnitVectors.cpp source/Impl/RandomVectors.cpp diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h index 88415f74..d3ef8b49 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h @@ -26,8 +26,10 @@ freely, subject to the following restrictions: #include //For std::exit #include //For std::cerr +#include #define POLYVOX_ASSERTS_ENABLED +//#define POLYVOX_THROW_ENABLED #if defined(_MSC_VER) #define POLYVOX_HALT() __debugbreak() @@ -54,7 +56,7 @@ freely, subject to the following restrictions: { \ std::cerr << std::endl << std::endl; \ std::cerr << " PolyVox Assertion Failed!" << std::endl; \ - std::cerr << " -------------------------" << std::endl; \ + std::cerr << " =========================" << std::endl; \ std::cerr << " Condition: " << #condition << std::endl; \ std::cerr << " Message: " << (message) << std::endl; \ std::cerr << " Location: " << "Line " << __LINE__ << " of " << __FILE__ << std::endl << std::endl; \ @@ -98,4 +100,25 @@ freely, subject to the following restrictions: #define POLYVOX_STATIC_ASSERT(condition, message) StaticAssert<(condition)>::ERROR_The_static_assertion_has_failed(); #endif +/* + * Exceptions + * ---------- + * ... + */ +#ifdef POLYVOX_THROW_ENABLED + #define POLYVOX_THROW(type, message) throw type((message)) +#else + namespace PolyVox + { + typedef void (*ThrowHandler)(std::exception& e, const char* file, int line); + + ThrowHandler getThrowHandler(); + void setThrowHandler(ThrowHandler newHandler); + } + + #define POLYVOX_THROW(type, message) \ + type except = (type)((message)); \ + getThrowHandler()((except), __FILE__, __LINE__) +#endif + #endif //__PolyVox_ErrorHandling_H__ diff --git a/library/PolyVoxCore/source/Impl/Utility.cpp b/library/PolyVoxCore/source/Impl/Utility.cpp index 6e907ab9..c82c9630 100644 --- a/library/PolyVoxCore/source/Impl/Utility.cpp +++ b/library/PolyVoxCore/source/Impl/Utility.cpp @@ -21,11 +21,9 @@ freely, subject to the following restrictions: distribution. *******************************************************************************/ +#include "PolyVoxCore/Impl/ErrorHandling.h" #include "PolyVoxCore/Impl/Utility.h" -#include -#include - namespace PolyVox { //Note: this function only works for inputs which are a power of two and not zero @@ -33,17 +31,17 @@ namespace PolyVox uint8_t logBase2(uint32_t uInput) { //Debug mode validation - assert(uInput != 0); - assert(isPowerOf2(uInput)); + POLYVOX_ASSERT(uInput != 0, "Cannot compute the log of zero."); + POLYVOX_ASSERT(isPowerOf2(uInput), "Input must be a power of two in order to compute the log."); //Release mode validation if(uInput == 0) { - throw std::invalid_argument("Cannot compute the log of zero."); + POLYVOX_THROW(std::invalid_argument, "Cannot compute the log of zero."); } if(!isPowerOf2(uInput)) { - throw std::invalid_argument("Input must be a power of two in order to compute the log."); + POLYVOX_THROW(std::invalid_argument, "Input must be a power of two in order to compute the log."); } uint32_t uResult = 0;