From c74c1a2b4456c30e2d4ea9187114522e99f13770 Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 26 Dec 2012 02:03:32 +0000 Subject: [PATCH] Work on new assert macro. --- .../include/PolyVoxCore/Impl/ErrorHandling.h | 12 +- .../polyvoxcore/source/Impl/ErrorHandling.cpp | 114 +++++++++--------- 2 files changed, 66 insertions(+), 60 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h index c3f832a3..f83975e1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h @@ -26,6 +26,13 @@ freely, subject to the following restrictions: #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 @@ -41,10 +48,7 @@ namespace PolyVox Handler GetHandler(); void SetHandler(Handler newHandler); - FailBehavior ReportFailure(const char* condition, - const char* file, - int line, - const char* msg, ...); + FailBehavior ReportFailure(const char* condition, const char* file, int line, const char* msg, ...); } } diff --git a/library/polyvoxcore/source/Impl/ErrorHandling.cpp b/library/polyvoxcore/source/Impl/ErrorHandling.cpp index 40225a44..8e62e9b9 100644 --- a/library/polyvoxcore/source/Impl/ErrorHandling.cpp +++ b/library/polyvoxcore/source/Impl/ErrorHandling.cpp @@ -3,68 +3,70 @@ #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) + namespace { - char messageBuffer[1024]; + Assert::FailBehavior DefaultHandler(const char* condition, const char* msg, const char* file, const int line) { - va_list args; - va_start(args, msg); - vsnprintf(messageBuffer, 1024, msg, args); - va_end(args); + 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; } - message = messageBuffer; } - return GetAssertHandlerInstance()(condition, message, file, line); -} + Assert::Handler Assert::GetHandler() + { + return GetAssertHandlerInstance(); + } -} \ No newline at end of file + 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); + } +}