decl.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef BOOST_CONTRACT_TEST_DESTRUCTOR_DECL_HPP_
  2. #define BOOST_CONTRACT_TEST_DESTRUCTOR_DECL_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. // Test with and without pre, post, and inv declarations.
  8. #include "../detail/oteststream.hpp"
  9. #include <boost/contract/destructor.hpp>
  10. #include <boost/contract/base_types.hpp>
  11. #include <boost/contract/check.hpp>
  12. #include <boost/contract/assert.hpp>
  13. #include <boost/config.hpp>
  14. boost::contract::test::detail::oteststream out;
  15. bool c_pre = true, c_post = true;
  16. bool c_entering_static_inv = true, c_entry_static_inv = true,
  17. c_exit_static_inv = true;
  18. bool c_entry_inv = true; // Only entry non-static inv for dtors.
  19. struct c {
  20. #ifndef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
  21. static void static_invariant() {
  22. out << "c::static_inv" << std::endl;
  23. if(c_entering_static_inv) BOOST_CONTRACT_ASSERT(c_entry_static_inv);
  24. else BOOST_CONTRACT_ASSERT(c_exit_static_inv);
  25. c_entering_static_inv = false;
  26. }
  27. #endif
  28. #ifndef BOOST_CONTRACT_TEST_NO_C_INV
  29. void invariant() const {
  30. out << "c::inv" << std::endl;
  31. BOOST_CONTRACT_ASSERT(c_entry_inv);
  32. }
  33. #endif
  34. virtual ~c() BOOST_NOEXCEPT_IF(false) {
  35. boost::contract::check c = boost::contract::destructor(this)
  36. #ifdef BOOST_CONTRACT_TEST_NO_C_PRE
  37. #error "destructors cannot have preconditions"
  38. #endif
  39. .old([] { out << "c::dtor::old" << std::endl; })
  40. #ifndef BOOST_CONTRACT_TEST_NO_C_POST
  41. .postcondition([] {
  42. out << "c::dtor::post" << std::endl;
  43. BOOST_CONTRACT_ASSERT(c_post);
  44. })
  45. #endif
  46. ;
  47. out << "c::dtor::body" << std::endl;
  48. }
  49. };
  50. bool b_pre = true, b_post = true;
  51. bool b_entering_static_inv = true, b_entry_static_inv = true,
  52. b_exit_static_inv = true;
  53. bool b_entry_inv = true; // Only entry non-static inv for dtors.
  54. struct b
  55. #define BASES public c
  56. : BASES
  57. {
  58. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  59. #undef BASES
  60. #ifndef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  61. static void static_invariant() {
  62. out << "b::static_inv" << std::endl;
  63. if(b_entering_static_inv) BOOST_CONTRACT_ASSERT(b_entry_static_inv);
  64. else BOOST_CONTRACT_ASSERT(b_exit_static_inv);
  65. b_entering_static_inv = false;
  66. }
  67. #endif
  68. #ifndef BOOST_CONTRACT_TEST_NO_B_INV
  69. void invariant() const {
  70. out << "b::inv" << std::endl;
  71. BOOST_CONTRACT_ASSERT(b_entry_inv);
  72. }
  73. #endif
  74. virtual ~b() BOOST_NOEXCEPT_IF(false) {
  75. boost::contract::check c = boost::contract::destructor(this)
  76. #ifdef BOOST_CONTRACT_TEST_NO_B_PRE
  77. #error "destructors cannot have preconditions"
  78. #endif
  79. .old([] { out << "b::dtor::old" << std::endl; })
  80. #ifndef BOOST_CONTRACT_TEST_NO_B_POST
  81. .postcondition([] {
  82. out << "b::dtor::post" << std::endl;
  83. BOOST_CONTRACT_ASSERT(b_post);
  84. })
  85. #endif
  86. ;
  87. out << "b::dtor::body" << std::endl;
  88. }
  89. };
  90. bool a_pre = true, a_post = true;
  91. bool a_entering_static_inv = true, a_entry_static_inv = true,
  92. a_exit_static_inv = true;
  93. bool a_entry_inv = true; // Only entry non-static inv for dtors.
  94. struct a
  95. #define BASES public b
  96. : BASES
  97. {
  98. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  99. #undef BASES
  100. #ifndef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  101. static void static_invariant() {
  102. out << "a::static_inv" << std::endl;
  103. if(a_entering_static_inv) BOOST_CONTRACT_ASSERT(a_entry_static_inv);
  104. else BOOST_CONTRACT_ASSERT(a_exit_static_inv);
  105. a_entering_static_inv = false;
  106. }
  107. #endif
  108. #ifndef BOOST_CONTRACT_TEST_NO_A_INV
  109. void invariant() const {
  110. out << "a::inv" << std::endl;
  111. BOOST_CONTRACT_ASSERT(a_entry_inv);
  112. }
  113. #endif
  114. virtual ~a() BOOST_NOEXCEPT_IF(false) {
  115. boost::contract::check c = boost::contract::destructor(this)
  116. #ifdef BOOST_CONTRACT_TEST_NO_A_PRE
  117. #error "destructors cannot have preconditions"
  118. #endif
  119. .old([] { out << "a::dtor::old" << std::endl; })
  120. #ifndef BOOST_CONTRACT_TEST_NO_A_POST
  121. .postcondition([] {
  122. out << "a::dtor::post" << std::endl;
  123. BOOST_CONTRACT_ASSERT(a_post);
  124. })
  125. #endif
  126. ;
  127. out << "a::dtor::body" << std::endl;
  128. }
  129. };
  130. #endif // #include guard