2 #ifndef BOOST_CONTRACT_DETAIL_DESTRUCTOR_HPP_
3 #define BOOST_CONTRACT_DETAIL_DESTRUCTOR_HPP_
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
10 #include <boost/contract/core/exception.hpp>
11 #include <boost/contract/core/config.hpp>
12 #include <boost/contract/detail/condition/cond_inv.hpp>
13 #include <boost/contract/detail/none.hpp>
14 #include <boost/contract/detail/exception.hpp>
15 #if !defined(BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION) && ( \
16 !defined(BOOST_CONTRACT_NO_INVARIANTS) || \
17 !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
18 !defined(BOOST_CONTRACT_NO_EXCEPTS))
19 #include <boost/contract/detail/checking.hpp>
21 #if !defined(BOOST_CONTRACT_NO_EXIT_INVARIANTS) || \
22 !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
23 !defined(BOOST_CONTRACT_NO_EXCEPTS)
24 #include <boost/config.hpp>
28 namespace boost { namespace contract { namespace detail {
30 // Dtor subcontracting impl via C++ obj destruction mechanism.
31 template<class C> // Non-copyable base.
32 class destructor : public cond_inv</* VR = */ none, C> {
34 explicit destructor(C* obj) : cond_inv</* VR = */ none, C>(
35 boost::contract::from_destructor, obj) {}
38 #if !defined(BOOST_CONTRACT_NO_ENTRY_INVARIANTS) || \
39 !defined(BOOST_CONTRACT_NO_OLDS)
40 void init() /* override */ {
41 #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
42 if(checking::already()) return;
45 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
47 #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
50 // Obj exists (before dtor body), check static and non- inv.
51 this->check_entry_all_inv();
52 // Dtor cannot have pre because it has no parameters.
55 #ifndef BOOST_CONTRACT_NO_OLDS
62 #if !defined(BOOST_CONTRACT_NO_EXIT_INVARIANTS) || \
63 !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
64 !defined(BOOST_CONTRACT_NO_EXCEPTS)
65 ~destructor() BOOST_NOEXCEPT_IF(false) {
66 this->assert_initialized();
67 #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
68 if(checking::already()) return;
72 // If dtor body threw, obj still exists so check subcontracted
73 // static and non- inv (but no post because of throw). Otherwise,
74 // obj destructed so check static inv and post (even if there is no
75 // obj after dtor body, this library allows dtor post, for example
76 // to check static members for an instance counter class).
77 // NOTE: In theory C++ destructors should not throw, but the
78 // language allows for that (even if in C++11 dtors declarations are
79 // implicitly noexcept(true) unless specified otherwise) so this
80 // library must handle such a case.
81 if(uncaught_exception()) {
82 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
83 this->check_exit_all_inv();
85 #ifndef BOOST_CONTRACT_NO_EXCEPTS
89 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
90 this->check_exit_static_inv();
92 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
93 this->check_post(none());
102 #endif // #include guard