access.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/constructor.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. #define BASES private boost::contract::constructor_precondition<b>
  15. : BASES
  16. {
  17. friend class boost::contract::access;
  18. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  19. #undef BASES
  20. static void static_invariant() { out << "b::static_inv" << std::endl; }
  21. void invariant() const { out << "b::inv" << std::endl; }
  22. public:
  23. b() : boost::contract::constructor_precondition<b>([] {
  24. out << "b::ctor::pre" << std::endl;
  25. }) {
  26. boost::contract::check c = boost::contract::constructor(this)
  27. .old([] { out << "b::ctor::old" << std::endl; })
  28. .postcondition([] { out << "b::ctor::post" << std::endl; })
  29. ;
  30. out << "b::ctor::body" << std::endl;
  31. }
  32. };
  33. class a
  34. #define BASES private boost::contract::constructor_precondition<a>, public b
  35. : BASES
  36. {
  37. friend class boost::contract::access;
  38. // Private base types (always OK because never used by ctors).
  39. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  40. #undef BASES
  41. // Private invariants.
  42. static void static_invariant() { out << "a::static_inv" << std::endl; }
  43. void invariant() const { out << "a::inv" << std::endl; }
  44. public:
  45. a() : boost::contract::constructor_precondition<a>([] {
  46. out << "a::ctor::pre" << std::endl;
  47. }) {
  48. boost::contract::check c = boost::contract::constructor(this)
  49. .old([] { out << "a::ctor::old" << std::endl; })
  50. .postcondition([] { out << "a::ctor::post" << std::endl; })
  51. ;
  52. out << "a::ctor::body" << std::endl;
  53. }
  54. };
  55. int main() {
  56. std::ostringstream ok;
  57. out.str("");
  58. a aa;
  59. ok.str(""); ok
  60. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  61. << "a::ctor::pre" << std::endl
  62. << "b::ctor::pre" << std::endl
  63. #endif
  64. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  65. << "b::static_inv" << std::endl
  66. #endif
  67. #ifndef BOOST_CONTRACT_NO_OLDS
  68. << "b::ctor::old" << std::endl
  69. #endif
  70. << "b::ctor::body" << std::endl
  71. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  72. << "b::static_inv" << std::endl
  73. << "b::inv" << std::endl
  74. #endif
  75. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  76. << "b::ctor::post" << std::endl
  77. #endif
  78. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  79. << "a::static_inv" << std::endl
  80. #endif
  81. #ifndef BOOST_CONTRACT_NO_OLDS
  82. << "a::ctor::old" << std::endl
  83. #endif
  84. << "a::ctor::body" << std::endl
  85. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  86. << "a::static_inv" << std::endl
  87. << "a::inv" << std::endl
  88. #endif
  89. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  90. << "a::ctor::post" << std::endl
  91. #endif
  92. ;
  93. BOOST_TEST(out.eq(ok.str()));
  94. out.str("");
  95. b bb;
  96. ok.str(""); ok
  97. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  98. << "b::ctor::pre" << std::endl
  99. #endif
  100. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  101. << "b::static_inv" << std::endl
  102. #endif
  103. #ifndef BOOST_CONTRACT_NO_OLDS
  104. << "b::ctor::old" << std::endl
  105. #endif
  106. << "b::ctor::body" << std::endl
  107. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  108. << "b::static_inv" << std::endl
  109. << "b::inv" << std::endl
  110. #endif
  111. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  112. << "b::ctor::post" << std::endl
  113. #endif
  114. ;
  115. BOOST_TEST(out.eq(ok.str()));
  116. return boost::report_errors();
  117. }