check_macro.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef BOOST_CONTRACT_CHECK_MACRO_HPP_
  2. #define BOOST_CONTRACT_CHECK_MACRO_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. /** @file
  8. Macros for implementation checks.
  9. */
  10. // IMPORTANT: Included by contract_macro.hpp so must #if-guard all its includes.
  11. #include <boost/contract/core/config.hpp>
  12. #include <boost/contract/detail/noop.hpp>
  13. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  14. /**
  15. Preferred way to assert implementation check conditions.
  16. It is preferred to use this macro instead of programming implementation
  17. checks in a nullary functor passed to @RefClass{boost::contract::check}
  18. constructor because this macro will completely remove implementation checks
  19. from the code when @RefMacro{BOOST_CONTRACT_NO_CHECKS} is defined:
  20. @code
  21. void f() {
  22. ...
  23. BOOST_CONTRACT_CHECK(cond);
  24. ...
  25. }
  26. @endcode
  27. @RefMacro{BOOST_CONTRACT_CHECK}, @RefMacro{BOOST_CONTRACT_CHECK_AUDIT}, and
  28. @RefMacro{BOOST_CONTRACT_CHECK_AXIOM} are the three assertion levels
  29. predefined by this library for implementation checks.
  30. @see @RefSect{advanced.implementation_checks, Implementation Checks}
  31. @param cond Boolean condition to check within implementation code (function
  32. body, etc.).
  33. (This is not a variadic macro parameter so any comma it might
  34. contain must be protected by round parenthesis and
  35. @c BOOST_CONTRACT_CHECK((cond)) will always work.)
  36. */
  37. #define BOOST_CONTRACT_CHECK(cond)
  38. #elif !defined(BOOST_CONTRACT_NO_CHECKS)
  39. #include <boost/contract/detail/check.hpp>
  40. #include <boost/contract/detail/assert.hpp>
  41. #define BOOST_CONTRACT_CHECK(cond) \
  42. BOOST_CONTRACT_DETAIL_CHECK(BOOST_CONTRACT_DETAIL_ASSERT(cond))
  43. #else
  44. #define BOOST_CONTRACT_CHECK(cond) /* nothing */
  45. #endif
  46. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  47. /**
  48. Preferred way to assert implementation check conditions that are
  49. computationally expensive, at least compared to the computational cost of
  50. executing the function body.
  51. The specified condition will always be compiled and validated syntactically,
  52. but it will not be checked at run-time unless
  53. @RefMacro{BOOST_CONTRACT_AUDITS} is defined (undefined by default).
  54. This macro is defined by code equivalent to:
  55. @code
  56. #ifdef BOOST_CONTRACT_AUDITS
  57. #define BOOST_CONTRACT_CHECK_AUDIT(cond) \
  58. BOOST_CONTRACT_CHECK(cond)
  59. #else
  60. #define BOOST_CONTRACT_CHECK_AUDIT(cond) \
  61. BOOST_CONTRACT_CHECK(true || cond)
  62. #endif
  63. @endcode
  64. @RefMacro{BOOST_CONTRACT_CHECK}, @RefMacro{BOOST_CONTRACT_CHECK_AUDIT}, and
  65. @RefMacro{BOOST_CONTRACT_CHECK_AXIOM} are the three assertion levels
  66. predefined by this library for implementation checks.
  67. If there is a need, programmers are free to implement their own assertion
  68. levels defining macros similar to the one above.
  69. @see @RefSect{extras.assertion_levels, Assertion Levels}
  70. @param cond Boolean condition to check within implementation code (function
  71. body, etc.).
  72. (This is not a variadic macro parameter so any comma it might
  73. contain must be protected by round parenthesis and
  74. @c BOOST_CONTRACT_CHECK_AUDIT((cond)) will always work.)
  75. */
  76. #define BOOST_CONTRACT_CHECK_AUDIT(cond)
  77. #elif defined(BOOST_CONTRACT_AUDITS)
  78. #define BOOST_CONTRACT_CHECK_AUDIT(cond) \
  79. BOOST_CONTRACT_CHECK(cond)
  80. #else
  81. #define BOOST_CONTRACT_CHECK_AUDIT(cond) \
  82. BOOST_CONTRACT_DETAIL_NOEVAL(cond)
  83. #endif
  84. /**
  85. Preferred way to document in the code implementation check conditions that are
  86. computationally prohibitive, at least compared to the computational cost of
  87. executing the function body.
  88. The specified condition will always be compiled and validated syntactically, but
  89. it will never be checked at run-time.
  90. This macro is defined by code equivalent to:
  91. @code
  92. #define BOOST_CONTRACT_CHECK_AXIOM(cond) \
  93. BOOST_CONTRACT_CHECK(true || cond)
  94. @endcode
  95. @RefMacro{BOOST_CONTRACT_CHECK}, @RefMacro{BOOST_CONTRACT_CHECK_AUDIT}, and
  96. @RefMacro{BOOST_CONTRACT_CHECK_AXIOM} are the three assertion levels predefined
  97. by this library for implementation checks.
  98. If there is a need, programmers are free to implement their own assertion levels
  99. defining macros similar to the one above.
  100. @see @RefSect{extras.assertion_levels, Assertion Levels}
  101. @param cond Boolean condition to check within implementation code (function
  102. body, etc.).
  103. (This is not a variadic macro parameter so any comma it might
  104. contain must be protected by round parenthesis and
  105. @c BOOST_CONTRACT_CHECK_AXIOM((cond)) will always work.)
  106. */
  107. #define BOOST_CONTRACT_CHECK_AXIOM(cond) \
  108. BOOST_CONTRACT_DETAIL_NOEVAL(cond)
  109. #endif // #include guard