source: trunk/kStuff/include/k/kHlpAssert.h@ 3543

Last change on this file since 3543 was 3543, checked in by bird, 18 years ago

kRdr hacking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/* $Id: kHlpAssert.h 3543 2007-08-25 06:10:29Z bird $ */
2/** @file
3 * kHlpAssert - Assertion Macros.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kStuff is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with kStuff; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#ifndef ___kHlpAssert_h___
28#define ___kHlpAssert_h___
29
30#include <k/kHlpDefs.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/** @defgroup grp_kHlpAssert - Assertion Macros
37 * @addtogroup grp_kHlp
38 * @{ */
39
40/** @def K_STRICT
41 * Assertions are enabled when K_STRICT is \#defined. */
42
43/** @def kHlpAssertBreakpoint
44 * Emits a breakpoint instruction or somehow triggers a debugger breakpoint.
45 */
46#ifdef _MSC_VER
47# define kHlpAssertBreakpoint() do { __debugbreak(); } while (0)
48#elif defined(__GNUC__)
49# define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
50#else
51# error "Port Me"
52#endif
53
54#ifdef K_STRICT
55
56# define kHlpAssert(expr) \
57 do { \
58 if (!(expr)) \
59 { \
60 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
61 kHlpAssertBreakpoint(); \
62 }
63 } while (0)
64
65# define kHlpAssertReturn(expr, rcRet) \
66 do { \
67 if (!(expr)) \
68 { \
69 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
70 kHlpAssertBreakpoint(); \
71 return (rcRet); \
72 }
73 } while (0)
74
75# define kHlpAssertMsg(expr, msg) \
76 do { \
77 if (!(expr)) \
78 { \
79 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
80 kHlpAssertMsg2 msg; \
81 kHlpAssertBreakpoint(); \
82 }
83 } while (0)
84
85# define kHlpAssertMsgReturn(expr, msg, rcRet) \
86 do { \
87 if (!(expr)) \
88 { \
89 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
90 kHlpAssertMsg2 msg; \
91 kHlpAssertBreakpoint(); \
92 return (rcRet); \
93 }
94 } while (0)
95
96#else /* !K_STRICT */
97# define kHlpAssert(expr) do { } while (0)
98# define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
99# define kHlpAssertMsg(expr, msg) do { } while (0)
100# define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
101#endif /* !K_STRICT */
102
103#define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
104#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
105#define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
106#define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
107#define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
108#define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
109#define kHlpAssertFailed() kHlpAssert(0)
110#define kHlpAssertFailedReturn(rcRet) kHlpAssertReturn(0, (rcRet))
111#define kHlpAssertMsgFailed(msg) kHlpAssertMsg(0, msg)
112#define kHlpAssertMsgFailedReturn(msg, rcRet) kHlpAssertMsgReturn(0, msg, (rcRet))
113
114/**
115 * Helper function that displays the first part of the assertion message.
116 *
117 * @param pszExpr The expression.
118 * @param pszFile The file name.
119 * @param iLine The line number is the file.
120 * @param pszFunction The function name.
121 * @internal
122 */
123KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
124
125/**
126 * Helper function that displays custom assert message.
127 *
128 * @param pszFormat Format string that get passed to vprintf.
129 * @param ... Format arguments.
130 * @internal
131 */
132KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
133
134
135/** @} */
136
137#ifdef __cplusplus
138}
139#endif
140
141#endif
Note: See TracBrowser for help on using the repository browser.