You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
6.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/assert.h
  3. *
  4. * Purpose: Pantheios Assertion API.
  5. *
  6. * Created: 8th May 2009
  7. * Updated: 6th November 2010
  8. *
  9. * Thanks to: markitus82 for requesting this functionality
  10. *
  11. * Home: http://www.pantheios.org/
  12. *
  13. * Copyright (c) 2009-2010, Matthew Wilson and Synesis Software
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are
  18. * met:
  19. *
  20. * - Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. * - Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
  26. * names of any contributors may be used to endorse or promote products
  27. * derived from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  31. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * ////////////////////////////////////////////////////////////////////// */
  42. /** \file pantheios/assert.h
  43. *
  44. * [C, C++] Include file for the \ref group__assertion
  45. */
  46. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_ASSERT
  47. #define PANTHEIOS_INCL_PANTHEIOS_H_ASSERT
  48. /* /////////////////////////////////////////////////////////////////////////
  49. * Version information
  50. */
  51. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  52. # define PANTHEIOS_VER_PANTHEIOS_H_ASSERT_MAJOR 1
  53. # define PANTHEIOS_VER_PANTHEIOS_H_ASSERT_MINOR 1
  54. # define PANTHEIOS_VER_PANTHEIOS_H_ASSERT_REVISION 2
  55. # define PANTHEIOS_VER_PANTHEIOS_H_ASSERT_EDIT 8
  56. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  57. /* /////////////////////////////////////////////////////////////////////////
  58. * Includes
  59. */
  60. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  61. # include <pantheios/pantheios.h>
  62. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  63. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_FILELINE
  64. # include <pantheios/fileline.h>
  65. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_FILELINE */
  66. #include <stlsoft/stlsoft.h>
  67. /* /////////////////////////////////////////////////////////////////////////
  68. * Assertion features
  69. */
  70. /** \defgroup group__assertion Pantheios Assertion API
  71. *
  72. * Pantheios Assertion API
  73. *
  74. * This API allows you to combine traditional assert()-based programming
  75. * with diagnostic logging, using the PANTHEIOS_ASSERT() macro.
  76. *
  77. <pre>
  78. int main()
  79. {
  80. PANTHEIOS_ASSERT(false); // Will assert, and will log
  81. return 0;
  82. }
  83. </pre>
  84. */
  85. /** \def PANTHEIOS_ASSERT_SEVERITY_LEVEL
  86. *
  87. * The level at which log statements will be submitted by
  88. * \c PANTHEIOS_ASSERT and \c PANTHEIOS_MESSAGE_ASSERT.
  89. *
  90. * Defaults to \c PANTHEIOS_SEV_EMERGENCY
  91. *
  92. * \ingroup group__assertion
  93. */
  94. #ifndef PANTHEIOS_ASSERT_SEVERITY_LEVEL
  95. # define PANTHEIOS_ASSERT_SEVERITY_LEVEL PANTHEIOS_SEV_EMERGENCY
  96. #endif /* !PANTHEIOS_ASSERT_SEVERITY_LEVEL */
  97. /** \def PANTHEIOS_ASSERT(expr)
  98. *
  99. * Defines an assertion construct for runtime verification.
  100. *
  101. * \param expr Must be non-zero, or an assertion will be fired
  102. *
  103. * If the expression evaluates to false a log statement will be
  104. * submitted at the PANTHEIOS_ASSERT_SEVERITY_LEVEL severity level, and
  105. * emitted if that level is currently switched on. The expression will also
  106. * be subjected to an assert, via \c STLSOFT_ASSERT()
  107. *
  108. * \ingroup group__assertion
  109. */
  110. #define PANTHEIOS_ASSERT(expr) \
  111. \
  112. do \
  113. { \
  114. if(!(expr)) \
  115. { \
  116. PANTHEIOS_NS_QUAL(pantheios_logassertfail)(PANTHEIOS_ASSERT_SEVERITY_LEVEL, PANTHEIOS_FILELINE_A, "assertion failed: " #expr); \
  117. } \
  118. \
  119. STLSOFT_ASSERT(expr); \
  120. } while(0)
  121. /** \def PANTHEIOS_MESSAGE_ASSERT(expr, msg)
  122. *
  123. * Defines an assertion construct for runtime verification.
  124. *
  125. * \param expr Must be non-zero, or an assertion will be fired
  126. * \param msg The message that will be associated with the output if the assertion fires
  127. *
  128. * If the expression evaluates to false a log statement will be
  129. * submitted at the PANTHEIOS_ASSERT_SEVERITY_LEVEL severity level, and
  130. * emitted if that level is currently switched on. The expression will also
  131. * be subjected to an assert, via \c STLSOFT_MESSAGE_ASSERT()
  132. *
  133. * \ingroup group__assertion
  134. */
  135. #define PANTHEIOS_MESSAGE_ASSERT(expr, msg) \
  136. \
  137. do \
  138. { \
  139. if(!(expr)) \
  140. { \
  141. PANTHEIOS_NS_QUAL(pantheios_logassertfail)(PANTHEIOS_ASSERT_SEVERITY_LEVEL, PANTHEIOS_FILELINE_A, "assertion failed: " #expr "; message: " msg); \
  142. } \
  143. \
  144. STLSOFT_MESSAGE_ASSERT(msg, expr); \
  145. } while(0)
  146. /* /////////////////////////////////////////////////////////////////////////
  147. * Inclusion
  148. */
  149. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  150. # pragma once
  151. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  152. /* ////////////////////////////////////////////////////////////////////// */
  153. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_ASSERT */
  154. /* ///////////////////////////// end of file //////////////////////////// */