virtual_access_multi.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 overrides with mixed access levels from different (multiple) bases.
  6. #include <boost/config.hpp>
  7. #ifdef BOOST_MSVC
  8. // WARNING: MSVC (at least up to VS 2015) gives a compile-time error if SFINAE
  9. // cannot introspect a member because of its private or protected access level.
  10. // That is incorrect, SFINAE should fail in these cases without generating
  11. // compile-time errors like GCC and CLang do. Therefore, currently it is not
  12. // possible to override a member that is public in one base but private or
  13. // protected in other base using this library on MSVC (that can be done instead
  14. // using this library on GCC or CLang).
  15. int main() { return 0; } // This test trivially passes on MSVC.
  16. #else
  17. #include "../detail/oteststream.hpp"
  18. #include <boost/contract/public_function.hpp>
  19. #include <boost/contract/function.hpp>
  20. #include <boost/contract/base_types.hpp>
  21. #include <boost/contract/override.hpp>
  22. #include <boost/contract/check.hpp>
  23. #include <boost/detail/lightweight_test.hpp>
  24. #include <sstream>
  25. boost::contract::test::detail::oteststream out;
  26. struct c { // Test public access different from base `b`'s access below.
  27. static void statci_inv() { out << "c::static_inv" << std::endl; }
  28. void invariant() const { out << "c::inv" << std::endl; }
  29. virtual void f(boost::contract::virtual_* v = 0) {
  30. boost::contract::check c = boost::contract::public_function(v, this)
  31. .precondition([] { out << "c::f::pre" << std::endl; })
  32. .old([] { out << "c::f::old" << std::endl; })
  33. .postcondition([] { out << "c::f::post" << std::endl; })
  34. ;
  35. out << "c::f::body" << std::endl;
  36. }
  37. virtual void g(boost::contract::virtual_* v = 0) {
  38. boost::contract::check c = boost::contract::public_function(v, this)
  39. .precondition([] { out << "c::g::pre" << std::endl; })
  40. .old([] { out << "c::g::old" << std::endl; })
  41. .postcondition([] { out << "c::g::post" << std::endl; })
  42. ;
  43. out << "c::g::body" << std::endl;
  44. }
  45. virtual void h(boost::contract::virtual_* v = 0) {
  46. boost::contract::check c = boost::contract::public_function(v, this)
  47. .precondition([] { out << "c::h::pre" << std::endl; })
  48. .old([] { out << "c::h::old" << std::endl; })
  49. .postcondition([] { out << "c::h::post" << std::endl; })
  50. ;
  51. out << "c::h::body" << std::endl;
  52. }
  53. };
  54. struct b { // Test all access levels (public, protected, and private).
  55. friend void call(b& me) { // Test polymorphic calls (object by &).
  56. me.f();
  57. me.g();
  58. me.h();
  59. }
  60. static void statci_inv() { out << "b::static_inv" << std::endl; }
  61. void invariant() const { out << "b::inv" << std::endl; }
  62. virtual void f(boost::contract::virtual_* v = 0) {
  63. boost::contract::check c = boost::contract::public_function(v, this)
  64. .precondition([] { out << "b::f::pre" << std::endl; })
  65. .old([] { out << "b::f::old" << std::endl; })
  66. .postcondition([] { out << "b::f::post" << std::endl; })
  67. ;
  68. out << "b::f::body" << std::endl;
  69. }
  70. // NOTE: Both protected and private virtual members must declare
  71. // extra `virtual_* = 0` parameter (otherwise they cannot be overridden in
  72. // derived classes with contracts because C++ uses also default parameters
  73. // to match signature of overriding functions).
  74. protected:
  75. virtual void g(boost::contract::virtual_* /* v */ = 0) {
  76. boost::contract::check c = boost::contract::function()
  77. .precondition([] { out << "b::g::pre" << std::endl; })
  78. .old([] { out << "b::g::old" << std::endl; })
  79. .postcondition([] { out << "b::g::post" << std::endl; })
  80. ;
  81. out << "b::g::body" << std::endl;
  82. }
  83. private:
  84. virtual void h(boost::contract::virtual_* /* v */ = 0) {
  85. boost::contract::check c = boost::contract::function()
  86. .precondition([] { out << "b::h::pre" << std::endl; })
  87. .old([] { out << "b::h::old" << std::endl; })
  88. .postcondition([] { out << "b::h::post" << std::endl; })
  89. ;
  90. out << "b::h::body" << std::endl;
  91. }
  92. };
  93. struct a // Test overrides with mixed access levels from different bases.
  94. #define BASES public b, public c
  95. : BASES
  96. {
  97. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  98. #undef BASES
  99. static void statci_inv() { out << "a::static_inv" << std::endl; }
  100. void invariant() const { out << "a::inv" << std::endl; }
  101. virtual void f(boost::contract::virtual_* v = 0) /* override */ {
  102. boost::contract::check c = boost::contract::public_function<override_f>(
  103. v, &a::f, this)
  104. .precondition([] { out << "a::f::pre" << std::endl; })
  105. .old([] { out << "a::f::old" << std::endl; })
  106. .postcondition([] { out << "a::f::post" << std::endl; })
  107. ;
  108. out << "a::f::body" << std::endl;
  109. }
  110. virtual void g(boost::contract::virtual_* v = 0) /* override */ {
  111. boost::contract::check c = boost::contract::public_function<override_g>(
  112. v, &a::g, this)
  113. .precondition([] { out << "a::g::pre" << std::endl; })
  114. .old([] { out << "a::g::old" << std::endl; })
  115. .postcondition([] { out << "a::g::post" << std::endl; })
  116. ;
  117. out << "a::g::body" << std::endl;
  118. }
  119. virtual void h(boost::contract::virtual_* v = 0) /* override */ {
  120. boost::contract::check c = boost::contract::public_function<override_h>(
  121. v, &a::h, this)
  122. .precondition([] { out << "a::h::pre" << std::endl; })
  123. .old([] { out << "a::h::old" << std::endl; })
  124. .postcondition([] { out << "a::h::post" << std::endl; })
  125. ;
  126. out << "a::h::body" << std::endl;
  127. }
  128. BOOST_CONTRACT_OVERRIDES(f, g, h)
  129. };
  130. int main() {
  131. std::ostringstream ok;
  132. b bb;
  133. out.str("");
  134. call(bb);
  135. ok.str(""); ok
  136. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  137. << "b::inv" << std::endl
  138. #endif
  139. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  140. << "b::f::pre" << std::endl
  141. #endif
  142. #ifndef BOOST_CONTRACT_NO_OLDS
  143. << "b::f::old" << std::endl
  144. #endif
  145. << "b::f::body" << std::endl
  146. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  147. << "b::inv" << std::endl
  148. #endif
  149. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  150. << "b::f::post" << std::endl
  151. #endif
  152. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  153. << "b::g::pre" << std::endl
  154. #endif
  155. #ifndef BOOST_CONTRACT_NO_OLDS
  156. << "b::g::old" << std::endl
  157. #endif
  158. << "b::g::body" << std::endl
  159. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  160. << "b::g::post" << std::endl
  161. #endif
  162. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  163. << "b::h::pre" << std::endl
  164. #endif
  165. #ifndef BOOST_CONTRACT_NO_OLDS
  166. << "b::h::old" << std::endl
  167. #endif
  168. << "b::h::body" << std::endl
  169. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  170. << "b::h::post" << std::endl
  171. #endif
  172. ;
  173. BOOST_TEST(out.eq(ok.str()));
  174. a aa;
  175. out.str("");
  176. call(aa);
  177. ok.str(""); ok
  178. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  179. << "b::inv" << std::endl
  180. << "c::inv" << std::endl
  181. << "a::inv" << std::endl
  182. #endif
  183. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  184. << "b::f::pre" << std::endl
  185. #endif
  186. #ifndef BOOST_CONTRACT_NO_OLDS
  187. << "b::f::old" << std::endl
  188. << "c::f::old" << std::endl
  189. << "a::f::old" << std::endl
  190. #endif
  191. << "a::f::body" << std::endl
  192. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  193. << "b::inv" << std::endl
  194. << "c::inv" << std::endl
  195. << "a::inv" << std::endl
  196. #endif
  197. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  198. << "b::f::old" << std::endl
  199. << "b::f::post" << std::endl
  200. << "c::f::old" << std::endl
  201. << "c::f::post" << std::endl
  202. << "a::f::post" << std::endl
  203. #endif
  204. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  205. << "c::inv" << std::endl
  206. << "a::inv" << std::endl
  207. #endif
  208. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  209. << "c::g::pre" << std::endl
  210. #endif
  211. #ifndef BOOST_CONTRACT_NO_OLDS
  212. << "c::g::old" << std::endl
  213. << "a::g::old" << std::endl
  214. #endif
  215. << "a::g::body" << std::endl
  216. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  217. << "c::inv" << std::endl
  218. << "a::inv" << std::endl
  219. #endif
  220. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  221. << "c::g::old" << std::endl
  222. << "c::g::post" << std::endl
  223. << "a::g::post" << std::endl
  224. #endif
  225. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  226. << "c::inv" << std::endl
  227. << "a::inv" << std::endl
  228. #endif
  229. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  230. << "c::h::pre" << std::endl
  231. #endif
  232. #ifndef BOOST_CONTRACT_NO_OLDS
  233. << "c::h::old" << std::endl
  234. << "a::h::old" << std::endl
  235. #endif
  236. << "a::h::body" << std::endl
  237. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  238. << "c::inv" << std::endl
  239. << "a::inv" << std::endl
  240. #endif
  241. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  242. << "c::h::old" << std::endl
  243. << "c::h::post" << std::endl
  244. << "a::h::post" << std::endl
  245. #endif
  246. ;
  247. BOOST_TEST(out.eq(ok.str()));
  248. return boost::report_errors();
  249. }
  250. #endif // MSVC