Work on new assert macro.

This commit is contained in:
David Williams 2012-12-26 02:03:32 +00:00
parent c78a8595fb
commit c74c1a2b44
2 changed files with 66 additions and 60 deletions

View File

@ -26,6 +26,13 @@ freely, subject to the following restrictions:
#define POLYVOX_ASSERTS_ENABLED #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 PolyVox
{ {
namespace Assert namespace Assert
@ -41,10 +48,7 @@ namespace PolyVox
Handler GetHandler(); Handler GetHandler();
void SetHandler(Handler newHandler); void SetHandler(Handler newHandler);
FailBehavior ReportFailure(const char* condition, FailBehavior ReportFailure(const char* condition, const char* file, int line, const char* msg, ...);
const char* file,
int line,
const char* msg, ...);
} }
} }

View File

@ -3,17 +3,19 @@
#include <cstdio> #include <cstdio>
#include <cstdarg> #include <cstdarg>
/*
* 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 PolyVox
{ {
namespace
namespace {
{ Assert::FailBehavior DefaultHandler(const char* condition, const char* msg, const char* file, const int line)
{
Assert::FailBehavior DefaultHandler(const char* condition,
const char* msg,
const char* file,
const int line)
{
std::printf("%s(%d): Assert Failure: ", file, line); std::printf("%s(%d): Assert Failure: ", file, line);
if (condition != NULL) if (condition != NULL)
@ -25,31 +27,28 @@ Assert::FailBehavior DefaultHandler(const char* condition,
std::printf("\n"); std::printf("\n");
return Assert::Halt; return Assert::Halt;
} }
Assert::Handler& GetAssertHandlerInstance() Assert::Handler& GetAssertHandlerInstance()
{ {
static Assert::Handler s_handler = &DefaultHandler; static Assert::Handler s_handler = &DefaultHandler;
return s_handler; return s_handler;
} }
} }
Assert::Handler Assert::GetHandler() Assert::Handler Assert::GetHandler()
{ {
return GetAssertHandlerInstance(); return GetAssertHandlerInstance();
} }
void Assert::SetHandler(Assert::Handler newHandler) void Assert::SetHandler(Assert::Handler newHandler)
{ {
GetAssertHandlerInstance() = newHandler; GetAssertHandlerInstance() = newHandler;
} }
Assert::FailBehavior Assert::ReportFailure(const char* condition, Assert::FailBehavior Assert::ReportFailure(const char* condition, const char* file, const int line, const char* msg, ...)
const char* file, {
const int line,
const char* msg, ...)
{
const char* message = NULL; const char* message = NULL;
if (msg != NULL) if (msg != NULL)
{ {
@ -57,7 +56,11 @@ Assert::FailBehavior Assert::ReportFailure(const char* condition,
{ {
va_list args; va_list args;
va_start(args, msg); va_start(args, msg);
#if defined (_MSC_VER)
vsnprintf_s(messageBuffer, 1024, msg, args);
#else
vsnprintf(messageBuffer, 1024, msg, args); vsnprintf(messageBuffer, 1024, msg, args);
#endif
va_end(args); va_end(args);
} }
@ -65,6 +68,5 @@ Assert::FailBehavior Assert::ReportFailure(const char* condition,
} }
return GetAssertHandlerInstance()(condition, message, file, line); return GetAssertHandlerInstance()(condition, message, file, line);
} }
} }