]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/contract/example/features/private_protected_virtual.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 #include <boost/contract.hpp>
11 //[private_protected_virtual_counter
13 // Virtual private and protected functions still declare extra
14 // `virtual_* = 0` parameter (otherwise they cannot be overridden), but...
16 virtual void set(int n
, boost::contract::virtual_
* = 0) {
17 boost::contract::check c
= boost::contract::function() // ...no `v`.
19 BOOST_CONTRACT_ASSERT(n
<= 0);
22 BOOST_CONTRACT_ASSERT(get() == n
);
30 virtual void dec(boost::contract::virtual_
* = 0) {
31 boost::contract::old_ptr
<int> old_get
= BOOST_CONTRACT_OLDOF(get()); // ...no `v`.
32 boost::contract::check c
= boost::contract::function() // ...no `v`.
34 BOOST_CONTRACT_ASSERT(
35 get() + 1 >= std::numeric_limits
<int>::min());
38 BOOST_CONTRACT_ASSERT(get() == *old_get
- 1);
51 virtual int get(boost::contract::virtual_
* v
= 0) const {
53 boost::contract::check c
= boost::contract::public_function(
55 .postcondition([&] (int const result
) {
56 BOOST_CONTRACT_ASSERT(result
<= 0);
57 BOOST_CONTRACT_ASSERT(result
== n_
);
64 counter() : n_(0) {} // Should contract constructor and destructor too.
66 void invariant() const {
67 BOOST_CONTRACT_ASSERT(get() <= 0);
73 //[private_protected_virtual_counter10
75 #define BASES public counter
79 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
;
82 // Overriding from non-public members so no subcontracting, no override_...
84 virtual void set(int n
, boost::contract::virtual_
* v
= 0) /* override */ {
85 boost::contract::check c
= boost::contract::public_function(v
, this)
87 BOOST_CONTRACT_ASSERT(n
% 10 == 0);
90 BOOST_CONTRACT_ASSERT(get() == n
);
97 virtual void dec(boost::contract::virtual_
* v
= 0) /* override */ {
98 boost::contract::old_ptr
<int> old_get
= BOOST_CONTRACT_OLDOF(v
, get());
99 boost::contract::check c
= boost::contract::public_function(v
, this)
101 BOOST_CONTRACT_ASSERT(
102 get() + 10 >= std::numeric_limits
<int>::min());
105 BOOST_CONTRACT_ASSERT(get() == *old_get
- 10);
115 virtual int get(boost::contract::virtual_
* v
= 0) const {
117 boost::contract::check c
= boost::contract::public_function
<
118 override_get
>(v
, result
, &counter10::get
, this);
120 return result
= counter::get();
122 BOOST_CONTRACT_OVERRIDE(get
)
124 // Should contract default constructor and destructor too.
126 void invariant() const {
127 BOOST_CONTRACT_ASSERT(get() % 10 == 0);
133 assert(cnt
.get() == 0);
135 assert(cnt
.get() == -1);
138 counter
& b
= cnt10
; // Polymorphic calls.
139 assert(b
.get() == 0);
141 assert(b
.get() == -10);