1 2 #ifndef BOOST_CONTRACT_TEST_DESTRUCTOR_DECL_HPP_ 3 #define BOOST_CONTRACT_TEST_DESTRUCTOR_DECL_HPP_ 4 5 // Copyright (C) 2008-2018 Lorenzo Caminiti 6 // Distributed under the Boost Software License, Version 1.0 (see accompanying 7 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 8 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 9 10 // Test with and without pre, post, and inv declarations. 11 12 #include "../detail/oteststream.hpp" 13 #include <boost/contract/destructor.hpp> 14 #include <boost/contract/base_types.hpp> 15 #include <boost/contract/check.hpp> 16 #include <boost/contract/assert.hpp> 17 #include <boost/config.hpp> 18 19 boost::contract::test::detail::oteststream out; 20 21 bool c_pre = true, c_post = true; 22 bool c_entering_static_inv = true, c_entry_static_inv = true, 23 c_exit_static_inv = true; 24 bool c_entry_inv = true; // Only entry non-static inv for dtors. 25 struct c { 26 #ifndef BOOST_CONTRACT_TEST_NO_C_STATIC_INV static_invariantc27 static void static_invariant() { 28 out << "c::static_inv" << std::endl; 29 if(c_entering_static_inv) BOOST_CONTRACT_ASSERT(c_entry_static_inv); 30 else BOOST_CONTRACT_ASSERT(c_exit_static_inv); 31 c_entering_static_inv = false; 32 } 33 #endif 34 #ifndef BOOST_CONTRACT_TEST_NO_C_INV invariantc35 void invariant() const { 36 out << "c::inv" << std::endl; 37 BOOST_CONTRACT_ASSERT(c_entry_inv); 38 } 39 #endif 40 BOOST_NOEXCEPT_IFc41 virtual ~c() BOOST_NOEXCEPT_IF(false) { 42 boost::contract::check c = boost::contract::destructor(this) 43 #ifdef BOOST_CONTRACT_TEST_NO_C_PRE 44 #error "destructors cannot have preconditions" 45 #endif 46 .old([] { out << "c::dtor::old" << std::endl; }) 47 #ifndef BOOST_CONTRACT_TEST_NO_C_POST 48 .postcondition([] { 49 out << "c::dtor::post" << std::endl; 50 BOOST_CONTRACT_ASSERT(c_post); 51 }) 52 #endif 53 ; 54 out << "c::dtor::body" << std::endl; 55 } 56 }; 57 58 bool b_pre = true, b_post = true; 59 bool b_entering_static_inv = true, b_entry_static_inv = true, 60 b_exit_static_inv = true; 61 bool b_entry_inv = true; // Only entry non-static inv for dtors. 62 struct b 63 #define BASES public c 64 : BASES 65 { 66 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; 67 #undef BASES 68 69 #ifndef BOOST_CONTRACT_TEST_NO_B_STATIC_INV static_invariantb70 static void static_invariant() { 71 out << "b::static_inv" << std::endl; 72 if(b_entering_static_inv) BOOST_CONTRACT_ASSERT(b_entry_static_inv); 73 else BOOST_CONTRACT_ASSERT(b_exit_static_inv); 74 b_entering_static_inv = false; 75 } 76 #endif 77 #ifndef BOOST_CONTRACT_TEST_NO_B_INV invariantb78 void invariant() const { 79 out << "b::inv" << std::endl; 80 BOOST_CONTRACT_ASSERT(b_entry_inv); 81 } 82 #endif 83 BOOST_NOEXCEPT_IFb84 virtual ~b() BOOST_NOEXCEPT_IF(false) { 85 boost::contract::check c = boost::contract::destructor(this) 86 #ifdef BOOST_CONTRACT_TEST_NO_B_PRE 87 #error "destructors cannot have preconditions" 88 #endif 89 .old([] { out << "b::dtor::old" << std::endl; }) 90 #ifndef BOOST_CONTRACT_TEST_NO_B_POST 91 .postcondition([] { 92 out << "b::dtor::post" << std::endl; 93 BOOST_CONTRACT_ASSERT(b_post); 94 }) 95 #endif 96 ; 97 out << "b::dtor::body" << std::endl; 98 } 99 }; 100 101 bool a_pre = true, a_post = true; 102 bool a_entering_static_inv = true, a_entry_static_inv = true, 103 a_exit_static_inv = true; 104 bool a_entry_inv = true; // Only entry non-static inv for dtors. 105 struct a 106 #define BASES public b 107 : BASES 108 { 109 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; 110 #undef BASES 111 112 #ifndef BOOST_CONTRACT_TEST_NO_A_STATIC_INV static_invarianta113 static void static_invariant() { 114 out << "a::static_inv" << std::endl; 115 if(a_entering_static_inv) BOOST_CONTRACT_ASSERT(a_entry_static_inv); 116 else BOOST_CONTRACT_ASSERT(a_exit_static_inv); 117 a_entering_static_inv = false; 118 } 119 #endif 120 #ifndef BOOST_CONTRACT_TEST_NO_A_INV invarianta121 void invariant() const { 122 out << "a::inv" << std::endl; 123 BOOST_CONTRACT_ASSERT(a_entry_inv); 124 } 125 #endif 126 BOOST_NOEXCEPT_IFa127 virtual ~a() BOOST_NOEXCEPT_IF(false) { 128 boost::contract::check c = boost::contract::destructor(this) 129 #ifdef BOOST_CONTRACT_TEST_NO_A_PRE 130 #error "destructors cannot have preconditions" 131 #endif 132 .old([] { out << "a::dtor::old" << std::endl; }) 133 #ifndef BOOST_CONTRACT_TEST_NO_A_POST 134 .postcondition([] { 135 out << "a::dtor::post" << std::endl; 136 BOOST_CONTRACT_ASSERT(a_post); 137 }) 138 #endif 139 ; 140 out << "a::dtor::body" << std::endl; 141 } 142 }; 143 144 #endif // #include guard 145 146