nullpo.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef _NULLPO_HPP_
  4. #define _NULLPO_HPP_
  5. #include "cbasetypes.hpp"
  6. #define NLP_MARK __FILE__, __LINE__, __func__
  7. // enabled by default on debug builds
  8. #if defined(DEBUG) && !defined(NULLPO_CHECK)
  9. #define NULLPO_CHECK
  10. #endif
  11. #if defined(NULLPO_CHECK)
  12. /**
  13. * Macros used to check for NULL pointer and output that information.
  14. */
  15. /**
  16. * Return 0 if pointer is not found.
  17. * @param t: Pointer to check
  18. * @return 0 if t is NULL
  19. */
  20. #define nullpo_ret(t) \
  21. if (nullpo_chk(NLP_MARK, (void *)(t))) {return(0);}
  22. /**
  23. * Return void if pointer is not found.
  24. * @param t: Pointer to check
  25. * @return void if t is NULL
  26. */
  27. #define nullpo_retv(t) \
  28. if (nullpo_chk(NLP_MARK, (void *)(t))) {return;}
  29. /**
  30. * Return the given value if pointer is not found.
  31. * @param ret: Value to return
  32. * @param t: Pointer to check
  33. * @return ret value
  34. */
  35. #define nullpo_retr(ret, t) \
  36. if (nullpo_chk(NLP_MARK, (void *)(t))) {return(ret);}
  37. /**
  38. * Break out of the loop/switch if pointer is not found.
  39. * @param t: Pointer to check
  40. */
  41. #define nullpo_retb(t) \
  42. if (nullpo_chk(NLP_MARK, (void *)(t))) {break;}
  43. // Different C compilers uses different argument formats
  44. #if __STDC_VERSION__ >= 199901L || defined(_MSC_VER)
  45. /* C99 standard */
  46. /**
  47. * Return 0 and display additional information if pointer is not found.
  48. * @param t: Pointer to check
  49. * @param fmt: Pass to vprintf, Format and arguments such as description
  50. * @return 0 if t is NULL
  51. */
  52. #define nullpo_ret_f(t, fmt, ...) \
  53. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(0);}
  54. /**
  55. * Return void and display additional information if pointer is not found.
  56. * @param t: Pointer to check
  57. * @param fmt: Pass to vprintf, Format and arguments such as description
  58. * @return void if t is NULL
  59. */
  60. #define nullpo_retv_f(t, fmt, ...) \
  61. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return;}
  62. /**
  63. * Return the given value and display additional information if pointer is not found.
  64. * @param t: Pointer to check
  65. * @param fmt: Pass to vprintf, Format and arguments such as description
  66. * @return ret value
  67. */
  68. #define nullpo_retr_f(ret, t, fmt, ...) \
  69. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(ret);}
  70. /**
  71. * Break out of the loop/switch and display additional information if pointer is not found.
  72. * @param t: Pointer to check
  73. * @param fmt: Pass to vprintf, Format and arguments such as description
  74. */
  75. #define nullpo_retb_f(t, fmt, ...) \
  76. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {break;}
  77. #elif __GNUC__ >= 2
  78. /* For GCC */
  79. /**
  80. * Return 0 and display additional information if pointer is not found.
  81. * @param t: Pointer to check
  82. * @param fmt: Pass to vprintf, Format and arguments such as description
  83. * @return 0 if t is NULL
  84. */
  85. #define nullpo_ret_f(t, fmt, args...) \
  86. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(0);}
  87. /**
  88. * Return void and display additional information if pointer is not found.
  89. * @param t: Pointer to check
  90. * @param fmt: Pass to vprintf, Format and arguments such as description
  91. * @return void if t is NULL
  92. */
  93. #define nullpo_retv_f(t, fmt, args...) \
  94. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return;}
  95. /**
  96. * Return the given value and display additional information if pointer is not found.
  97. * @param t: Pointer to check
  98. * @param fmt: Pass to vprintf, Format and arguments such as description
  99. * @return ret value
  100. */
  101. #define nullpo_retr_f(ret, t, fmt, args...) \
  102. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(ret);}
  103. /**
  104. * Break out of the loop/switch and display additional information if pointer is not found.
  105. * @param t: Pointer to check
  106. * @param fmt: Pass to vprintf, Format and arguments such as description
  107. */
  108. #define nullpo_retb_f(t, fmt, args...) \
  109. if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {break;}
  110. #else
  111. /* Otherwise... */
  112. #endif
  113. #else /* NULLPO_CHECK */
  114. /* No Nullpo check */
  115. // if((t)){;}
  116. // Do nothing if Nullpo check is disabled
  117. #define nullpo_ret(t) (void)(t)
  118. #define nullpo_retv(t) (void)(t)
  119. #define nullpo_retr(ret, t) (void)(t)
  120. #define nullpo_retb(t) (void)(t)
  121. // Different C compilers uses different argument formats
  122. #if __STDC_VERSION__ >= 199901L || defined(_MSC_VER)
  123. /* C99 standard */
  124. #define nullpo_ret_f(t, fmt, ...) (void)(t)
  125. #define nullpo_retv_f(t, fmt, ...) (void)(t)
  126. #define nullpo_retr_f(ret, t, fmt, ...) (void)(t)
  127. #define nullpo_retb_f(t, fmt, ...) (void)(t)
  128. #elif __GNUC__ >= 2
  129. /* For GCC */
  130. #define nullpo_ret_f(t, fmt, args...) (void)(t)
  131. #define nullpo_retv_f(t, fmt, args...) (void)(t)
  132. #define nullpo_retr_f(ret, t, fmt, args...) (void)(t)
  133. #define nullpo_retb_f(t, fmt, args...) (void)(t)
  134. #else
  135. /* Otherwise... orz */
  136. #endif
  137. #endif /* NULLPO_CHECK */
  138. /**
  139. * Check for NULL pointer and output information.
  140. * @param file: __FILE__
  141. * @param line: __LINE__
  142. * @param func: __func__ (name of the function) [NLP_MARK]
  143. * @param target: Target to check
  144. * @return 0 on success or 1 on NULL
  145. */
  146. int nullpo_chk(const char *file, int line, const char *func, const void *target);
  147. /**
  148. * Check for NULL pointer and output detailed information.
  149. * @param file: __FILE__
  150. * @param line: __LINE__
  151. * @param func: __func__ (name of the function) [NLP_MARK]
  152. * @param target: Target to check
  153. * @param fmt: Passed to vprintf
  154. * @return 0 on success or 1 on NULL
  155. */
  156. int nullpo_chk_f(const char *file, int line, const char *func, const void *target,
  157. const char *fmt, ...)
  158. __attribute__((format(printf,5,6)));
  159. /**
  160. * Display information of the code that cause this function to trigger.
  161. * @param file: __FILE__
  162. * @param line: __LINE__
  163. * @param func: __func__ (name of the function) [NLP_MARK]
  164. * @param target: Target to check
  165. */
  166. void nullpo_info(const char *file, int line, const char *func);
  167. /**
  168. * Check for NULL pointer and output detailed information.
  169. * @param file: __FILE__
  170. * @param line: __LINE__
  171. * @param func: __func__ (name of the function) [NLP_MARK]
  172. * @param target: Target to check
  173. * @param fmt: Passed to vprintf
  174. */
  175. void nullpo_info_f(const char *file, int line, const char *func,
  176. const char *fmt, ...)
  177. __attribute__((format(printf,4,5)));
  178. #endif /* _NULLPO_HPP_ */