Work on new asserts.

This commit is contained in:
David Williams 2012-12-26 10:34:09 +00:00
parent c74c1a2b44
commit e17271a2c7
3 changed files with 21 additions and 109 deletions

View File

@ -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

View File

@ -24,58 +24,43 @@ freely, subject to the following restrictions:
#ifndef __PolyVox_ErrorHandling_H__
#define __PolyVox_ErrorHandling_H__
#include <iostream>
#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) \
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__

View File

@ -1,72 +0,0 @@
#include "PolyVoxCore/Impl/ErrorHandling.h"
#include <cstdio>
#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
{
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);
}
}