2 #ifndef BOOST_CONTRACT_DETAIL_COND_SUBCONTRACTING_HPP_
3 #define BOOST_CONTRACT_DETAIL_COND_SUBCONTRACTING_HPP_
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
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>
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>
41 #include <boost/preprocessor/punctuation/comma_if.hpp>
42 #include <boost/config.hpp>
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>
50 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
51 #include <boost/mpl/pop_front.hpp>
52 #include <boost/mpl/front.hpp>
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>
63 namespace boost { namespace contract { namespace detail {
65 namespace cond_subcontracting_ {
66 // Exception signals (must not inherit).
67 class signal_no_error {};
68 class signal_not_checked {};
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 {
79 typedef typename boost::mpl::fold<
80 typename boost::contract::access::base_types_of<Class>::
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.
89 typename O::template BOOST_CONTRACT_DETAIL_NAME1(
92 typename member_function_types<C, F>::
94 typename member_function_types<C, F>::
95 virtual_argument_types,
96 typename member_function_types<C, F>::
100 boost::mpl::push_back<
101 overridden_bases_of<boost::mpl::_2,
103 // Bases as * since for_each constructs them.
104 boost::add_pointer<boost::mpl::_2>
107 overridden_bases_of<boost::mpl::_2, boost::mpl::_1>
113 typedef typename boost::mpl::eval_if<
114 boost::contract::access::has_base_types<Class>,
117 boost::mpl::identity<Result> // Return result (stop recursion).
121 typedef typename boost::mpl::eval_if<boost::is_same<O, none>,
124 overridden_bases_of<C>
125 >::type overridden_bases;
127 #ifndef BOOST_CONTRACT_PERMISSIVE
128 BOOST_STATIC_ASSERT_MSG(
130 boost::is_same<O, none>,
131 boost::mpl::not_<boost::mpl::empty<overridden_bases> >
133 "subcontracting function specified as 'override' but does not "
134 "override any contracted member function"
138 typedef boost::mpl::vector<> overridden_bases;
142 explicit cond_subcontracting(
143 boost::contract::from from,
144 boost::contract::virtual_* v,
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)
151 cond_inv<VR, C>(from, obj)
152 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
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)
161 #ifndef BOOST_CONTRACT_NO_CONDITIONS
164 v_ = v; // Invariant: v_ never null if base_call_.
165 BOOST_CONTRACT_DETAIL_DEBUG(v_);
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;
181 #ifndef BOOST_CONTRACT_NO_CONDITIONS
182 virtual ~cond_subcontracting() BOOST_NOEXCEPT_IF(false) {
183 if(!base_call_) delete v_;
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);
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);
202 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
203 void check_subcontracted_pre() {
205 boost::contract::virtual_::check_pre,
206 &cond_subcontracting::check_pre,
207 &boost::contract::precondition_failure
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);
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);
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);
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);
240 #ifndef BOOST_CONTRACT_NO_CONDITIONS
241 bool base_call() const { return base_call_; }
243 bool failed() const /* override */ {
244 if(v_) return v_->failed_;
245 else return cond_base::failed();
248 void failed(bool value) /* override */ {
249 if(v_) v_->failed_ = value;
250 else cond_base::failed(value);
255 #ifndef BOOST_CONTRACT_NO_OLDS
256 void copy_virtual_old() {
257 boost::contract::virtual_::action_enum a;
260 v_->action_ = boost::contract::virtual_::push_old_ftor_copy;
263 if(base_call_) v_->action_ = a;
266 void pop_base_old() {
268 boost::contract::virtual_::action_enum a = v_->action_;
269 v_->action_ = boost::contract::virtual_::pop_old_ftor_copy;
272 } // Else, do nothing (for base calls only).
276 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
277 void check_virtual_post() {
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_) {
285 r = **boost::any_cast<boost::optional<r_type>*>(
287 } catch(boost::bad_any_cast const&) {
288 try { // Handle optional<...&>.
289 r = **boost::any_cast<boost::optional<r_type&>*>(
291 } catch(boost::bad_any_cast const&) {
293 throw boost::contract::bad_virtual_result_cast(v_->
294 result_type_name_, typeid(r_type).name());
296 this->fail(&boost::contract::postcondition_failure);
302 r = *boost::any_cast<r_type*>(v_->result_ptr_);
303 } catch(boost::bad_any_cast const&) {
305 throw boost::contract::bad_virtual_result_cast(
306 v_->result_type_name_, typeid(r_type).name());
308 this->fail(&boost::contract::postcondition_failure);
312 check_virtual_post_with_result<VR>(r);
315 template<typename R_, typename Result>
316 typename boost::enable_if<is_optional<R_> >::type
317 check_virtual_post_with_result(Result const& r) {
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);
329 #ifndef BOOST_CONTRACT_NO_EXCEPTS
330 void check_virtual_except() {
332 this->check_except();
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
344 if(!base_call_ || v_->action_ == a) {
345 if(!base_call_ && v_) {
347 boost::mpl::for_each<overridden_bases>(call_base(*this));
351 throw cond_subcontracting_::signal_no_error();
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
364 if(!base_call_ || v_->action_ == a) {
365 if(!base_call_ && v_) {
368 exec_or_bases<overridden_bases>();
369 return; // A base checked with no error (done).
371 bool checked = f ? (this->*f)(
372 /* throw_on_failure = */ false) : false;
374 try { throw; } // Report latest exception found.
375 catch(...) { this->fail(h); }
377 return; // Checked and no exception (done).
381 (this->*f)(/* throw_on_failure = */ base_call_) : false;
384 throw cond_subcontracting_::signal_not_checked();
386 throw cond_subcontracting_::signal_no_error();
391 template<typename Bases>
392 typename boost::enable_if<boost::mpl::empty<Bases>, bool>::type
393 exec_or_bases() { return false; }
395 template<typename Bases>
396 typename boost::disable_if<boost::mpl::empty<Bases>, bool>::type
398 if(boost::mpl::empty<Bases>::value) return false;
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>();
405 bool checked = false;
407 checked = exec_or_bases<
408 typename boost::mpl::pop_front<Bases>::type>();
409 } catch(...) { checked = false; }
416 #ifndef BOOST_CONTRACT_NO_CONDITIONS
417 class call_base { // Copyable (as &).
419 explicit call_base(cond_subcontracting& me) : me_(me) {}
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);
428 call<B>(BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_OF(
430 } catch(cond_subcontracting_::signal_no_error const&) {
431 // No error (do not throw).
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)
443 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_FPARAM(I)) {
444 O::template BOOST_CONTRACT_DETAIL_NAME1(call_base)<B>(
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_)
454 cond_subcontracting& me_;
458 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
461 #ifndef BOOST_CONTRACT_NO_CONDITIONS
462 boost::contract::virtual_* v_;
464 BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_Z(1,
465 BOOST_CONTRACT_MAX_ARGS, Args, &, args_)
471 #endif // #include guard