cond_subcontracting.hpp 18 KB

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