KallistiOS  ##version##
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
assert.h
Go to the documentation of this file.
1 /* KallistiOS ##version##
2 
3  assert.h
4  Copyright (C)2002,2004 Dan Potter
5 
6 */
7 
8 #ifndef __ASSERT_H
9 #define __ASSERT_H
10 
11 #include <kos/cdefs.h>
12 __BEGIN_DECLS
13 
14 /**
15  \file assert.h
16  \brief Standard C Assertions
17 
18  This file contains the standard C assertions to raise an assertion or to
19  change the assertion handler.
20 
21  \author Dan Potter
22 */
23 
24 /* This is nice and simple, modeled after the BSD one like most of KOS;
25  the addition here is assert_msg(), which allows you to provide an
26  error message. */
27 #define _assert(e) assert(e)
28 
29 /* __FUNCTION__ is not ANSI, it's GCC, but we depend on GCC anyway.. */
30 #ifdef NDEBUG
31 # define assert(e) ((void)0)
32 # define assert_msg(e, m) ((void)0)
33 #else
34 
35 /** \brief Standard C assertion macro.
36 
37  This macro does a standard C assertion, wherein the expression is evaluated,
38  and if false, the program is ultimately aborted using abort(). If the
39  expression evaluates to true, the macro does nothing (other than any side
40  effects of evaluating the expression).
41 
42  \param e A value or expression to be evaluated as true or
43  false.
44 */
45 # define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, NULL, __FUNCTION__))
46 
47 /** \brief assert() with a custom message.
48 
49  This macro acts the same as the assert() macro, but allows you to specify a
50  custom message to be printed out if the assertion fails.
51 
52  \param e A value or expression to be evaluated as true or
53  false.
54  \param m A message (const char *).
55 */
56 # define assert_msg(e, m) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, m, __FUNCTION__))
57 #endif
58 
59 /* \cond */
60 /* Defined in assert.c */
61 void __assert(const char *file, int line, const char *expr,
62  const char *msg, const char *func);
63 /* \endcond */
64 
65 /** \brief Assertion handler type.
66 
67  The user can provide their own assertion handler with this type. If none is
68  provided, a default is used which ultimately prints out the location of the
69  failed assertion and calls abort().
70 
71  \param file The filename where the assertion happened.
72  \param line The line number where the assertion happened.
73  \param expr The expression that raised the assertion.
74  \param msg A custom message for why the assertion happened.
75  \param func The function name from which the assertion happened.
76 
77  \see assert_set_handler
78 */
79 typedef void (*assert_handler_t)(const char * file, int line, const char * expr,
80  const char * msg, const char * func);
81 
82 /** \brief Set an assertion handler to call on a failed assertion.
83 
84  The default assertion handler simply will print a message and call abort().
85 
86  \return The old assertion handler so it may be restored
87  later if appropriate.
88 
89  \see assert_handler_t
90 */
92 
93 __END_DECLS
94 
95 #endif /* __ASSERT_H */
96 
void(* assert_handler_t)(const char *file, int line, const char *expr, const char *msg, const char *func)
Assertion handler type.
Definition: assert.h:79
Potentially useful definitions for C Programs.
assert_handler_t assert_set_handler(assert_handler_t hnd)
Set an assertion handler to call on a failed assertion.