blob: c8898abced957f387e1a74d3d6fff7d742aeb53c [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_ASSERT_H__
20#define __PJ_ASSERT_H__
21
22/**
23 * @file assert.h
24 * @brief Assertion macro pj_assert().
25 */
26
27#include <pj/config.h>
28#include <pj/compat/assert.h>
29
30/**
31 * @defgroup pj_assert Assertion Macro
32 * @ingroup PJ_MISC
33 * @{
34 *
35 * Assertion and other helper macros for sanity checking.
36 */
37
38/**
39 * @hideinitializer
40 * Check during debug build that an expression is true. If the expression
41 * computes to false during run-time, then the program will stop at the
42 * offending statements.
43 * For release build, this macro will not do anything.
44 *
45 * @param expr The expression to be evaluated.
46 */
Benny Prijonoaae2b1c2007-06-01 09:58:57 +000047#ifndef pj_assert
48# define pj_assert(expr) assert(expr)
49#endif
Benny Prijono5dcb38d2005-11-21 01:55:47 +000050
51
52/**
53 * @hideinitializer
Benny Prijono40f2f642006-01-30 18:40:05 +000054 * If #PJ_ENABLE_EXTRA_CHECK is declared and the value is non-zero, then
Benny Prijono5dcb38d2005-11-21 01:55:47 +000055 * #PJ_ASSERT_RETURN macro will evaluate the expression in @a expr during
56 * run-time. If the expression yields false, assertion will be triggered
57 * and the current function will return with the specified return value.
58 *
59 * If #PJ_ENABLE_EXTRA_CHECK is not declared or is zero, then no run-time
60 * checking will be performed. The macro simply evaluates to pj_assert(expr).
61 */
62#if defined(PJ_ENABLE_EXTRA_CHECK) && PJ_ENABLE_EXTRA_CHECK != 0
63# define PJ_ASSERT_RETURN(expr,retval) \
64 do { \
Benny Prijonocdf3c112006-05-19 15:54:09 +000065 if (!(expr)) { pj_assert(expr); return retval; } \
Benny Prijono5dcb38d2005-11-21 01:55:47 +000066 } while (0)
67#else
68# define PJ_ASSERT_RETURN(expr,retval) pj_assert(expr)
69#endif
70
Benny Prijono0ca04b62005-12-30 23:50:15 +000071/**
72 * @hideinitializer
73 * If #PJ_ENABLE_EXTRA_CHECK is declared and non-zero, then
74 * #PJ_ASSERT_ON_FAIL macro will evaluate the expression in @a expr during
75 * run-time. If the expression yields false, assertion will be triggered
76 * and @a exec_on_fail will be executed.
77 *
78 * If #PJ_ENABLE_EXTRA_CHECK is not declared or is zero, then no run-time
79 * checking will be performed. The macro simply evaluates to pj_assert(expr).
80 */
81#if defined(PJ_ENABLE_EXTRA_CHECK) && PJ_ENABLE_EXTRA_CHECK != 0
82# define PJ_ASSERT_ON_FAIL(expr,exec_on_fail) \
83 do { \
84 pj_assert(expr); \
85 if (!(expr)) exec_on_fail; \
86 } while (0)
87#else
88# define PJ_ASSERT_ON_FAIL(expr,exec_on_fail) pj_assert(expr)
89#endif
90
Benny Prijono5dcb38d2005-11-21 01:55:47 +000091/** @} */
92
93#endif /* __PJ_ASSERT_H__ */
94