Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 4 | * |
| 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 Prijono | aae2b1c | 2007-06-01 09:58:57 +0000 | [diff] [blame] | 47 | #ifndef pj_assert |
| 48 | # define pj_assert(expr) assert(expr) |
| 49 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | /** |
| 53 | * @hideinitializer |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 54 | * If #PJ_ENABLE_EXTRA_CHECK is declared and the value is non-zero, then |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 55 | * #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 Prijono | cdf3c11 | 2006-05-19 15:54:09 +0000 | [diff] [blame] | 65 | if (!(expr)) { pj_assert(expr); return retval; } \ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 66 | } while (0) |
| 67 | #else |
| 68 | # define PJ_ASSERT_RETURN(expr,retval) pj_assert(expr) |
| 69 | #endif |
| 70 | |
Benny Prijono | 0ca04b6 | 2005-12-30 23:50:15 +0000 | [diff] [blame] | 71 | /** |
| 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 Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 91 | /** @} */ |
| 92 | |
| 93 | #endif /* __PJ_ASSERT_H__ */ |
| 94 | |