[7139] | 1 | #ifndef __DEBUG_H__
|
---|
| 2 | #define __DEBUG_H__
|
---|
| 3 |
|
---|
| 4 | // Abstract: This file provides the following macros:
|
---|
| 5 | //
|
---|
| 6 | // ASSERT(p) - If p is false and DEBUG is true drops into the
|
---|
| 7 | // debugger, if ASSERTS_THROW is true throws a XAssertException exception.
|
---|
| 8 | //
|
---|
| 9 | // VERIFY(p) - Like ASSERT except that p is evaluated in release builds.
|
---|
| 10 | //
|
---|
| 11 | // REQUIRE(p) - Like VERIFY except that it exits the app if the test fails
|
---|
| 12 | // (this is needed because the boot strap code can't safely throw).
|
---|
| 13 | //
|
---|
| 14 | // COMPILE_CHECK(p) - An ASSERT that fires at compile time. This is useful
|
---|
| 15 | // for doing things like verifying the layout of items within a struct.
|
---|
| 16 | // Note that this check is also done in release builds.
|
---|
| 17 | //
|
---|
| 18 | // DEBUGSTR(formatStr, n1, n2, ...) - Drops into the debugger and
|
---|
| 19 | // displays a string.
|
---|
| 20 | //
|
---|
| 21 | // TRACE(formatStr, n1, n2, ...) - Writes a string to a debug window
|
---|
| 22 | // without suspending the program.
|
---|
| 23 | //
|
---|
| 24 | // TRACEFLOW(category, formatStr, n1, n2, ...) - Like TRACE, but with
|
---|
| 25 | // the addition of a category string. The category string allows users
|
---|
| 26 | // to turn off entire categories of messages. Note that DEBUGSTR, TRACE,
|
---|
| 27 | // and TRACEFLOW may be used with boolean expressions by tacking an _IF
|
---|
| 28 | // on the end (eg DEBUGSTR_IF(x < 0, "Sqrt was passed a negative argument.")).
|
---|
| 29 | //
|
---|
| 30 | // PRECONDITION(p) - Used in conjunction with POSTCONDITION to verify
|
---|
| 31 | // the integrity of an object: each public method should include one call to
|
---|
| 32 | // PRECONDITION and another to POSTCONDITION. The idea here is to check to
|
---|
| 33 | // see if the object stays in a valid state given valid arguments. Note that
|
---|
| 34 | // these macros call a method named Invariant. This method should use ASSERT's
|
---|
| 35 | // to check to see if the object is in a sane state. See ZInvariant.h for
|
---|
| 36 | // more details.
|
---|
| 37 | //
|
---|
| 38 | // CHECK_INVARIANT() - Calls the Invariant. This is useful in ctors because
|
---|
| 39 | // PRECONDITION doesn't call the dtor (PRECONDITION constructs an object on
|
---|
| 40 | // the stack which calls the Invariant when the function exits).
|
---|
| 41 |
|
---|
| 42 | #pragma once // Open only once per build
|
---|
| 43 |
|
---|
| 44 | #define RUNTIME_EXPORT
|
---|
| 45 |
|
---|
| 46 | RUNTIME_EXPORT std::wstring ToStr(bool value); // I originally used iostreams, but that caused a ridiculous amount of bloat in object files (but much less so in the exe)
|
---|
| 47 | RUNTIME_EXPORT std::wstring ToStr(char value);
|
---|
| 48 | inline std::wstring ToStr(wchar_t value) {return std::wstring(1, value);}
|
---|
| 49 | RUNTIME_EXPORT std::wstring ToStr(int16_t value, int32_t fieldWidth = 1);
|
---|
| 50 | RUNTIME_EXPORT std::wstring ToStr(uint16_t value, int32_t fieldWidth = 1);
|
---|
| 51 | RUNTIME_EXPORT std::wstring ToStr(int32_t value, int32_t fieldWidth = 1);
|
---|
| 52 | RUNTIME_EXPORT std::wstring ToStr(uint32_t value, int32_t fieldWidth = 1);
|
---|
| 53 | RUNTIME_EXPORT std::wstring ToStr(float value, int32_t precision = 6);
|
---|
| 54 | RUNTIME_EXPORT std::wstring ToStr(double_t value, int32_t precision = 6);
|
---|
| 55 | RUNTIME_EXPORT std::wstring ToStr(const char* value);
|
---|
| 56 | inline std::wstring ToStr(const wchar_t* value) {return value;}
|
---|
| 57 | RUNTIME_EXPORT std::wstring ToStr(const std::string& value);
|
---|
| 58 | inline std::wstring ToStr(const std::wstring& value) {return value;}
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | #ifdef _DEBUG
|
---|
| 62 | #define DEBUG 1
|
---|
| 63 | #else
|
---|
| 64 | #define DEBUG 0
|
---|
| 65 |
|
---|
| 66 | #if !defined(NDEBUG)
|
---|
| 67 | #define NDEBUG // used by <assert.h>
|
---|
| 68 | #endif
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | #ifndef ASSERTS_THROW
|
---|
| 72 | #define ASSERTS_THROW 0
|
---|
| 73 | #endif
|
---|
[7140] | 74 | // #define ASSERTS_THROW 1
|
---|
[7139] | 75 | // Synch with ANSI definitions.
|
---|
| 76 | #if DEBUG && defined(NDEBUG)
|
---|
| 77 | #error DEBUG and NDEBUG are out of sync!
|
---|
| 78 | #endif
|
---|
| 79 |
|
---|
| 80 | #if !DEBUG && !defined(NDEBUG)
|
---|
| 81 | #error DEBUG and NDEBUG are out of sync!
|
---|
| 82 | #endif
|
---|
| 83 |
|
---|
| 84 | // Synch with MSVC.
|
---|
| 85 | #if _MSC_VER && DEBUG != defined(_DEBUG)
|
---|
| 86 | #error DEBUG and _DEBUG are out of sync!
|
---|
| 87 | #endif
|
---|
| 88 |
|
---|
| 89 | #ifndef _lint
|
---|
| 90 | #define COMPILE_CHECK(p) {struct _CC {char a[(p) ? 1 : -1];};} (void) 0
|
---|
| 91 | #else
|
---|
| 92 | #define COMPILE_CHECK(p)
|
---|
| 93 | #endif
|
---|
| 94 |
|
---|
| 95 | #if DEBUG
|
---|
| 96 |
|
---|
| 97 | void AssertFailed(const char* expr, const char* file, int line);
|
---|
| 98 |
|
---|
| 99 | #undef assert
|
---|
| 100 |
|
---|
| 101 | #define ASSERT(p) do {if (!(p)) AssertFailed(#p, __FILE__, __LINE__);} while (false)
|
---|
| 102 |
|
---|
| 103 | #define ASSERT_IF(p, x) do {if ((p) && !(x)) AssertFailed(#x, __FILE__, __LINE__);} while (false)
|
---|
| 104 | #define assert(p) ASSERT(p)
|
---|
| 105 | #define VERIFY(p) ASSERT(p)
|
---|
| 106 | #define REQUIRE(p) ASSERT(p)
|
---|
| 107 |
|
---|
| 108 | #define PRECONDITION(p) ASSERT(p)
|
---|
| 109 | #define POSTCONDITION(p) ASSERT(p)
|
---|
| 110 |
|
---|
| 111 | #ifdef NO_NESTED_QUOTES
|
---|
| 112 | #define PRECONDITION2(a,b) ASSERT(a)
|
---|
| 113 | #define POSTCONDITION2(a,b) ASSERT(a)
|
---|
| 114 | #else
|
---|
| 115 | #define PRECONDITION2(a,b) ASSERT((b, (a) !=0))
|
---|
| 116 | #define POSTCONDITION2(a,b) ASSERT((b, (a) !=0))
|
---|
| 117 | #endif
|
---|
| 118 |
|
---|
| 119 | #else // #if DEBUG
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | #if ASSERTS_THROW
|
---|
| 123 | void AssertFailed(const char*, const char*, int);
|
---|
| 124 |
|
---|
| 125 | #define ASSERT(p) do {if (!(p)) AssertFailed(#p, __FILE__, __LINE__);} while (false)
|
---|
| 126 |
|
---|
| 127 | #define ASSERT_IF(p, x) do {if ((p) && !(x)) AssertFailed(#x, __FILE__, __LINE__);} while (false)
|
---|
| 128 | #define VERIFY(p) ASSERT(p)
|
---|
| 129 |
|
---|
| 130 | #define PRECONDITION(p) ASSERT(p)
|
---|
| 131 | #define POSTCONDITION(p) ASSERT(p)
|
---|
| 132 |
|
---|
| 133 | #define PRECONDITION2(a,b) ASSERT(a)
|
---|
| 134 | #define POSTCONDITION2(a,b) ASSERT(a)
|
---|
| 135 | #else
|
---|
| 136 | #define ASSERT(p) ((void) 0)
|
---|
| 137 |
|
---|
| 138 | #define ASSERT_IF(p, x) ((void) 0)
|
---|
| 139 | #define VERIFY(p) do {if (p) 0;} while (false)
|
---|
| 140 |
|
---|
| 141 | #define PRECONDITION(p) ((void) 0)
|
---|
| 142 | #define POSTCONDITION(p) ((void) 0)
|
---|
| 143 |
|
---|
| 144 | #define PRECONDITION2(a,b)
|
---|
| 145 | #define POSTCONDITION2(a,b)
|
---|
| 146 | #endif
|
---|
| 147 |
|
---|
| 148 | void RequireFailed(const char*, const char*, int);
|
---|
| 149 |
|
---|
| 150 | #define REQUIRE(p) do {if (!(p)) RequireFailed(#p, __FILE__, __LINE__);} while (false)
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | #endif // DEBUG
|
---|
| 154 |
|
---|
| 155 | #ifdef _CONSOLE
|
---|
| 156 | #define TRACE
|
---|
| 157 | #endif
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 |
|
---|
[7140] | 161 | #endif /* __DEBUG_H__ */
|
---|