]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/contract/example/features/optional_result_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>
8 #include <boost/optional.hpp>
15 virtual T
& at(unsigned index
, boost::contract::virtual_
* v
= 0) = 0;
17 // Could program class invariants and contracts for following too.
18 virtual T
const& operator[](unsigned index
) const = 0;
19 virtual unsigned size() const = 0;
22 //[optional_result_virtual
24 T
& accessible
<T
>::at(unsigned index
, boost::contract::virtual_
* v
) {
25 boost::optional
<T
&> result
;
26 // Pass `result` right after `v`...
27 boost::contract::check c
= boost::contract::public_function(v
, result
, this)
29 BOOST_CONTRACT_ASSERT(index
< size());
31 // ...plus postconditions take `result` as a parameter (not capture).
32 .postcondition([&] (boost::optional
<T
const&> const& result
) {
33 BOOST_CONTRACT_ASSERT(*result
== operator[](index
));
44 #define BASES public accessible<T>
48 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
;
51 T
& at(unsigned index
, boost::contract::virtual_
* v
= 0) /* override */ {
52 boost::optional
<T
&> result
;
53 // Pass `result` right after `v`...
54 boost::contract::check c
= boost::contract::public_function
<
55 override_at
>(v
, result
, &vector::at
, this, index
)
56 // ...plus postconditions take `result` as parameter (not capture).
57 .postcondition([&] (boost::optional
<T
const&> const& result
) {
58 if(index
== 0) BOOST_CONTRACT_ASSERT(*result
== front());
62 return *(result
= vect_
[index
]);
65 // Could program class invariants and contracts for following too.
66 T
const& operator[](unsigned index
) const { return vect_
[index
]; }
67 unsigned size() const { return vect_
.size(); }
68 T
const& front() const { return vect_
.front(); }
69 void push_back(T
const& value
) { vect_
.push_back(value
); }
71 BOOST_CONTRACT_OVERRIDE(at
)
85 assert(v
.at(1) == -456);