access.cpp 3.6 KB

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