]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/contract/detail/condition/cond_subcontracting.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / contract / detail / condition / cond_subcontracting.hpp
1
2 #ifndef BOOST_CONTRACT_DETAIL_COND_SUBCONTRACTING_HPP_
3 #define BOOST_CONTRACT_DETAIL_COND_SUBCONTRACTING_HPP_
4
5 // Copyright (C) 2008-2018 Lorenzo Caminiti
6 // Distributed under the Boost Software License, Version 1.0 (see accompanying
7 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
8 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
9
10 #include <boost/contract/core/config.hpp>
11 #if !defined(BOOST_CONTRACT_NO_PRECONDITIONS) || \
12 !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
13 !defined(BOOST_CONTRACT_NO_EXCEPTS)
14 #include <boost/contract/core/exception.hpp>
15 #endif
16 #include <boost/contract/detail/condition/cond_inv.hpp>
17 #include <boost/contract/detail/decl.hpp>
18 #include <boost/contract/detail/tvariadic.hpp>
19 #ifndef BOOST_CONTRACT_NO_CONDITIONS
20 #include <boost/contract/core/virtual.hpp>
21 #include <boost/contract/core/access.hpp>
22 #include <boost/contract/detail/type_traits/optional.hpp>
23 #include <boost/contract/detail/type_traits/member_function_types.hpp>
24 #include <boost/contract/detail/debug.hpp>
25 #include <boost/contract/detail/none.hpp>
26 #include <boost/contract/detail/name.hpp>
27 #include <boost/type_traits/add_pointer.hpp>
28 #include <boost/mpl/fold.hpp>
29 #include <boost/mpl/contains.hpp>
30 #include <boost/mpl/empty.hpp>
31 #include <boost/mpl/push_back.hpp>
32 #include <boost/mpl/eval_if.hpp>
33 #include <boost/mpl/identity.hpp>
34 #include <boost/mpl/placeholders.hpp>
35 #ifndef BOOST_CONTRACT_PERMISSIVE
36 #include <boost/type_traits/is_same.hpp>
37 #include <boost/mpl/or.hpp>
38 #include <boost/mpl/not.hpp>
39 #include <boost/static_assert.hpp>
40 #endif
41 #include <boost/preprocessor/punctuation/comma_if.hpp>
42 #include <boost/config.hpp>
43 #endif
44 #include <boost/mpl/vector.hpp>
45 #if !defined(BOOST_CONTRACT_NO_INVARIANTS) || \
46 !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
47 !defined(BOOST_CONTRACT_NO_EXCEPTS)
48 #include <boost/mpl/for_each.hpp>
49 #endif
50 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
51 #include <boost/mpl/pop_front.hpp>
52 #include <boost/mpl/front.hpp>
53 #endif
54 #if !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
55 !defined(BOSOT_CONTRACT_NO_EXCEPTS)
56 #include <boost/any.hpp>
57 #include <boost/optional.hpp>
58 #include <boost/type_traits/remove_reference.hpp>
59 #include <boost/utility/enable_if.hpp>
60 #include <typeinfo>
61 #endif
62
63 namespace boost { namespace contract { namespace detail {
64
65 namespace cond_subcontracting_ {
66 // Exception signals (must not inherit).
67 class signal_no_error {};
68 class signal_not_checked {};
69 }
70
71 // O, VR, F, and Args-i can be none types (but C cannot).
72 BOOST_CONTRACT_DETAIL_DECL_DETAIL_COND_SUBCONTRACTING_Z(1,
73 /* is_friend = */ 0, O, VR, F, C, Args) : public cond_inv<VR, C>
74 { // Non-copyable base.
75 #ifndef BOOST_CONTRACT_NO_CONDITIONS
76 template<class Class, typename Result = boost::mpl::vector<> >
77 class overridden_bases_of {
78 struct search_bases {
79 typedef typename boost::mpl::fold<
80 typename boost::contract::access::base_types_of<Class>::
81 type,
82 Result,
83 // Fold: _1 = result, _2 = current base from base_types.
84 boost::mpl::eval_if<boost::mpl::contains<boost::mpl::_1,
85 boost::add_pointer<boost::mpl::_2> >,
86 boost::mpl::_1 // Base in result, do not add it again.
87 ,
88 boost::mpl::eval_if<
89 typename O::template BOOST_CONTRACT_DETAIL_NAME1(
90 has_member_function)<
91 boost::mpl::_2,
92 typename member_function_types<C, F>::
93 result_type,
94 typename member_function_types<C, F>::
95 virtual_argument_types,
96 typename member_function_types<C, F>::
97 property_tag
98 >
99 ,
100 boost::mpl::push_back<
101 overridden_bases_of<boost::mpl::_2,
102 boost::mpl::_1>,
103 // Bases as * since for_each constructs them.
104 boost::add_pointer<boost::mpl::_2>
105 >
106 ,
107 overridden_bases_of<boost::mpl::_2, boost::mpl::_1>
108 >
109 >
110 >::type type;
111 };
112 public:
113 typedef typename boost::mpl::eval_if<
114 boost::contract::access::has_base_types<Class>,
115 search_bases
116 ,
117 boost::mpl::identity<Result> // Return result (stop recursion).
118 >::type type;
119 };
120
121 typedef typename boost::mpl::eval_if<boost::is_same<O, none>,
122 boost::mpl::vector<>
123 ,
124 overridden_bases_of<C>
125 >::type overridden_bases;
126
127 #ifndef BOOST_CONTRACT_PERMISSIVE
128 BOOST_STATIC_ASSERT_MSG(
129 (boost::mpl::or_<
130 boost::is_same<O, none>,
131 boost::mpl::not_<boost::mpl::empty<overridden_bases> >
132 >::value),
133 "subcontracting function specified as 'override' but does not "
134 "override any contracted member function"
135 );
136 #endif
137 #else
138 typedef boost::mpl::vector<> overridden_bases;
139 #endif
140
141 public:
142 explicit cond_subcontracting(
143 boost::contract::from from,
144 boost::contract::virtual_* v,
145 C* obj,
146 VR& r
147 BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(BOOST_CONTRACT_MAX_ARGS)
148 BOOST_CONTRACT_DETAIL_TVARIADIC_FPARAMS_Z(1,
149 BOOST_CONTRACT_MAX_ARGS, Args, &, args)
150 ) :
151 cond_inv<VR, C>(from, obj)
152 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
153 , r_(r)
154 #endif
155 #ifndef BOOST_CONTRACT_NO_CONDITIONS
156 BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(BOOST_CONTRACT_MAX_ARGS)
157 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INIT_Z(1,
158 BOOST_CONTRACT_MAX_ARGS, args_, args)
159 #endif
160 {
161 #ifndef BOOST_CONTRACT_NO_CONDITIONS
162 if(v) {
163 base_call_ = true;
164 v_ = v; // Invariant: v_ never null if base_call_.
165 BOOST_CONTRACT_DETAIL_DEBUG(v_);
166 } else {
167 base_call_ = false;
168 if(!boost::mpl::empty<overridden_bases>::value) {
169 v_ = new boost::contract::virtual_(
170 boost::contract::virtual_::no_action);
171 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
172 v_->result_ptr_ = &r_;
173 v_->result_type_name_ = typeid(VR).name();
174 v_->result_optional_ = is_optional<VR>::value;
175 #endif
176 } else v_ = 0;
177 }
178 #endif
179 }
180
181 #ifndef BOOST_CONTRACT_NO_CONDITIONS
182 virtual ~cond_subcontracting() BOOST_NOEXCEPT_IF(false) {
183 if(!base_call_) delete v_;
184 }
185 #endif
186
187 protected:
188 #ifndef BOOST_CONTRACT_NO_OLDS
189 void init_subcontracted_old() {
190 // Old values of overloaded func on stack (so no `f` param here).
191 exec_and(boost::contract::virtual_::push_old_init_copy);
192 }
193 #endif
194
195 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
196 void check_subcontracted_entry_inv() {
197 exec_and(boost::contract::virtual_::check_entry_inv,
198 &cond_subcontracting::check_entry_inv);
199 }
200 #endif
201
202 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
203 void check_subcontracted_pre() {
204 exec_or(
205 boost::contract::virtual_::check_pre,
206 &cond_subcontracting::check_pre,
207 &boost::contract::precondition_failure
208 );
209 }
210 #endif
211
212 #ifndef BOOST_CONTRACT_NO_OLDS
213 void copy_subcontracted_old() {
214 exec_and(boost::contract::virtual_::call_old_ftor,
215 &cond_subcontracting::copy_virtual_old);
216 }
217 #endif
218
219 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
220 void check_subcontracted_exit_inv() {
221 exec_and(boost::contract::virtual_::check_exit_inv,
222 &cond_subcontracting::check_exit_inv);
223 }
224 #endif
225
226 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
227 void check_subcontracted_post() {
228 exec_and(boost::contract::virtual_::check_post,
229 &cond_subcontracting::check_virtual_post);
230 }
231 #endif
232
233 #ifndef BOOST_CONTRACT_NO_EXCEPTS
234 void check_subcontracted_except() {
235 exec_and(boost::contract::virtual_::check_except,
236 &cond_subcontracting::check_virtual_except);
237 }
238 #endif
239
240 #ifndef BOOST_CONTRACT_NO_CONDITIONS
241 bool base_call() const { return base_call_; }
242
243 bool failed() const /* override */ {
244 if(v_) return v_->failed_;
245 else return cond_base::failed();
246 }
247
248 void failed(bool value) /* override */ {
249 if(v_) v_->failed_ = value;
250 else cond_base::failed(value);
251 }
252 #endif
253
254 private:
255 #ifndef BOOST_CONTRACT_NO_OLDS
256 void copy_virtual_old() {
257 boost::contract::virtual_::action_enum a;
258 if(base_call_) {
259 a = v_->action_;
260 v_->action_ = boost::contract::virtual_::push_old_ftor_copy;
261 }
262 this->copy_old();
263 if(base_call_) v_->action_ = a;
264 }
265
266 void pop_base_old() {
267 if(base_call_) {
268 boost::contract::virtual_::action_enum a = v_->action_;
269 v_->action_ = boost::contract::virtual_::pop_old_ftor_copy;
270 this->copy_old();
271 v_->action_ = a;
272 } // Else, do nothing (for base calls only).
273 }
274 #endif
275
276 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
277 void check_virtual_post() {
278 pop_base_old();
279 typedef typename boost::remove_reference<typename
280 optional_value_type<VR>::type>::type r_type;
281 boost::optional<r_type const&> r; // No result copy in this code.
282 if(!base_call_) r = optional_get(r_);
283 else if(v_->result_optional_) {
284 try {
285 r = **boost::any_cast<boost::optional<r_type>*>(
286 v_->result_ptr_);
287 } catch(boost::bad_any_cast const&) {
288 try { // Handle optional<...&>.
289 r = **boost::any_cast<boost::optional<r_type&>*>(
290 v_->result_ptr_);
291 } catch(boost::bad_any_cast const&) {
292 try {
293 throw boost::contract::bad_virtual_result_cast(v_->
294 result_type_name_, typeid(r_type).name());
295 } catch(...) {
296 this->fail(&boost::contract::postcondition_failure);
297 }
298 }
299 }
300 } else {
301 try {
302 r = *boost::any_cast<r_type*>(v_->result_ptr_);
303 } catch(boost::bad_any_cast const&) {
304 try {
305 throw boost::contract::bad_virtual_result_cast(
306 v_->result_type_name_, typeid(r_type).name());
307 } catch(...) {
308 this->fail(&boost::contract::postcondition_failure);
309 }
310 }
311 }
312 check_virtual_post_with_result<VR>(r);
313 }
314
315 template<typename R_, typename Result>
316 typename boost::enable_if<is_optional<R_> >::type
317 check_virtual_post_with_result(Result const& r) {
318 this->check_post(r);
319 }
320
321 template<typename R_, typename Result>
322 typename boost::disable_if<is_optional<R_> >::type
323 check_virtual_post_with_result(Result const& r) {
324 BOOST_CONTRACT_DETAIL_DEBUG(r);
325 this->check_post(*r);
326 }
327 #endif
328
329 #ifndef BOOST_CONTRACT_NO_EXCEPTS
330 void check_virtual_except() {
331 pop_base_old();
332 this->check_except();
333 }
334 #endif
335
336 #if !defined(BOOST_CONTRACT_NO_INVARIANTS) || \
337 !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
338 !defined(BOOST_CONTRACT_NO_EXCEPTS)
339 void exec_and( // Execute action in short-circuit logic-and with bases.
340 boost::contract::virtual_::action_enum a,
341 void (cond_subcontracting::* f)() = 0
342 ) {
343 if(failed()) return;
344 if(!base_call_ || v_->action_ == a) {
345 if(!base_call_ && v_) {
346 v_->action_ = a;
347 boost::mpl::for_each<overridden_bases>(call_base(*this));
348 }
349 if(f) (this->*f)();
350 if(base_call_) {
351 throw cond_subcontracting_::signal_no_error();
352 }
353 }
354 }
355 #endif
356
357 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
358 void exec_or( // Execute action in short-circuit logic-or with bases.
359 boost::contract::virtual_::action_enum a,
360 bool (cond_subcontracting::* f)(bool) = 0,
361 void (*h)(boost::contract::from) = 0
362 ) {
363 if(failed()) return;
364 if(!base_call_ || v_->action_ == a) {
365 if(!base_call_ && v_) {
366 v_->action_ = a;
367 try {
368 exec_or_bases<overridden_bases>();
369 return; // A base checked with no error (done).
370 } catch(...) {
371 bool checked = f ? (this->*f)(
372 /* throw_on_failure = */ false) : false;
373 if(!checked) {
374 try { throw; } // Report latest exception found.
375 catch(...) { this->fail(h); }
376 }
377 return; // Checked and no exception (done).
378 }
379 }
380 bool checked = f ?
381 (this->*f)(/* throw_on_failure = */ base_call_) : false;
382 if(base_call_) {
383 if(!checked) {
384 throw cond_subcontracting_::signal_not_checked();
385 }
386 throw cond_subcontracting_::signal_no_error();
387 }
388 }
389 }
390
391 template<typename Bases>
392 typename boost::enable_if<boost::mpl::empty<Bases>, bool>::type
393 exec_or_bases() { return false; }
394
395 template<typename Bases>
396 typename boost::disable_if<boost::mpl::empty<Bases>, bool>::type
397 exec_or_bases() {
398 if(boost::mpl::empty<Bases>::value) return false;
399 try {
400 call_base(*this)(typename boost::mpl::front<Bases>::type());
401 } catch(cond_subcontracting_::signal_not_checked const&) {
402 return exec_or_bases<
403 typename boost::mpl::pop_front<Bases>::type>();
404 } catch(...) {
405 bool checked = false;
406 try {
407 checked = exec_or_bases<
408 typename boost::mpl::pop_front<Bases>::type>();
409 } catch(...) { checked = false; }
410 if(!checked) throw;
411 }
412 return true;
413 }
414 #endif
415
416 #ifndef BOOST_CONTRACT_NO_CONDITIONS
417 class call_base { // Copyable (as &).
418 public:
419 explicit call_base(cond_subcontracting& me) : me_(me) {}
420
421 template<class B>
422 void operator()(B*) {
423 BOOST_CONTRACT_DETAIL_DEBUG(me_.object());
424 BOOST_CONTRACT_DETAIL_DEBUG(me_.v_);
425 BOOST_CONTRACT_DETAIL_DEBUG(me_.v_->action_ !=
426 boost::contract::virtual_::no_action);
427 try {
428 call<B>(BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_OF(
429 Args));
430 } catch(cond_subcontracting_::signal_no_error const&) {
431 // No error (do not throw).
432 }
433 }
434
435 private:
436 template<
437 class B
438 // Can't use TVARIADIC_COMMA here.
439 BOOST_PP_COMMA_IF(BOOST_CONTRACT_DETAIL_TVARIADIC)
440 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_TPARAM(I)
441 >
442 void call(
443 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_FPARAM(I)) {
444 O::template BOOST_CONTRACT_DETAIL_NAME1(call_base)<B>(
445 me_.v_,
446 me_.object()
447 BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(
448 BOOST_CONTRACT_MAX_ARGS)
449 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_ELEMS_Z(1,
450 BOOST_CONTRACT_MAX_ARGS, I, me_.args_)
451 );
452 }
453
454 cond_subcontracting& me_;
455 };
456 #endif
457
458 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
459 VR& r_;
460 #endif
461 #ifndef BOOST_CONTRACT_NO_CONDITIONS
462 boost::contract::virtual_* v_;
463 bool base_call_;
464 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_Z(1,
465 BOOST_CONTRACT_MAX_ARGS, Args, &, args_)
466 #endif
467 };
468
469 } } } // namespace
470
471 #endif // #include guard
472