]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/contract/example/mitchell02/courier.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
8 #include <boost/contract.hpp>
16 double delivered_hour
;
20 std::string
const& _location
= "",
21 double _accepted_hour
= 0.0,
22 double _delivered_hour
= 0.0
24 weight_kg(_weight_kg
),
26 accepted_hour(_accepted_hour
),
27 delivered_hour(_delivered_hour
)
31 // Courier for package delivery.
33 #define BASES private boost::contract::constructor_precondition<courier>
36 friend class boost::contract::access
;
38 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
;
41 static void static_invariant() {
42 // Positive min. insurance.
43 BOOST_CONTRACT_ASSERT(min_insurance_usd
>= 0.0);
46 void invariant() const {
47 // Above min. insurance.
48 BOOST_CONTRACT_ASSERT(insurance_cover_usd() >= min_insurance_usd
);
52 static double min_insurance_usd
;
56 // Create courier with specified insurance value.
57 explicit courier(double _insurance_cover_usd
= min_insurance_usd
) :
58 boost::contract::constructor_precondition
<courier
>([&] {
59 // Positive insurance.
60 BOOST_CONTRACT_ASSERT(_insurance_cover_usd
>= 0.0);
62 insurance_cover_usd_(_insurance_cover_usd
)
65 boost::contract::check c
= boost::contract::constructor(this);
71 boost::contract::check c
= boost::contract::destructor(this);
76 // Return insurance cover.
77 double insurance_cover_usd() const {
79 boost::contract::check c
= boost::contract::public_function(this);
80 return insurance_cover_usd_
;
85 // Deliver package to destination.
87 package
& package_delivery
,
88 std::string
const& destination
,
89 boost::contract::virtual_
* v
= 0
91 boost::contract::check c
= boost::contract::public_function(v
, this)
93 // Within max weight of this delivery.
94 BOOST_CONTRACT_ASSERT(package_delivery
.weight_kg
< 5.0);
97 // Within max delivery type.
98 BOOST_CONTRACT_ASSERT(double(package_delivery
.delivered_hour
-
99 package_delivery
.accepted_hour
) <= 3.0);
100 // Delivered at destination.
101 BOOST_CONTRACT_ASSERT(package_delivery
.location
== destination
);
105 package_delivery
.location
= destination
;
106 // Delivery takes 2.5 hours.
107 package_delivery
.delivered_hour
= package_delivery
.accepted_hour
+ 2.5;
111 double insurance_cover_usd_
;
114 double courier::min_insurance_usd
= 10.0e+6;
116 // Different courier for package delivery.
117 class different_courier
118 #define BASES private boost::contract::constructor_precondition< \
119 different_courier>, public courier
122 friend class boost::contract::access
;
124 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
; // Subcontracting.
127 static void static_invariant() {
128 BOOST_CONTRACT_ASSERT( // Better insurance amount.
129 different_insurance_usd
>= courier::min_insurance_usd
);
132 void invariant() const {
133 // Above different insurance value.
134 BOOST_CONTRACT_ASSERT(insurance_cover_usd() >= different_insurance_usd
);
137 BOOST_CONTRACT_OVERRIDE(deliver
)
140 static double different_insurance_usd
;
144 // Create courier with specified insurance value.
145 explicit different_courier(
146 double insurance_cover_usd
= different_insurance_usd
) :
147 boost::contract::constructor_precondition
<different_courier
>([&] {
148 // Positive insurance value.
149 BOOST_CONTRACT_ASSERT(insurance_cover_usd
> 0.0);
151 courier(insurance_cover_usd
)
154 boost::contract::check c
= boost::contract::constructor(this);
158 virtual ~different_courier() {
160 boost::contract::check c
= boost::contract::destructor(this);
165 virtual void deliver(
166 package
& package_delivery
,
167 std::string
const& destination
,
168 boost::contract::virtual_
* v
= 0
170 boost::contract::check c
= boost::contract::public_function
<
172 >(v
, &different_courier::deliver
, this, package_delivery
, destination
)
174 // Package can weight more (weaker precondition).
175 BOOST_CONTRACT_ASSERT(package_delivery
.weight_kg
<= 8.0);
178 // Faster delivery (stronger postcondition).
179 BOOST_CONTRACT_ASSERT(double(package_delivery
.delivered_hour
-
180 package_delivery
.accepted_hour
) <= 2.0);
181 // Inherited "delivery at destination" postcondition.
185 package_delivery
.location
= destination
;
186 // Delivery takes 0.5 hours.
187 package_delivery
.delivered_hour
= package_delivery
.accepted_hour
+ 0.5;
191 double different_courier::different_insurance_usd
= 20.0e+6;
194 package
cups(3.6, "store");
196 c
.deliver(cups
, "home");
197 assert(cups
.location
== "home");
199 package
desk(7.2, "store");
200 different_courier dc
;
201 dc
.deliver(desk
, "office");
202 assert(desk
.location
== "office");