|
|
|||
File indexing completed on 2025-12-16 09:45:02
0001 0002 #ifndef BOOST_CONTRACT_ASSERT_HPP_ 0003 #define BOOST_CONTRACT_ASSERT_HPP_ 0004 0005 // Copyright (C) 2008-2018 Lorenzo Caminiti 0006 // Distributed under the Boost Software License, Version 1.0 (see accompanying 0007 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 0008 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 0009 0010 /** @file 0011 Assert contract conditions. 0012 */ 0013 0014 #include <boost/contract/core/config.hpp> 0015 #include <boost/contract/detail/noop.hpp> 0016 0017 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN 0018 /** 0019 Preferred way to assert contract conditions. 0020 0021 Any exception thrown from within a contract (preconditions, postconditions, 0022 exception guarantees, old value copies at body, class invariants, etc.) is 0023 interpreted by this library as a contract failure. 0024 Therefore, users can program contract assertions manually throwing an 0025 exception when an asserted condition is checked to be @c false (this 0026 library will then call the appropriate contract failure handler 0027 @RefFunc{boost::contract::precondition_failure}, etc.). 0028 However, it is preferred to use this macro because it expands to 0029 code that throws @RefClass{boost::contract::assertion_failure} with the 0030 correct assertion file name (using <c>__FILE__</c>), line number (using 0031 <c>__LINE__</c>), and asserted condition code so to produce informative 0032 error messages (C++11 <c>__func__</c> is not used here because in most cases 0033 it will simply expand to the internal compiler name of the lambda function 0034 used to program the contract conditions adding no specificity to the error 0035 message). 0036 0037 @RefMacro{BOOST_CONTRACT_ASSERT}, @RefMacro{BOOST_CONTRACT_ASSERT_AUDIT}, 0038 and @RefMacro{BOOST_CONTRACT_ASSERT_AXIOM} are the three assertion levels 0039 predefined by this library. 0040 0041 @see @RefSect{tutorial.preconditions, Preconditions}, 0042 @RefSect{tutorial.postconditions, Postconditions}, 0043 @RefSect{tutorial.exception_guarantees, Exceptions Guarantees}, 0044 @RefSect{tutorial.class_invariants, Class Invariants}, 0045 @RefSect{extras.no_macros__and_no_variadic_macros_, No Macros} 0046 0047 @param cond Boolean contract condition to check. 0048 (This is not a variadic macro parameter so any comma it might 0049 contain must be protected by round parenthesis and 0050 @c BOOST_CONTRACT_ASSERT((cond)) will always work.) 0051 */ 0052 // This must be an expression (a trivial one so the compiler can optimize it 0053 // away). It cannot an empty code block `{}`, etc. otherwise code like 0054 // `if(...) ASSERT(...); else ASSERT(...);` won't work when NO_ALL. 0055 #define BOOST_CONTRACT_ASSERT(cond) 0056 #elif !defined(BOOST_CONTRACT_NO_ALL) 0057 #include <boost/contract/detail/assert.hpp> 0058 #define BOOST_CONTRACT_ASSERT(cond) \ 0059 BOOST_CONTRACT_DETAIL_ASSERT(cond) /* no `;` here */ 0060 #else 0061 // This must be an expression (a trivial one so the compiler can optimize it 0062 // away). It cannot an empty code block `{}`, etc. otherwise code like 0063 // `if(...) ASSERT(...); else ASSERT(...);` won't work when NO_ALL. 0064 #define BOOST_CONTRACT_ASSERT(cond) \ 0065 BOOST_CONTRACT_DETAIL_NOOP 0066 #endif 0067 0068 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN 0069 /** 0070 Preferred way to assert contract conditions that are computationally 0071 expensive, at least compared to the computational cost of executing the 0072 function body. 0073 0074 The asserted condition will always be compiled and validated syntactically, 0075 but it will not be checked at run-time unless 0076 @RefMacro{BOOST_CONTRACT_AUDITS} is defined (undefined by default). 0077 This macro is defined by code equivalent to: 0078 0079 @code 0080 #ifdef BOOST_CONTRACT_AUDITS 0081 #define BOOST_CONTRACT_ASSERT_AUDIT(cond) \ 0082 BOOST_CONTRACT_ASSERT(cond) 0083 #else 0084 #define BOOST_CONTRACT_ASSERT_AUDIT(cond) \ 0085 BOOST_CONTRACT_ASSERT(true || cond) 0086 #endif 0087 @endcode 0088 0089 @RefMacro{BOOST_CONTRACT_ASSERT}, @RefMacro{BOOST_CONTRACT_ASSERT_AUDIT}, 0090 and @RefMacro{BOOST_CONTRACT_ASSERT_AXIOM} are the three assertion levels 0091 predefined by this library. 0092 If there is a need, programmers are free to implement their own assertion 0093 levels defining macros similar to the one above. 0094 0095 @see @RefSect{extras.assertion_levels, Assertion Levels}, 0096 @RefSect{extras.no_macros__and_no_variadic_macros_, No Macros} 0097 0098 @param cond Boolean contract condition to check. 0099 (This is not a variadic macro parameter so any comma it might 0100 contain must be protected by round parenthesis and 0101 @c BOOST_CONTRACT_ASSERT_AUDIT((cond)) will always work.) 0102 */ 0103 #define BOOST_CONTRACT_ASSERT_AUDIT(cond) 0104 #elif defined(BOOST_CONTRACT_AUDITS) 0105 #define BOOST_CONTRACT_ASSERT_AUDIT(cond) \ 0106 BOOST_CONTRACT_ASSERT(cond) 0107 #else 0108 #define BOOST_CONTRACT_ASSERT_AUDIT(cond) \ 0109 BOOST_CONTRACT_DETAIL_NOEVAL(cond) 0110 #endif 0111 0112 /** 0113 Preferred way to document in the code contract conditions that are 0114 computationally prohibitive, at least compared to the computational cost of 0115 executing the function body. 0116 0117 The asserted condition will always be compiled and validated syntactically, but 0118 it will never be checked at run-time. 0119 This macro is defined by code equivalent to: 0120 0121 @code 0122 #define BOOST_CONTRACT_ASSERT_AXIOM(cond) \ 0123 BOOST_CONTRACT_ASSERT(true || cond) 0124 @endcode 0125 0126 @RefMacro{BOOST_CONTRACT_ASSERT}, @RefMacro{BOOST_CONTRACT_ASSERT_AUDIT}, and 0127 @RefMacro{BOOST_CONTRACT_ASSERT_AXIOM} are the three assertion levels predefined 0128 by this library. 0129 If there is a need, programmers are free to implement their own assertion levels 0130 defining macros similar to the one above. 0131 0132 @see @RefSect{extras.assertion_levels, Assertion Levels}, 0133 @RefSect{extras.no_macros__and_no_variadic_macros_, No Macros} 0134 0135 @param cond Boolean contract condition to check. 0136 (This is not a variadic macro parameter so any comma it might 0137 contain must be protected by round parenthesis and 0138 @c BOOST_CONTRACT_ASSERT_AXIOM((cond)) will always work.) 0139 */ 0140 #define BOOST_CONTRACT_ASSERT_AXIOM(cond) \ 0141 BOOST_CONTRACT_DETAIL_NOEVAL(cond) 0142 0143 #endif // #include guard 0144
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|