diff options
Diffstat (limited to 'src/common/nullpo.h')
-rw-r--r-- | src/common/nullpo.h | 302 |
1 files changed, 102 insertions, 200 deletions
diff --git a/src/common/nullpo.h b/src/common/nullpo.h index 8ee86a782..ae39b1151 100644 --- a/src/common/nullpo.h +++ b/src/common/nullpo.h @@ -1,225 +1,127 @@ // Copyright (c) Athena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder -#ifndef _NULLPO_H_ -#define _NULLPO_H_ - +#ifndef COMMON_NULLPO_H +#define COMMON_NULLPO_H #include "../common/cbasetypes.h" -#define NLP_MARK __FILE__, __LINE__, __func__ - // enabled by default on debug builds #if defined(DEBUG) && !defined(NULLPO_CHECK) #define NULLPO_CHECK #endif -/*---------------------------------------------------------------------------- - * Macros - *---------------------------------------------------------------------------- - */ -/*====================================== - * Nullチェック 及び 情報出力後 return - *・展開するとifとかreturn等が出るので - * 一行単体で使ってください。 - *・nullpo_ret(x = func()); - * のような使用法も想定しています。 - *-------------------------------------- - * nullpo_ret(t) - * 戻り値 0固定 - * [引数] - * t チェック対象 - *-------------------------------------- - * nullpo_retv(t) - * 戻り値 なし - * [引数] - * t チェック対象 - *-------------------------------------- - * nullpo_retr(ret, t) - * 戻り値 指定 - * [引数] - * ret return(ret); - * t チェック対象 - *-------------------------------------- - * nullpo_ret_f(t, fmt, ...) - * 詳細情報出力用 - * 戻り値 0 - * [引数] - * t チェック対象 - * fmt ... vprintfに渡される - * 備考や関係変数の書き出しなどに - *-------------------------------------- - * nullpo_retv_f(t, fmt, ...) - * 詳細情報出力用 - * 戻り値 なし - * [引数] - * t チェック対象 - * fmt ... vprintfに渡される - * 備考や関係変数の書き出しなどに - *-------------------------------------- - * nullpo_retr_f(ret, t, fmt, ...) - * 詳細情報出力用 - * 戻り値 指定 - * [引数] - * ret return(ret); - * t チェック対象 - * fmt ... vprintfに渡される - * 備考や関係変数の書き出しなどに - *-------------------------------------- - */ +// Skip assert checks on release builds +#if !defined(RELEASE) && !defined(ASSERT_CHECK) +#define ASSERT_CHECK +#endif + +/** Assert */ + +#if defined(ASSERT_CHECK) +// extern "C" { +#include <assert.h> +// } +#if !defined(DEFCPP) && defined(WIN32) && !defined(MINGW) +#include <crtdbg.h> +#endif // !DEFCPP && WIN && !MINGW +#define Assert(EX) assert(EX) +#define Assert_chk(EX) ( (EX) ? false : (assert_report(__FILE__, __LINE__, __func__, #EX, "failed assertion"), true) ) +#else // ! ASSERT_CHECK +#define Assert(EX) (EX) +#define Assert_chk(EX) ((EX), false) +#endif // ASSERT_CHECK #if defined(NULLPO_CHECK) +/** + * Reports NULL pointer information if the passed pointer is NULL + * + * @param t pointer to check + * @return true if the passed pointer is NULL, false otherwise + */ +#define nullpo_chk(t) ( (t) != NULL ? false : (assert_report(__FILE__, __LINE__, __func__, #t, "nullpo info"), true) ) +#else // ! NULLPO_CHECK +#define nullpo_chk(t) ((t), false) +#endif // NULLPO_CHECK + +/** + * The following macros check for NULL pointers and return from the current + * function or block in case one is found. + * + * It is guaranteed that the argument is evaluated once and only once, so it + * is safe to call them as: + * nullpo_ret(x = func()); + * The macros can be used safely in any context, as they expand to a do/while + * construct, except nullpo_retb, which expands to an if/else construct. + */ +/** + * Returns 0 if a NULL pointer is found. + * + * @param t pointer to check + */ #define nullpo_ret(t) \ - if (nullpo_chk(NLP_MARK, (void *)(t))) {return(0);} - -#define nullpo_retv(t) \ - if (nullpo_chk(NLP_MARK, (void *)(t))) {return;} - -#define nullpo_retr(ret, t) \ - if (nullpo_chk(NLP_MARK, (void *)(t))) {return(ret);} - -#define nullpo_retb(t) \ - if (nullpo_chk(NLP_MARK, (void *)(t))) {break;} - -// 可変引数マクロに関する条件コンパイル -#if __STDC_VERSION__ >= 199901L -/* C99に対応 */ -#define nullpo_ret_f(t, fmt, ...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(0);} - -#define nullpo_retv_f(t, fmt, ...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return;} - -#define nullpo_retr_f(ret, t, fmt, ...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(ret);} - -#define nullpo_retb_f(t, fmt, ...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {break;} + do { if (nullpo_chk(t)) return(0); } while(0) -#elif __GNUC__ >= 2 -/* GCC用 */ -#define nullpo_ret_f(t, fmt, args...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(0);} - -#define nullpo_retv_f(t, fmt, args...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return;} - -#define nullpo_retr_f(ret, t, fmt, args...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(ret);} - -#define nullpo_retb_f(t, fmt, args...) \ - if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {break;} - -#else - -/* その他の場合・・・ orz */ - -#endif - -#else /* NULLPO_CHECK */ -/* No Nullpo check */ - -// if((t)){;} -// 良い方法が思いつかなかったので・・・苦肉の策です。 -// 一応ワーニングは出ないはず - -#define nullpo_ret(t) (void)(t) -#define nullpo_retv(t) (void)(t) -#define nullpo_retr(ret, t) (void)(t) -#define nullpo_retb(t) (void)(t) - -// 可変引数マクロに関する条件コンパイル -#if __STDC_VERSION__ >= 199901L -/* C99に対応 */ -#define nullpo_ret_f(t, fmt, ...) (void)(t) -#define nullpo_retv_f(t, fmt, ...) (void)(t) -#define nullpo_retr_f(ret, t, fmt, ...) (void)(t) -#define nullpo_retb_f(t, fmt, ...) (void)(t) - -#elif __GNUC__ >= 2 -/* GCC用 */ -#define nullpo_ret_f(t, fmt, args...) (void)(t) -#define nullpo_retv_f(t, fmt, args...) (void)(t) -#define nullpo_retr_f(ret, t, fmt, args...) (void)(t) -#define nullpo_retb_f(t, fmt, args...) (void)(t) - -#else -/* その他の場合・・・ orz */ -#endif +/** + * Returns 0 if the given assertion fails. + * + * @param t statement to check + */ +#define Assert_ret(t) \ + do { if (Assert_chk(t)) return(0); } while(0) -#endif /* NULLPO_CHECK */ +/** + * Returns void if a NULL pointer is found. + * + * @param t pointer to check + */ +#define nullpo_retv(t) \ + do { if (nullpo_chk(t)) return; } while(0) -/*---------------------------------------------------------------------------- - * Functions - *---------------------------------------------------------------------------- +/** + * Returns void if the given assertion fails. + * + * @param t statement to check */ -/*====================================== - * nullpo_chk - * Nullチェック 及び 情報出力 - * [引数] - * file __FILE__ - * line __LINE__ - * func __func__ (関数名) - * これらには NLP_MARK を使うとよい - * target チェック対象 - * [返り値] - * 0 OK - * 1 NULL - *-------------------------------------- +#define Assert_retv(t) \ + do { if (Assert_chk(t)) return; } while(0) + +/** + * Returns the given value if a NULL pointer is found. + * + * @param ret value to return + * @param t pointer to check */ -int nullpo_chk(const char *file, int line, const char *func, const void *target); - - -/*====================================== - * nullpo_chk_f - * Nullチェック 及び 詳細な情報出力 - * [引数] - * file __FILE__ - * line __LINE__ - * func __func__ (関数名) - * これらには NLP_MARK を使うとよい - * target チェック対象 - * fmt ... vprintfに渡される - * 備考や関係変数の書き出しなどに - * [返り値] - * 0 OK - * 1 NULL - *-------------------------------------- +#define nullpo_retr(ret, t) \ + do { if (nullpo_chk(t)) return(ret); } while(0) + +/** + * Returns the given value if the given assertion fails. + * + * @param ret value to return + * @param t statement to check */ -int nullpo_chk_f(const char *file, int line, const char *func, const void *target, - const char *fmt, ...) - __attribute__((format(printf,5,6))); - - -/*====================================== - * nullpo_info - * nullpo情報出力 - * [引数] - * file __FILE__ - * line __LINE__ - * func __func__ (関数名) - * これらには NLP_MARK を使うとよい - *-------------------------------------- +#define Assert_retr(ret, t) \ + do { if (Assert_chk(t)) return(ret); } while(0) + +/** + * Breaks from the current loop/switch if a NULL pointer is found. + * + * @param t pointer to check */ -void nullpo_info(const char *file, int line, const char *func); - - -/*====================================== - * nullpo_info_f - * nullpo詳細情報出力 - * [引数] - * file __FILE__ - * line __LINE__ - * func __func__ (関数名) - * これらには NLP_MARK を使うとよい - * fmt ... vprintfに渡される - * 備考や関係変数の書き出しなどに - *-------------------------------------- +#define nullpo_retb(t) \ + if (nullpo_chk(t)) break; else (void)0 + +/** + * Breaks from the current loop/switch if the given assertion fails. + * + * @param t statement to check */ -void nullpo_info_f(const char *file, int line, const char *func, - const char *fmt, ...) - __attribute__((format(printf,4,5))); +#define Assert_retb(t) \ + if (Assert_chk(t)) break; else (void)0 + +void assert_report(const char *file, int line, const char *func, const char *targetname, const char *title); -#endif /* _NULLPO_H_ */ +#endif /* COMMON_NULLPO_H */ |