Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (83 sloc) 1.98 KB
#ifndef com_sleepless_assert_cpp
#define com_sleepless_assert_cpp
/* Copyright 2009 Sleepless Software Inc. All Rights Reserved */
#include "system.cpp"
#ifdef _DEBUG
#ifndef DEBUG
# define DEBUG
#endif
#endif
#ifdef DEBUG ////////////////////////////////////////////////
#if defined(linux) || (defined(__APPLE__) && defined(__GNUC__))
// Under linux, just abort with a message
# define ASSERT(exp) { \
if(!(exp)) \
{ \
char s[128]; \
const char *f = strrchr(__FILE__, '\\'); \
if(f) \
f++; \
else \
f = __FILE__; \
sprintf(s, "\n%s:%d: %s\n", f, __LINE__, #exp); \
System::alert("FAILED ASSERTION", s); \
abort(); \
} \
}
#endif
#ifdef WIN32
/* Under windows, generate a divide by zero exception. If being run
* in the debugger, the code will freeze at the point of failure giving
* me a chance to check the current stack trace to see what when wrong */
# define ASSERT(exp) { \
if(!(exp)) \
{ \
int zero = 0; \
int byebye = 1 / zero; \
} \
}
#endif
#else ////////////////////////////////////////////////
#if 1
# define ASSERT(exp) { \
if(!(exp)) \
{ \
char *s = new char[1000]; \
if(s) \
{ \
sprintf(s, \
"\n" \
"An unexpected condition has occurred that this program\n" \
"is not able to cope with. As such, the program will\n" \
"now exit. Please make a note of the coded information\n" \
"below in case your System Adminstrator should need it.\n" \
"\n" \
"\t%s:%d\n", \
__FILE__, \
__LINE__ \
); \
System::alert("Unexpected Condition", s); \
delete s; \
} \
exit(1); \
} \
}
#else
# define ASSERT(exp)
#endif
#endif ////////////////////////////////////////////////
// XXX get rid of DASSERT in lib code
#define DASSERT ASSERT
#ifdef TEST_ASSERT
int main(int argc, char **argv)
{
int x = 0;
int y = 1;
ASSERT(x == 0);
ASSERT(y == 0);
DASSERT(x == 0);
DASSERT(y == 0);
}
#endif
#define ass ASSERT
#endif // com_sleepless_assert_cpp