cond_subcontracting.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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& r
  138. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(BOOST_CONTRACT_MAX_ARGS)
  139. BOOST_CONTRACT_DETAIL_TVARIADIC_FPARAMS_Z(1,
  140. BOOST_CONTRACT_MAX_ARGS, Args, &, args)
  141. ) :
  142. cond_inv<VR, C>(from, obj)
  143. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  144. , r_(r)
  145. #endif
  146. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  147. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(BOOST_CONTRACT_MAX_ARGS)
  148. BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INIT_Z(1,
  149. BOOST_CONTRACT_MAX_ARGS, args_, args)
  150. #endif
  151. {
  152. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  153. if(v) {
  154. base_call_ = true;
  155. v_ = v; // Invariant: v_ never null if base_call_.
  156. BOOST_CONTRACT_DETAIL_DEBUG(v_);
  157. } else {
  158. base_call_ = false;
  159. if(!boost::mpl::empty<overridden_bases>::value) {
  160. v_ = new boost::contract::virtual_(
  161. boost::contract::virtual_::no_action);
  162. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  163. v_->result_ptr_ = &r_;
  164. v_->result_type_name_ = typeid(VR).name();
  165. v_->result_optional_ = is_optional<VR>::value;
  166. #endif
  167. } else v_ = 0;
  168. }
  169. #endif
  170. }
  171. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  172. virtual ~cond_subcontracting() BOOST_NOEXCEPT_IF(false) {
  173. if(!base_call_) delete v_;
  174. }
  175. #endif
  176. protected:
  177. #ifndef BOOST_CONTRACT_NO_OLDS
  178. void init_subcontracted_old() {
  179. // Old values of overloaded func on stack (so no `f` param here).
  180. exec_and(boost::contract::virtual_::push_old_init_copy);
  181. }
  182. #endif
  183. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  184. void check_subcontracted_entry_inv() {
  185. exec_and(boost::contract::virtual_::check_entry_inv,
  186. &cond_subcontracting::check_entry_inv);
  187. }
  188. #endif
  189. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  190. void check_subcontracted_pre() {
  191. exec_or(
  192. boost::contract::virtual_::check_pre,
  193. &cond_subcontracting::check_pre,
  194. &boost::contract::precondition_failure
  195. );
  196. }
  197. #endif
  198. #ifndef BOOST_CONTRACT_NO_OLDS
  199. void copy_subcontracted_old() {
  200. exec_and(boost::contract::virtual_::call_old_ftor,
  201. &cond_subcontracting::copy_virtual_old);
  202. }
  203. #endif
  204. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  205. void check_subcontracted_exit_inv() {
  206. exec_and(boost::contract::virtual_::check_exit_inv,
  207. &cond_subcontracting::check_exit_inv);
  208. }
  209. #endif
  210. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  211. void check_subcontracted_post() {
  212. exec_and(boost::contract::virtual_::check_post,
  213. &cond_subcontracting::check_virtual_post);
  214. }
  215. #endif
  216. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  217. void check_subcontracted_except() {
  218. exec_and(boost::contract::virtual_::check_except,
  219. &cond_subcontracting::check_virtual_except);
  220. }
  221. #endif
  222. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  223. bool base_call() const { return base_call_; }
  224. bool failed() const /* override */ {
  225. if(v_) return v_->failed_;
  226. else return cond_base::failed();
  227. }
  228. void failed(bool value) /* override */ {
  229. if(v_) v_->failed_ = value;
  230. else cond_base::failed(value);
  231. }
  232. #endif
  233. private:
  234. #ifndef BOOST_CONTRACT_NO_OLDS
  235. void copy_virtual_old() {
  236. boost::contract::virtual_::action_enum a;
  237. if(base_call_) {
  238. a = v_->action_;
  239. v_->action_ = boost::contract::virtual_::push_old_ftor_copy;
  240. }
  241. this->copy_old();
  242. if(base_call_) v_->action_ = a;
  243. }
  244. void pop_base_old() {
  245. if(base_call_) {
  246. boost::contract::virtual_::action_enum a = v_->action_;
  247. v_->action_ = boost::contract::virtual_::pop_old_ftor_copy;
  248. this->copy_old();
  249. v_->action_ = a;
  250. } // Else, do nothing (for base calls only).
  251. }
  252. #endif
  253. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  254. void check_virtual_post() {
  255. pop_base_old();
  256. typedef typename boost::remove_reference<typename
  257. optional_value_type<VR>::type>::type r_type;
  258. boost::optional<r_type const&> r; // No result copy in this code.
  259. if(!base_call_) r = optional_get(r_);
  260. else if(v_->result_optional_) {
  261. try {
  262. r = **boost::any_cast<boost::optional<r_type>*>(
  263. v_->result_ptr_);
  264. } catch(boost::bad_any_cast const&) {
  265. try { // Handle optional<...&>.
  266. r = **boost::any_cast<boost::optional<r_type&>*>(
  267. v_->result_ptr_);
  268. } catch(boost::bad_any_cast const&) {
  269. try {
  270. throw boost::contract::bad_virtual_result_cast(v_->
  271. result_type_name_, typeid(r_type).name());
  272. } catch(...) {
  273. this->fail(&boost::contract::postcondition_failure);
  274. }
  275. }
  276. }
  277. } else {
  278. try {
  279. r = *boost::any_cast<r_type*>(v_->result_ptr_);
  280. } catch(boost::bad_any_cast const&) {
  281. try {
  282. throw boost::contract::bad_virtual_result_cast(
  283. v_->result_type_name_, typeid(r_type).name());
  284. } catch(...) {
  285. this->fail(&boost::contract::postcondition_failure);
  286. }
  287. }
  288. }
  289. check_virtual_post_with_result<VR>(r);
  290. }
  291. template<typename R_, typename Result>
  292. typename boost::enable_if<is_optional<R_> >::type
  293. check_virtual_post_with_result(Result const& r) {
  294. this->check_post(r);
  295. }
  296. template<typename R_, typename Result>
  297. typename boost::disable_if<is_optional<R_> >::type
  298. check_virtual_post_with_result(Result const& r) {
  299. BOOST_CONTRACT_DETAIL_DEBUG(r);
  300. this->check_post(*r);
  301. }
  302. #endif
  303. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  304. void check_virtual_except() {
  305. pop_base_old();
  306. this->check_except();
  307. }
  308. #endif
  309. #if !defined(BOOST_CONTRACT_NO_INVARIANTS) || \
  310. !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  311. !defined(BOOST_CONTRACT_NO_EXCEPTS)
  312. void exec_and( // Execute action in short-circuit logic-and with bases.
  313. boost::contract::virtual_::action_enum a,
  314. void (cond_subcontracting::* f)() = 0
  315. ) {
  316. if(failed()) return;
  317. if(!base_call_ || v_->action_ == a) {
  318. if(!base_call_ && v_) {
  319. v_->action_ = a;
  320. boost::mpl::for_each<overridden_bases>(call_base(*this));
  321. }
  322. if(f) (this->*f)();
  323. if(base_call_) {
  324. throw cond_subcontracting_::signal_no_error();
  325. }
  326. }
  327. }
  328. #endif
  329. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  330. void exec_or( // Execute action in short-circuit logic-or with bases.
  331. boost::contract::virtual_::action_enum a,
  332. bool (cond_subcontracting::* f)(bool) = 0,
  333. void (*h)(boost::contract::from) = 0
  334. ) {
  335. if(failed()) return;
  336. if(!base_call_ || v_->action_ == a) {
  337. if(!base_call_ && v_) {
  338. v_->action_ = a;
  339. try {
  340. exec_or_bases<overridden_bases>();
  341. return; // A base checked with no error (done).
  342. } catch(...) {
  343. bool checked = f ? (this->*f)(
  344. /* throw_on_failure = */ false) : false;
  345. if(!checked) {
  346. try { throw; } // Report latest exception found.
  347. catch(...) { this->fail(h); }
  348. }
  349. return; // Checked and no exception (done).
  350. }
  351. }
  352. bool checked = f ?
  353. (this->*f)(/* throw_on_failure = */ base_call_) : false;
  354. if(base_call_) {
  355. if(!checked) {
  356. throw cond_subcontracting_::signal_not_checked();
  357. }
  358. throw cond_subcontracting_::signal_no_error();
  359. }
  360. }
  361. }
  362. template<typename Bases>
  363. typename boost::enable_if<boost::mpl::empty<Bases>, bool>::type
  364. exec_or_bases() { return false; }
  365. template<typename Bases>
  366. typename boost::disable_if<boost::mpl::empty<Bases>, bool>::type
  367. exec_or_bases() {
  368. if(boost::mpl::empty<Bases>::value) return false;
  369. try {
  370. call_base(*this)(typename boost::mpl::front<Bases>::type());
  371. } catch(cond_subcontracting_::signal_not_checked const&) {
  372. return exec_or_bases<
  373. typename boost::mpl::pop_front<Bases>::type>();
  374. } catch(...) {
  375. bool checked = false;
  376. try {
  377. checked = exec_or_bases<
  378. typename boost::mpl::pop_front<Bases>::type>();
  379. } catch(...) { checked = false; }
  380. if(!checked) throw;
  381. }
  382. return true;
  383. }
  384. #endif
  385. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  386. class call_base { // Copyable (as &).
  387. public:
  388. explicit call_base(cond_subcontracting& me) : me_(me) {}
  389. template<class B>
  390. void operator()(B*) {
  391. BOOST_CONTRACT_DETAIL_DEBUG(me_.object());
  392. BOOST_CONTRACT_DETAIL_DEBUG(me_.v_);
  393. BOOST_CONTRACT_DETAIL_DEBUG(me_.v_->action_ !=
  394. boost::contract::virtual_::no_action);
  395. try {
  396. call<B>(BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_OF(
  397. Args));
  398. } catch(cond_subcontracting_::signal_no_error const&) {
  399. // No error (do not throw).
  400. }
  401. }
  402. private:
  403. template<
  404. class B
  405. // Can't use TVARIADIC_COMMA here.
  406. BOOST_PP_COMMA_IF(BOOST_CONTRACT_DETAIL_TVARIADIC)
  407. BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_TPARAM(I)
  408. >
  409. void call(
  410. BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_INDEXES_FPARAM(I)) {
  411. O::template BOOST_CONTRACT_DETAIL_NAME1(call_base)<B>(
  412. me_.v_,
  413. me_.object()
  414. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(
  415. BOOST_CONTRACT_MAX_ARGS)
  416. BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_ELEMS_Z(1,
  417. BOOST_CONTRACT_MAX_ARGS, I, me_.args_)
  418. );
  419. }
  420. cond_subcontracting& me_;
  421. };
  422. #endif
  423. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  424. VR& r_;
  425. #endif
  426. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  427. boost::contract::virtual_* v_;
  428. bool base_call_;
  429. BOOST_CONTRACT_DETAIL_TVARIADIC_TUPLE_Z(1,
  430. BOOST_CONTRACT_MAX_ARGS, Args, &, args_)
  431. #endif
  432. };
  433. } } } // namespace
  434. #endif // #include guard