decl.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef BOOST_CONTRACT_TEST_CONSTRUCTOR_DECL_HPP_
  2. #define BOOST_CONTRACT_TEST_CONSTRUCTOR_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/constructor.hpp>
  10. #include <boost/contract/base_types.hpp>
  11. #include <boost/contract/check.hpp>
  12. #include <boost/contract/assert.hpp>
  13. boost::contract::test::detail::oteststream out;
  14. bool c_pre = true, c_post = true;
  15. bool c_entering_static_inv = true, c_entry_static_inv = true,
  16. c_exit_static_inv = true;
  17. bool c_exit_inv = true; // Only exit non-static inv for ctors.
  18. struct c
  19. #ifndef BOOST_CONTRACT_TEST_NO_C_PRE
  20. : private boost::contract::constructor_precondition<c>
  21. #endif
  22. {
  23. #ifndef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
  24. static void static_invariant() {
  25. out << "c::static_inv" << std::endl;
  26. if(c_entering_static_inv) BOOST_CONTRACT_ASSERT(c_entry_static_inv);
  27. else BOOST_CONTRACT_ASSERT(c_exit_static_inv);
  28. c_entering_static_inv = false;
  29. }
  30. #endif
  31. #ifndef BOOST_CONTRACT_TEST_NO_C_INV
  32. void invariant() const {
  33. out << "c::inv" << std::endl;
  34. BOOST_CONTRACT_ASSERT(c_exit_inv);
  35. }
  36. #endif
  37. c()
  38. #ifndef BOOST_CONTRACT_TEST_NO_C_PRE
  39. : boost::contract::constructor_precondition<c>([] {
  40. out << "c::ctor::pre" << std::endl;
  41. BOOST_CONTRACT_ASSERT(c_pre);
  42. })
  43. #endif
  44. {
  45. boost::contract::check c = boost::contract::constructor(this)
  46. .old([] { out << "c::ctor::old" << std::endl; })
  47. #ifndef BOOST_CONTRACT_TEST_NO_C_POST
  48. .postcondition([] {
  49. out << "c::ctor::post" << std::endl;
  50. BOOST_CONTRACT_ASSERT(c_post);
  51. })
  52. #endif
  53. ;
  54. out << "c::ctor::body" << std::endl;
  55. }
  56. };
  57. bool b_pre = true, b_post = true;
  58. bool b_entering_static_inv = true, b_entry_static_inv = true,
  59. b_exit_static_inv = true;
  60. bool b_exit_inv = true; // Only exit non-static inv for ctors.
  61. struct b
  62. #ifndef BOOST_CONTRACT_TEST_NO_B_PRE
  63. #define BASES \
  64. private boost::contract::constructor_precondition<b>, public c
  65. #else
  66. #define BASES public c
  67. #endif
  68. : BASES
  69. {
  70. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  71. #undef BASES
  72. #ifndef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  73. static void static_invariant() {
  74. out << "b::static_inv" << std::endl;
  75. if(b_entering_static_inv) BOOST_CONTRACT_ASSERT(b_entry_static_inv);
  76. else BOOST_CONTRACT_ASSERT(b_exit_static_inv);
  77. b_entering_static_inv = false;
  78. }
  79. #endif
  80. #ifndef BOOST_CONTRACT_TEST_NO_B_INV
  81. void invariant() const {
  82. out << "b::inv" << std::endl;
  83. BOOST_CONTRACT_ASSERT(b_exit_inv);
  84. }
  85. #endif
  86. b()
  87. #ifndef BOOST_CONTRACT_TEST_NO_B_PRE
  88. : boost::contract::constructor_precondition<b>([] {
  89. out << "b::ctor::pre" << std::endl;
  90. BOOST_CONTRACT_ASSERT(b_pre);
  91. })
  92. #endif
  93. {
  94. boost::contract::check c = boost::contract::constructor(this)
  95. .old([] { out << "b::ctor::old" << std::endl; })
  96. #ifndef BOOST_CONTRACT_TEST_NO_B_POST
  97. .postcondition([] {
  98. out << "b::ctor::post" << std::endl;
  99. BOOST_CONTRACT_ASSERT(b_post);
  100. })
  101. #endif
  102. ;
  103. out << "b::ctor::body" << std::endl;
  104. }
  105. };
  106. bool a_pre = true, a_post = true;
  107. bool a_entering_static_inv = true, a_entry_static_inv = true,
  108. a_exit_static_inv = true;
  109. bool a_exit_inv = true; // Only exit non-static inv for ctors.
  110. struct a
  111. #ifndef BOOST_CONTRACT_TEST_NO_A_PRE
  112. #define BASES \
  113. private boost::contract::constructor_precondition<a>, public b
  114. #else
  115. #define BASES public b
  116. #endif
  117. : BASES
  118. {
  119. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  120. #undef BASES
  121. #ifndef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  122. static void static_invariant() {
  123. out << "a::static_inv" << std::endl;
  124. if(a_entering_static_inv) BOOST_CONTRACT_ASSERT(a_entry_static_inv);
  125. else BOOST_CONTRACT_ASSERT(a_exit_static_inv);
  126. a_entering_static_inv = false;
  127. }
  128. #endif
  129. #ifndef BOOST_CONTRACT_TEST_NO_A_INV
  130. void invariant() const {
  131. out << "a::inv" << std::endl;
  132. BOOST_CONTRACT_ASSERT(a_exit_inv);
  133. }
  134. #endif
  135. a()
  136. #ifndef BOOST_CONTRACT_TEST_NO_A_PRE
  137. : boost::contract::constructor_precondition<a>([] {
  138. out << "a::ctor::pre" << std::endl;
  139. BOOST_CONTRACT_ASSERT(a_pre);
  140. })
  141. #endif
  142. {
  143. boost::contract::check c = boost::contract::constructor(this)
  144. .old([] { out << "a::ctor::old" << std::endl; })
  145. #ifndef BOOST_CONTRACT_TEST_NO_A_POST
  146. .postcondition([] {
  147. out << "a::ctor::post" << std::endl;
  148. BOOST_CONTRACT_ASSERT(a_post);
  149. })
  150. #endif
  151. ;
  152. out << "a::ctor::body" << std::endl;
  153. }
  154. };
  155. #endif // #include guard