assert.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  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. * @file assert.h
  23. * @brief Assertion macro pj_assert().
  24. */
  25. #include <pj/config.h>
  26. #include <pj/compat/assert.h>
  27. /**
  28. * @defgroup pj_assert Assertion Macro
  29. * @ingroup PJ_MISC
  30. * @{
  31. *
  32. * Assertion and other helper macros for sanity checking.
  33. */
  34. /**
  35. * @hideinitializer
  36. * Check during debug build that an expression is true. If the expression
  37. * computes to false during run-time, then the program will stop at the
  38. * offending statements.
  39. * For release build, this macro will not do anything.
  40. *
  41. * @param expr The expression to be evaluated.
  42. */
  43. #ifndef pj_assert
  44. # define pj_assert(expr) assert(expr)
  45. #endif
  46. /**
  47. * @hideinitializer
  48. * If the expression yields false, assertion will be triggered
  49. * and the current function will return with the specified return value.
  50. */
  51. // #if defined(PJ_ENABLE_EXTRA_CHECK) && PJ_ENABLE_EXTRA_CHECK != 0
  52. #define PJ_ASSERT_RETURN(expr,retval) \
  53. do { \
  54. if (!(expr)) { pj_assert(expr); return retval; } \
  55. } while (0)
  56. //#else
  57. //# define PJ_ASSERT_RETURN(expr,retval) pj_assert(expr)
  58. //#endif
  59. /**
  60. * @hideinitializer
  61. * If the expression yields false, assertion will be triggered
  62. * and @a exec_on_fail will be executed.
  63. */
  64. //#if defined(PJ_ENABLE_EXTRA_CHECK) && PJ_ENABLE_EXTRA_CHECK != 0
  65. #define PJ_ASSERT_ON_FAIL(expr,exec_on_fail) \
  66. { \
  67. pj_assert(expr); \
  68. if (!(expr)) exec_on_fail; \
  69. }
  70. //#else
  71. //# define PJ_ASSERT_ON_FAIL(expr,exec_on_fail) pj_assert(expr)
  72. //#endif
  73. /** @} */
  74. #endif /* __PJ_ASSERT_H__ */