1 2 // Copyright (C) 2008-2018 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 (see accompanying 4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 6 7 // Test making all contract extra declarations (base types, inv, etc.) private. 8 9 #include "../detail/oteststream.hpp" 10 #include <boost/contract/destructor.hpp> 11 #include <boost/contract/base_types.hpp> 12 #include <boost/contract/check.hpp> 13 #include <boost/detail/lightweight_test.hpp> 14 #include <sstream> 15 16 boost::contract::test::detail::oteststream out; 17 18 class b { 19 friend class boost::contract::access; 20 static_invariant()21 static void static_invariant() { out << "b::static_inv" << std::endl; } invariant() const22 void invariant() const { out << "b::inv" << std::endl; } 23 24 public: ~b()25 virtual ~b() { 26 boost::contract::check c = boost::contract::destructor(this) 27 .old([] { out << "b::dtor::old" << std::endl; }) 28 .postcondition([] { out << "b::dtor::post" << std::endl; }) 29 ; 30 out << "b::dtor::body" << std::endl; 31 } 32 }; 33 34 class a 35 #define BASES public b 36 : BASES 37 { 38 friend class boost::contract::access; 39 40 // Private base types (always OK because never used by dtors). 41 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; 42 #undef BASES 43 44 // Private invariants. static_invariant()45 static void static_invariant() { out << "a::static_inv" << std::endl; } invariant() const46 void invariant() const { out << "a::inv" << std::endl; } 47 48 public: ~a()49 virtual ~a() { 50 boost::contract::check c = boost::contract::destructor(this) 51 .old([] { out << "a::dtor::old" << std::endl; }) 52 .postcondition([] { out << "a::dtor::post" << std::endl; }) 53 ; 54 out << "a::dtor::body" << std::endl; 55 } 56 }; 57 main()58int main() { 59 std::ostringstream ok; 60 61 { 62 a aa; 63 out.str(""); 64 } // Call aa's destructor. 65 ok.str(""); ok 66 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 67 << "a::static_inv" << std::endl 68 << "a::inv" << std::endl 69 #endif 70 #ifndef BOOST_CONTRACT_NO_OLDS 71 << "a::dtor::old" << std::endl 72 #endif 73 << "a::dtor::body" << std::endl 74 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 75 << "a::static_inv" << std::endl 76 #endif 77 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 78 << "a::dtor::post" << std::endl 79 #endif 80 81 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 82 << "b::static_inv" << std::endl 83 << "b::inv" << std::endl 84 #endif 85 #ifndef BOOST_CONTRACT_NO_OLDS 86 << "b::dtor::old" << std::endl 87 #endif 88 << "b::dtor::body" << std::endl 89 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 90 << "b::static_inv" << std::endl 91 #endif 92 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 93 << "b::dtor::post" << std::endl 94 #endif 95 ; 96 BOOST_TEST(out.eq(ok.str())); 97 98 { 99 b bb; 100 out.str(""); 101 } // Call bb's destructor. 102 ok.str(""); ok 103 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 104 << "b::static_inv" << std::endl 105 << "b::inv" << std::endl 106 #endif 107 #ifndef BOOST_CONTRACT_NO_OLDS 108 << "b::dtor::old" << std::endl 109 #endif 110 << "b::dtor::body" << std::endl 111 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 112 << "b::static_inv" << std::endl 113 #endif 114 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 115 << "b::dtor::post" << std::endl 116 #endif 117 ; 118 BOOST_TEST(out.eq(ok.str())); 119 120 return boost::report_errors(); 121 } 122 123