constructor.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef BOOST_CONTRACT_DETAIL_CONSTRUCTOR_HPP_
  2. #define BOOST_CONTRACT_DETAIL_CONSTRUCTOR_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. #include <boost/contract/core/exception.hpp>
  8. #include <boost/contract/core/config.hpp>
  9. #include <boost/contract/detail/condition/cond_inv.hpp>
  10. #include <boost/contract/detail/none.hpp>
  11. #include <boost/contract/detail/exception.hpp>
  12. #if !defined(BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION) && ( \
  13. !defined(BOOST_CONTRACT_NO_INVARIANTS) || \
  14. !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  15. !defined(BOOST_CONTRACT_NO_EXCEPTS))
  16. #include <boost/contract/detail/checking.hpp>
  17. #endif
  18. #if !defined(BOOST_CONTRACT_NO_EXIT_INVARIANTS) || \
  19. !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  20. !defined(BOOST_CONTRACT_NO_EXCEPTS)
  21. #include <boost/config.hpp>
  22. #include <exception>
  23. #endif
  24. namespace boost { namespace contract { namespace detail {
  25. // Ctor subcontracting impl via C++ obj construction mechanism.
  26. template<class C> // Non-copyable base.
  27. class constructor : public cond_inv</* VR = */ none, C> {
  28. public:
  29. explicit constructor(C* obj) : cond_inv</* VR = */ none, C>(
  30. boost::contract::from_constructor, obj) {}
  31. private:
  32. #if !defined(BOOST_CONTRACT_NO_ENTRY_INVARIANTS) || \
  33. !defined(BOOST_CONTRACT_NO_OLDS)
  34. void init() /* override */ {
  35. #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
  36. if(checking::already()) return;
  37. #endif
  38. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  39. {
  40. #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
  41. checking k;
  42. #endif
  43. this->check_entry_static_inv();
  44. // No object before ctor body so check only static inv at
  45. // entry. Ctor pre checked by constructor_precondition.
  46. }
  47. #endif
  48. #ifndef BOOST_CONTRACT_NO_OLDS
  49. this->copy_old();
  50. #endif
  51. }
  52. #endif
  53. public:
  54. #if !defined(BOOST_CONTRACT_NO_EXIT_INVARIANTS) || \
  55. !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  56. !defined(BOOST_CONTRACT_NO_EXCEPTS)
  57. ~constructor() BOOST_NOEXCEPT_IF(false) {
  58. this->assert_initialized();
  59. #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
  60. if(checking::already()) return;
  61. checking k;
  62. #endif
  63. // If ctor body threw, no obj so check only static inv. Otherwise,
  64. // obj constructed so check static inv, non-static inv, and post.
  65. if(uncaught_exception()) {
  66. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  67. this->check_exit_static_inv();
  68. #endif
  69. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  70. this->check_except();
  71. #endif
  72. } else {
  73. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  74. this->check_exit_all_inv();
  75. #endif
  76. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  77. this->check_post(none());
  78. #endif
  79. }
  80. }
  81. #endif
  82. };
  83. } } } // namespace
  84. #endif // #include guard