access.cpp 4.2 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/base_types.hpp>
  8. #include <boost/contract/override.hpp>
  9. #include <boost/contract/public_function.hpp>
  10. #include <boost/contract/check.hpp>
  11. #include <boost/contract/assert.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <sstream>
  14. boost::contract::test::detail::oteststream out;
  15. class b {
  16. friend class boost::contract::access;
  17. static void static_invariant() { out << "b::static_inv" << std::endl; }
  18. void invariant() const { out << "b::inv" << std::endl; }
  19. public:
  20. virtual void f(char ch, boost::contract::virtual_* v = 0) {
  21. boost::contract::check c = boost::contract::public_function(v, this)
  22. .precondition([&] {
  23. out << "b::f::pre" << std::endl;
  24. BOOST_CONTRACT_ASSERT(ch == 'b');
  25. })
  26. .old([] { out << "b::f::old" << std::endl; })
  27. .postcondition([] { out << "b::f::post" << std::endl; })
  28. ;
  29. out << "b::f::body" << std::endl;
  30. }
  31. };
  32. class a
  33. #define BASES public b
  34. : BASES
  35. {
  36. friend class boost::contract::access;
  37. // Private base types.
  38. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  39. #undef BASES
  40. // Private invariants.
  41. static void static_invariant() { out << "a::static_inv" << std::endl; }
  42. void invariant() const { out << "a::inv" << std::endl; }
  43. // Private override (always possible even when access is not friend).
  44. BOOST_CONTRACT_OVERRIDE(f)
  45. public:
  46. virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
  47. boost::contract::check c = boost::contract::public_function<override_f>(
  48. v, &a::f, this, ch)
  49. .precondition([&] {
  50. out << "a::f::pre" << std::endl;
  51. BOOST_CONTRACT_ASSERT(ch == 'a');
  52. })
  53. .old([] { out << "a::f::old" << std::endl; })
  54. .postcondition([] { out << "a::f::post" << std::endl; })
  55. ;
  56. out << "a::f::body" << std::endl;
  57. }
  58. };
  59. int main() {
  60. std::ostringstream ok;
  61. a aa;
  62. out.str("");
  63. aa.f('a');
  64. ok.str(""); ok
  65. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  66. << "b::static_inv" << std::endl
  67. << "b::inv" << std::endl
  68. << "a::static_inv" << std::endl
  69. << "a::inv" << std::endl
  70. #endif
  71. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  72. << "b::f::pre" << std::endl
  73. << "a::f::pre" << std::endl
  74. #endif
  75. #ifndef BOOST_CONTRACT_NO_OLDS
  76. << "b::f::old" << std::endl
  77. << "a::f::old" << std::endl
  78. #endif
  79. << "a::f::body" << std::endl
  80. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  81. << "b::static_inv" << std::endl
  82. << "b::inv" << std::endl
  83. << "a::static_inv" << std::endl
  84. << "a::inv" << std::endl
  85. #endif
  86. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  87. << "b::f::old" << std::endl
  88. << "b::f::post" << std::endl
  89. // No old call here because not a base object.
  90. << "a::f::post" << std::endl
  91. #endif
  92. ;
  93. BOOST_TEST(out.eq(ok.str()));
  94. b bb;
  95. out.str("");
  96. bb.f('b');
  97. ok.str(""); ok
  98. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  99. << "b::static_inv" << std::endl
  100. << "b::inv" << std::endl
  101. #endif
  102. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  103. << "b::f::pre" << std::endl
  104. #endif
  105. #ifndef BOOST_CONTRACT_NO_OLDS
  106. << "b::f::old" << std::endl
  107. #endif
  108. << "b::f::body" << std::endl
  109. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  110. << "b::static_inv" << std::endl
  111. << "b::inv" << std::endl
  112. #endif
  113. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  114. << "b::f::post" << std::endl
  115. #endif
  116. ;
  117. BOOST_TEST(out.eq(ok.str()));
  118. return boost::report_errors();
  119. }