]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/contract/test/public_function/virtual_access.cpp
2 // Copyright (C) 2008-2018 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0 (see accompanying
4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
7 // Test overrides with mixed access level from (single) base.
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/public_function.hpp>
11 #include <boost/contract/function.hpp>
12 #include <boost/contract/base_types.hpp>
13 #include <boost/contract/override.hpp>
14 #include <boost/contract/check.hpp>
15 #include <boost/detail/lightweight_test.hpp>
18 boost::contract::test::detail::oteststream out
;
20 struct b
{ // Test all access levels (public, protected, and private).
21 friend void call(b
& me
) { // Test polymorphic calls (object by &).
27 static void statci_inv() { out
<< "b::static_inv" << std::endl
; }
28 void invariant() const { out
<< "b::inv" << std::endl
; }
30 virtual void f(boost::contract::virtual_
* v
= 0) {
31 boost::contract::check c
= boost::contract::public_function(v
, this)
32 .precondition([] { out
<< "b::f::pre" << std::endl
; })
33 .old([] { out
<< "b::f::old" << std::endl
; })
34 .postcondition([] { out
<< "b::f::post" << std::endl
; })
36 out
<< "b::f::body" << std::endl
;
39 // NOTE: Both protected and private virtual members must declare
40 // extra `virtual_* = 0` parameter (otherwise they cannot be overridden in
41 // derived classes with contracts because C++ uses also default parameters
42 // to match signature of overriding functions).
45 virtual void g(boost::contract::virtual_
* /* v */= 0) {
46 boost::contract::check c
= boost::contract::function()
47 .precondition([] { out
<< "b::g::pre" << std::endl
; })
48 .old([] { out
<< "b::g::old" << std::endl
; })
49 .postcondition([] { out
<< "b::g::post" << std::endl
; })
51 out
<< "b::g::body" << std::endl
;
55 virtual void h(boost::contract::virtual_
* /* v */ = 0) {
56 boost::contract::check c
= boost::contract::function()
57 .precondition([] { out
<< "b::h::pre" << std::endl
; })
58 .old([] { out
<< "b::h::old" << std::endl
; })
59 .postcondition([] { out
<< "b::h::post" << std::endl
; })
61 out
<< "b::h::body" << std::endl
;
65 struct a
// Test overrides with mixed access levels from base.
66 #define BASES public b
69 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
;
72 static void statci_inv() { out
<< "a::static_inv" << std::endl
; }
73 void invariant() const { out
<< "a::inv" << std::endl
; }
75 virtual void f(boost::contract::virtual_
* v
= 0) /* override */ {
76 boost::contract::check c
= boost::contract::public_function
<override_f
>(
78 .precondition([] { out
<< "a::f::pre" << std::endl
; })
79 .old([] { out
<< "a::f::old" << std::endl
; })
80 .postcondition([] { out
<< "a::f::post" << std::endl
; })
82 out
<< "a::f::body" << std::endl
;
84 BOOST_CONTRACT_OVERRIDES(f
)
86 // Following do not override public members so no `override_...` param and
87 // they do not actually subcontract.
89 virtual void g(boost::contract::virtual_
* v
= 0) /* override */ {
90 boost::contract::check c
= boost::contract::public_function(v
, this)
91 .precondition([] { out
<< "a::g::pre" << std::endl
; })
92 .old([] { out
<< "a::g::old" << std::endl
; })
93 .postcondition([] { out
<< "a::g::post" << std::endl
; })
95 out
<< "a::g::body" << std::endl
;
98 virtual void h(boost::contract::virtual_
* v
= 0) /* override */ {
99 boost::contract::check c
= boost::contract::public_function(v
, this)
100 .precondition([] { out
<< "a::h::pre" << std::endl
; })
101 .old([] { out
<< "a::h::old" << std::endl
; })
102 .postcondition([] { out
<< "a::h::post" << std::endl
; })
104 out
<< "a::h::body" << std::endl
;
109 std::ostringstream ok
;
115 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
116 << "b::inv" << std::endl
118 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
119 << "b::f::pre" << std::endl
121 #ifndef BOOST_CONTRACT_NO_OLDS
122 << "b::f::old" << std::endl
124 << "b::f::body" << std::endl
125 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
126 << "b::inv" << std::endl
128 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
129 << "b::f::post" << std::endl
132 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
133 << "b::g::pre" << std::endl
135 #ifndef BOOST_CONTRACT_NO_OLDS
136 << "b::g::old" << std::endl
138 << "b::g::body" << std::endl
139 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
140 << "b::g::post" << std::endl
143 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
144 << "b::h::pre" << std::endl
146 #ifndef BOOST_CONTRACT_NO_OLDS
147 << "b::h::old" << std::endl
149 << "b::h::body" << std::endl
150 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
151 << "b::h::post" << std::endl
154 BOOST_TEST(out
.eq(ok
.str()));
160 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
161 << "b::inv" << std::endl
162 << "a::inv" << std::endl
164 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
165 << "b::f::pre" << std::endl
167 #ifndef BOOST_CONTRACT_NO_OLDS
168 << "b::f::old" << std::endl
169 << "a::f::old" << std::endl
171 << "a::f::body" << std::endl
172 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
173 << "b::inv" << std::endl
174 << "a::inv" << std::endl
176 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
177 << "b::f::old" << std::endl
178 << "b::f::post" << std::endl
179 << "a::f::post" << std::endl
182 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
183 << "a::inv" << std::endl
185 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
186 << "a::g::pre" << std::endl
188 #ifndef BOOST_CONTRACT_NO_OLDS
189 << "a::g::old" << std::endl
191 << "a::g::body" << std::endl
192 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
193 << "a::inv" << std::endl
195 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
196 << "a::g::post" << std::endl
199 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
200 << "a::inv" << std::endl
202 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
203 << "a::h::pre" << std::endl
205 #ifndef BOOST_CONTRACT_NO_OLDS
206 << "a::h::old" << std::endl
208 << "a::h::body" << std::endl
209 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
210 << "a::inv" << std::endl
212 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
213 << "a::h::post" << std::endl
216 BOOST_TEST(out
.eq(ok
.str()));
218 return boost::report_errors();