KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 assert.h
4 Copyright (C) 2002, 2004 Megan 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 \ingroup assertions
18
19 This file contains the standard C assertions to raise an assertion or to
20 change the assertion handler.
21
22 \author Megan Potter
23*/
24
25/** \defgroup assertions Assertions
26 \brief assert() management and custom handlers
27 \ingroup debugging
28
29 KOS maps the standard C assert() mechanism to a default implementation
30 which logs the failed expression as well as the source code context.
31 A secondary assertion mechanism, assert_msg() is also provided for adding
32 a cusom message. You may also override KOS's assertion handler and replace
33 it with your own via assert_set_handler().
34
35 @{
36*/
37
38/* This is nice and simple, modeled after the BSD one like most of KOS;
39 the addition here is assert_msg(), which allows you to provide an
40 error message. */
41/** \cond */
42#define _assert(e) assert(e)
43/** \endcond */
44
45#ifdef NDEBUG
46# define assert(e) ((void)0)
47# define assert_msg(e, m) ((void)0)
48#else
49
50/* This bit of magic borrowed from Newlib's assert.h... */
51/* \cond */
52#ifndef __ASSERT_FUNC
53#if defined(__cplusplus)
54# define __ASSERT_FUNC __PRETTY_FUNCTION__
55#elif __STDC_VERSION__ >= 199901L
56# define __ASSERT_FUNC __func__
57#elif __GNUC__ >= 2
58# define __ASSERT_FUNC __FUNCTION__
59#else
60# define __ASSERT_FUNC ((char *)0)
61#endif
62#endif
63/* \endcond */
64
65/** \brief Standard C assertion macro.
66
67 This macro does a standard C assertion, wherein the expression is evaluated,
68 and if false, the program is ultimately aborted using abort(). If the
69 expression evaluates to true, the macro does nothing (other than any side
70 effects of evaluating the expression).
71
72 \param e A value or expression to be evaluated as true or
73 false.
74*/
75# define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, NULL, __ASSERT_FUNC))
76
77/** \brief assert() with a custom message.
78
79 This macro acts the same as the assert() macro, but allows you to specify a
80 custom message to be printed out if the assertion fails.
81
82 \param e A value or expression to be evaluated as true or
83 false.
84 \param m A message (const char *).
85*/
86# define assert_msg(e, m) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e, m, __ASSERT_FUNC))
87#endif
88
89/* \cond */
90/* Defined in assert.c */
91void __assert(const char *file, int line, const char *expr,
92 const char *msg, const char *func);
93/* \endcond */
94
95/** \brief Assertion handler type.
96
97 The user can provide their own assertion handler with this type. If none is
98 provided, a default is used which ultimately prints out the location of the
99 failed assertion and calls abort().
100
101 \param file The filename where the assertion happened.
102 \param line The line number where the assertion happened.
103 \param expr The expression that raised the assertion.
104 \param msg A custom message for why the assertion happened.
105 \param func The function name from which the assertion happened.
106
107 \see assert_set_handler
108*/
109typedef void (*assert_handler_t)(const char * file, int line, const char * expr,
110 const char * msg, const char * func);
111
112/** \brief Set an assertion handler to call on a failed assertion.
113
114 The default assertion handler simply will print a message and call abort().
115 NULL is a valid value and will cause nothing to happen on an assert.
116
117 \return The old assertion handler so it may be restored
118 later if appropriate.
119
120 \see assert_handler_t
121*/
123
124/** @} */
125
126__END_DECLS
127
128#endif /* __ASSERT_H */
Definitions for builtin attributes and compiler directives.
void(* assert_handler_t)(const char *file, int line, const char *expr, const char *msg, const char *func)
Assertion handler type.
Definition assert.h:109
assert_handler_t assert_set_handler(assert_handler_t hnd)
Set an assertion handler to call on a failed assertion.