VirtualBox

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

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

kHlpAssert.h: Fixed macros, missing \.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.8 KB
Line 
1/* $Id: kHlpAssert.h 18 2009-01-30 06:26:49Z bird $ */
2/** @file
3 * kHlpAssert - Assertion Macros.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
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 kHlpAssertReturnVoid(expr) \
76 do { \
77 if (!(expr)) \
78 { \
79 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
80 kHlpAssertBreakpoint(); \
81 return; \
82 } \
83 } while (0)
84
85# define kHlpAssertMsg(expr, msg) \
86 do { \
87 if (!(expr)) \
88 { \
89 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
90 kHlpAssertMsg2 msg; \
91 kHlpAssertBreakpoint(); \
92 } \
93 } while (0)
94
95# define kHlpAssertMsgReturn(expr, msg, rcRet) \
96 do { \
97 if (!(expr)) \
98 { \
99 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
100 kHlpAssertMsg2 msg; \
101 kHlpAssertBreakpoint(); \
102 return (rcRet); \
103 } \
104 } while (0)
105
106# define kHlpAssertMsgReturnVoid(expr, msg) \
107 do { \
108 if (!(expr)) \
109 { \
110 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
111 kHlpAssertMsg2 msg; \
112 kHlpAssertBreakpoint(); \
113 return; \
114 } \
115 } while (0)
116
117#else /* !K_STRICT */
118# define kHlpAssert(expr) do { } while (0)
119# define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
120# define kHlpAssertReturnVoid(expr) do { if (!(expr)) return; } while (0)
121# define kHlpAssertMsg(expr, msg) do { } while (0)
122# define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
123# define kHlpAssertMsgReturnVoid(expr, msg) do { if (!(expr)) return; } while (0)
124#endif /* !K_STRICT */
125
126#define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
127#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
128#define kHlpAssertPtrReturnVoid(ptr) kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
129#define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
130#define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
131#define kHlpAssertPtrNullReturnVoid(ptr) kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
132#define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
133#define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
134#define kHlpAssertRCReturnVoid(rc) kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
135#define kHlpAssertFailed() kHlpAssert(0)
136#define kHlpAssertFailedReturn(rcRet) kHlpAssertReturn(0, (rcRet))
137#define kHlpAssertFailedReturnVoid() kHlpAssertReturnVoid(0)
138#define kHlpAssertMsgFailed(msg) kHlpAssertMsg(0, msg)
139#define kHlpAssertMsgFailedReturn(msg, rcRet) kHlpAssertMsgReturn(0, msg, (rcRet))
140#define kHlpAssertMsgFailedReturnVoid(msg) kHlpAssertMsgReturnVoid(0, msg))
141
142/**
143 * Helper function that displays the first part of the assertion message.
144 *
145 * @param pszExpr The expression.
146 * @param pszFile The file name.
147 * @param iLine The line number is the file.
148 * @param pszFunction The function name.
149 * @internal
150 */
151KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
152
153/**
154 * Helper function that displays custom assert message.
155 *
156 * @param pszFormat Format string that get passed to vprintf.
157 * @param ... Format arguments.
158 * @internal
159 */
160KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
161
162
163/** @} */
164
165#ifdef __cplusplus
166}
167#endif
168
169#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette