| 1 | /* $Id: kHlpAssert.h 29 2009-07-01 20:30:29Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpAssert - Assertion Macros.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * Permission is hereby granted, free of charge, to any person
|
|---|
| 10 | * obtaining a copy of this software and associated documentation
|
|---|
| 11 | * files (the "Software"), to deal in the Software without
|
|---|
| 12 | * restriction, including without limitation the rights to use,
|
|---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|---|
| 14 | * copies of the Software, and to permit persons to whom the
|
|---|
| 15 | * Software is furnished to do so, subject to the following
|
|---|
| 16 | * conditions:
|
|---|
| 17 | *
|
|---|
| 18 | * The above copyright notice and this permission notice shall be
|
|---|
| 19 | * included in all copies or substantial portions of the Software.
|
|---|
| 20 | *
|
|---|
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | #ifndef ___kHlpAssert_h___
|
|---|
| 32 | #define ___kHlpAssert_h___
|
|---|
| 33 |
|
|---|
| 34 | #include <k/kHlpDefs.h>
|
|---|
| 35 |
|
|---|
| 36 | #ifdef __cplusplus
|
|---|
| 37 | extern "C" {
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | /** @defgroup grp_kHlpAssert - Assertion Macros
|
|---|
| 41 | * @addtogroup grp_kHlp
|
|---|
| 42 | * @{ */
|
|---|
| 43 |
|
|---|
| 44 | /** @def K_STRICT
|
|---|
| 45 | * Assertions are enabled when K_STRICT is \#defined. */
|
|---|
| 46 |
|
|---|
| 47 | /** @def kHlpAssertBreakpoint
|
|---|
| 48 | * Emits a breakpoint instruction or somehow triggers a debugger breakpoint.
|
|---|
| 49 | */
|
|---|
| 50 | #ifdef _MSC_VER
|
|---|
| 51 | # define kHlpAssertBreakpoint() do { __debugbreak(); } while (0)
|
|---|
| 52 | #elif defined(__GNUC__)
|
|---|
| 53 | # define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
|
|---|
| 54 | #else
|
|---|
| 55 | # error "Port Me"
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | #ifdef K_STRICT
|
|---|
| 59 |
|
|---|
| 60 | # define kHlpAssert(expr) \
|
|---|
| 61 | do { \
|
|---|
| 62 | if (!(expr)) \
|
|---|
| 63 | { \
|
|---|
| 64 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 65 | kHlpAssertBreakpoint(); \
|
|---|
| 66 | } \
|
|---|
| 67 | } while (0)
|
|---|
| 68 |
|
|---|
| 69 | # define kHlpAssertReturn(expr, rcRet) \
|
|---|
| 70 | do { \
|
|---|
| 71 | if (!(expr)) \
|
|---|
| 72 | { \
|
|---|
| 73 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 74 | kHlpAssertBreakpoint(); \
|
|---|
| 75 | return (rcRet); \
|
|---|
| 76 | } \
|
|---|
| 77 | } while (0)
|
|---|
| 78 |
|
|---|
| 79 | # define kHlpAssertReturnVoid(expr) \
|
|---|
| 80 | do { \
|
|---|
| 81 | if (!(expr)) \
|
|---|
| 82 | { \
|
|---|
| 83 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 84 | kHlpAssertBreakpoint(); \
|
|---|
| 85 | return; \
|
|---|
| 86 | } \
|
|---|
| 87 | } while (0)
|
|---|
| 88 |
|
|---|
| 89 | # define kHlpAssertMsg(expr, msg) \
|
|---|
| 90 | do { \
|
|---|
| 91 | if (!(expr)) \
|
|---|
| 92 | { \
|
|---|
| 93 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 94 | kHlpAssertMsg2 msg; \
|
|---|
| 95 | kHlpAssertBreakpoint(); \
|
|---|
| 96 | } \
|
|---|
| 97 | } while (0)
|
|---|
| 98 |
|
|---|
| 99 | # define kHlpAssertMsgReturn(expr, msg, rcRet) \
|
|---|
| 100 | do { \
|
|---|
| 101 | if (!(expr)) \
|
|---|
| 102 | { \
|
|---|
| 103 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 104 | kHlpAssertMsg2 msg; \
|
|---|
| 105 | kHlpAssertBreakpoint(); \
|
|---|
| 106 | return (rcRet); \
|
|---|
| 107 | } \
|
|---|
| 108 | } while (0)
|
|---|
| 109 |
|
|---|
| 110 | # define kHlpAssertMsgReturnVoid(expr, msg) \
|
|---|
| 111 | do { \
|
|---|
| 112 | if (!(expr)) \
|
|---|
| 113 | { \
|
|---|
| 114 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 115 | kHlpAssertMsg2 msg; \
|
|---|
| 116 | kHlpAssertBreakpoint(); \
|
|---|
| 117 | return; \
|
|---|
| 118 | } \
|
|---|
| 119 | } while (0)
|
|---|
| 120 |
|
|---|
| 121 | #else /* !K_STRICT */
|
|---|
| 122 | # define kHlpAssert(expr) do { } while (0)
|
|---|
| 123 | # define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
|
|---|
| 124 | # define kHlpAssertReturnVoid(expr) do { if (!(expr)) return; } while (0)
|
|---|
| 125 | # define kHlpAssertMsg(expr, msg) do { } while (0)
|
|---|
| 126 | # define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
|
|---|
| 127 | # define kHlpAssertMsgReturnVoid(expr, msg) do { if (!(expr)) return; } while (0)
|
|---|
| 128 | #endif /* !K_STRICT */
|
|---|
| 129 |
|
|---|
| 130 | #define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
|
|---|
| 131 | #define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
|---|
| 132 | #define kHlpAssertPtrReturnVoid(ptr) kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
|
|---|
| 133 | #define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
|
|---|
| 134 | #define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
|---|
| 135 | #define kHlpAssertPtrNullReturnVoid(ptr) kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
|
|---|
| 136 | #define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
|
|---|
| 137 | #define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
|
|---|
| 138 | #define kHlpAssertRCReturnVoid(rc) kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
|
|---|
| 139 | #define kHlpAssertFailed() kHlpAssert(0)
|
|---|
| 140 | #define kHlpAssertFailedReturn(rcRet) kHlpAssertReturn(0, (rcRet))
|
|---|
| 141 | #define kHlpAssertFailedReturnVoid() kHlpAssertReturnVoid(0)
|
|---|
| 142 | #define kHlpAssertMsgFailed(msg) kHlpAssertMsg(0, msg)
|
|---|
| 143 | #define kHlpAssertMsgFailedReturn(msg, rcRet) kHlpAssertMsgReturn(0, msg, (rcRet))
|
|---|
| 144 | #define kHlpAssertMsgFailedReturnVoid(msg) kHlpAssertMsgReturnVoid(0, msg))
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Helper function that displays the first part of the assertion message.
|
|---|
| 148 | *
|
|---|
| 149 | * @param pszExpr The expression.
|
|---|
| 150 | * @param pszFile The file name.
|
|---|
| 151 | * @param iLine The line number is the file.
|
|---|
| 152 | * @param pszFunction The function name.
|
|---|
| 153 | * @internal
|
|---|
| 154 | */
|
|---|
| 155 | KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * Helper function that displays custom assert message.
|
|---|
| 159 | *
|
|---|
| 160 | * @param pszFormat Format string that get passed to vprintf.
|
|---|
| 161 | * @param ... Format arguments.
|
|---|
| 162 | * @internal
|
|---|
| 163 | */
|
|---|
| 164 | KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | /** @} */
|
|---|
| 168 |
|
|---|
| 169 | #ifdef __cplusplus
|
|---|
| 170 | }
|
|---|
| 171 | #endif
|
|---|
| 172 |
|
|---|
| 173 | #endif
|
|---|