From e17271a2c72427d1c936dd4823f2572d9bdc41f4 Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 26 Dec 2012 10:34:09 +0000 Subject: [PATCH] Work on new asserts. --- library/PolyVoxCore/CMakeLists.txt | 1 - .../include/PolyVoxCore/Impl/ErrorHandling.h | 57 ++++++--------- .../polyvoxcore/source/Impl/ErrorHandling.cpp | 72 ------------------- 3 files changed, 21 insertions(+), 109 deletions(-) delete mode 100644 library/polyvoxcore/source/Impl/ErrorHandling.cpp diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index 201d8ab5..db97c3f3 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -94,7 +94,6 @@ 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 f83975e1..b99f0278 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h @@ -24,58 +24,43 @@ freely, subject to the following restrictions: #ifndef __PolyVox_ErrorHandling_H__ #define __PolyVox_ErrorHandling_H__ +#include + #define POLYVOX_ASSERTS_ENABLED -/* - * Assertions - * ---------- - * The code below implements a custom assert function called POLYVOX_ASSERT which has a number of advantages compared to - * the standard C/C++ assert(). It is based on http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ which - * provides code under the MIT license. - */ -namespace PolyVox -{ - namespace Assert - { - enum FailBehavior - { - Halt, - Continue, - }; - - typedef FailBehavior (*Handler)(const char* condition, const char* msg, const char* file, int line); - - Handler GetHandler(); - void SetHandler(Handler newHandler); - - FailBehavior ReportFailure(const char* condition, const char* file, int line, const char* msg, ...); - } -} - #define POLYVOX_HALT() __debugbreak() #define POLYVOX_UNUSED(x) do { (void)sizeof(x); } while(0) +/* + * Assertions + * ---------- + * The code below implements a custom assert function called POLYVOX_ASSERT which has a number of advantages compared + * to the standard C/C++ assert(). It is inspired by http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ + * which provides code under the MIT license. + */ + #ifdef POLYVOX_ASSERTS_ENABLED - #define POLYVOX_ASSERT(cond, msg, ...) \ + #define POLYVOX_ASSERT(condition, message) \ do \ { \ - if (!(cond)) \ + if (!(condition)) \ { \ - if (PolyVox::Assert::ReportFailure(#cond, __FILE__, __LINE__, (msg), __VA_ARGS__) == \ - PolyVox::Assert::Halt) \ - POLYVOX_HALT(); \ + std::cerr << std::endl << std::endl; \ + std::cerr << " PolyVox Assertion Failed!" << 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; \ + POLYVOX_HALT(); \ } \ } while(0) #else - #define POLYVOX_ASSERT(condition, msg, ...) \ - do { POLYVOX_UNUSED(condition); POLYVOX_UNUSED(msg); } while(0) + #define POLYVOX_ASSERT(condition, message) \ + do { POLYVOX_UNUSED(condition); POLYVOX_UNUSED(message); } while(0) #endif -#define POLYVOX_STATIC_ASSERT(x) \ - typedef char polyvoxStaticAssert[(x) ? 1 : -1]; - #endif //__PolyVox_ErrorHandling_H__ diff --git a/library/polyvoxcore/source/Impl/ErrorHandling.cpp b/library/polyvoxcore/source/Impl/ErrorHandling.cpp deleted file mode 100644 index 8e62e9b9..00000000 --- a/library/polyvoxcore/source/Impl/ErrorHandling.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "PolyVoxCore/Impl/ErrorHandling.h" - -#include -#include - -/* - * Assertions - * ---------- - * The code below implements a custom assert function called POLYVOX_ASSERT which has a number of advantages compared to - * the standard C/C++ assert(). It is based on http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ which - * provides code under the MIT license. - */ -namespace PolyVox -{ - namespace - { - Assert::FailBehavior DefaultHandler(const char* condition, const char* msg, const char* file, const int line) - { - std::printf("%s(%d): Assert Failure: ", file, line); - - if (condition != NULL) - std::printf("'%s' ", condition); - - if (msg != NULL) - std::printf("%s", msg); - - std::printf("\n"); - - return Assert::Halt; - } - - Assert::Handler& GetAssertHandlerInstance() - { - static Assert::Handler s_handler = &DefaultHandler; - return s_handler; - } - - } - - Assert::Handler Assert::GetHandler() - { - return GetAssertHandlerInstance(); - } - - void Assert::SetHandler(Assert::Handler newHandler) - { - GetAssertHandlerInstance() = newHandler; - } - - Assert::FailBehavior Assert::ReportFailure(const char* condition, const char* file, const int line, const char* msg, ...) - { - const char* message = NULL; - if (msg != NULL) - { - char messageBuffer[1024]; - { - va_list args; - va_start(args, msg); -#if defined (_MSC_VER) - vsnprintf_s(messageBuffer, 1024, msg, args); -#else - vsnprintf(messageBuffer, 1024, msg, args); -#endif - va_end(args); - } - - message = messageBuffer; - } - - return GetAssertHandlerInstance()(condition, message, file, line); - } -}