public_function.hpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. #ifndef BOOST_CONTRACT_PUBLIC_FUNCTION_HPP_
  2. #define BOOST_CONTRACT_PUBLIC_FUNCTION_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. /** @file
  8. Program contracts for public functions (including subcontracting).
  9. The different overloads handle public functions that are static, virtual void,
  10. virtual non-void, overriding void, and overriding non-void.
  11. */
  12. #include <boost/contract/core/config.hpp>
  13. #include <boost/contract/core/specify.hpp>
  14. #include <boost/contract/core/access.hpp>
  15. #include <boost/contract/core/virtual.hpp>
  16. /** @cond */
  17. // Needed within macro expansions below instead of defined(...) (PRIVATE macro).
  18. #if !defined(BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS) || \
  19. defined(BOOST_CONTRACT_STATIC_LINK)
  20. #define BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_ 1
  21. #else
  22. #define BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_ 0
  23. #endif
  24. /** @endcond */
  25. #include <boost/contract/detail/decl.hpp>
  26. #include <boost/contract/detail/tvariadic.hpp>
  27. #if BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_
  28. #include <boost/contract/detail/operation/static_public_function.hpp>
  29. #include <boost/contract/detail/operation/public_function.hpp>
  30. #include <boost/contract/detail/type_traits/optional.hpp>
  31. #include <boost/contract/detail/none.hpp>
  32. #include <boost/function_types/result_type.hpp>
  33. #include <boost/function_types/function_arity.hpp>
  34. #include <boost/optional.hpp>
  35. #include <boost/type_traits/remove_reference.hpp>
  36. #include <boost/type_traits/is_same.hpp>
  37. #include <boost/static_assert.hpp>
  38. #include <boost/preprocessor/tuple/eat.hpp>
  39. #endif
  40. #if !BOOST_CONTRACT_DETAIL_TVARIADIC
  41. #include <boost/preprocessor/repetition/repeat.hpp>
  42. #include <boost/preprocessor/arithmetic/sub.hpp>
  43. #include <boost/preprocessor/arithmetic/inc.hpp>
  44. #endif
  45. #include <boost/preprocessor/control/expr_iif.hpp>
  46. #include <boost/preprocessor/control/iif.hpp>
  47. #include <boost/preprocessor/facilities/empty.hpp>
  48. #include <boost/preprocessor/punctuation/comma_if.hpp>
  49. namespace boost { namespace contract {
  50. // NOTE: Override and (optionally) VirtualResult allowed only when v is present
  51. // because:
  52. // * An overriding func must override a base func declared virtual so with
  53. // v extra param, thus the overriding func must also always have v (i.e.,
  54. // Override might be present only if v is also present). However, the first
  55. // appearing virtual func (e.g., in root class) will not override any
  56. // previously declared virtual func so does not need Override (i.e., Override
  57. // always optional).
  58. // Furthermore, F needs to be specified only together with Override.
  59. // * VirtualResult is only used for virtual functions (i.e., VirtualResult might
  60. // be present only if v is also present).
  61. // However, VirtualResult is never specified, not even for virtual functions,
  62. // when the return type is void (i.e., VirtualResult always optional).
  63. /**
  64. Program contracts for static public functions.
  65. This is used to specify preconditions, postconditions, exception guarantees, old
  66. value copies at body, and check static class invariants for static public
  67. functions:
  68. @code
  69. class u {
  70. friend class boost::contract::access;
  71. static void static_invariant() { // Optional (as for non-static).
  72. BOOST_CONTRACT_ASSERT(...);
  73. ...
  74. }
  75. public:
  76. static void f(...) {
  77. boost::contract::old_ptr<old_type> old_var;
  78. boost::contract::check c = boost::contract::public_function<u>()
  79. .precondition([&] { // Optional.
  80. BOOST_CONTRACT_ASSERT(...);
  81. ...
  82. })
  83. .old([&] { // Optional.
  84. old_var = BOOST_CONTRACT_OLDOF(old_expr);
  85. ...
  86. })
  87. .postcondition([&] { // Optional.
  88. BOOST_CONTRACT_ASSERT(...);
  89. ...
  90. })
  91. .except([&] { // Optional.
  92. BOOST_CONTRACT_ASSERT(...);
  93. ...
  94. })
  95. ;
  96. ... // Function body.
  97. }
  98. ...
  99. };
  100. @endcode
  101. For optimization, this can be omitted for static public functions that do not
  102. have preconditions, postconditions and exception guarantees, within classes that
  103. have no static invariants.
  104. @see @RefSect{tutorial.static_public_functions, Static Public Functions}
  105. @tparam Class The type of the class containing the static public function
  106. declaring the contract.
  107. This template parameter must be explicitly specified for static
  108. public functions (because they have no object @c this so there
  109. is no function argument from which this type template parameter
  110. can be automatically deduced by C++).
  111. @return The result of this function must be assigned to a variable of type
  112. @RefClass{boost::contract::check} declared explicitly (i.e., without
  113. using C++11 @c auto declarations) and locally just before the code of
  114. the static public function body (otherwise this library will generate a
  115. run-time error, see @RefMacro{BOOST_CONTRACT_ON_MISSING_CHECK_DECL}).
  116. */
  117. template<class Class>
  118. specify_precondition_old_postcondition_except<> public_function() {
  119. #if BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_
  120. return specify_precondition_old_postcondition_except<>(
  121. new boost::contract::detail::static_public_function<Class>());
  122. #else
  123. return specify_precondition_old_postcondition_except<>();
  124. #endif
  125. }
  126. /**
  127. Program contracts for public functions that are not static, not virtual, and do
  128. not not override.
  129. This is used to specify preconditions, postconditions, exception guarantees, old
  130. value copies at body, and check class invariants for public functions that are
  131. not static, not virtual, and do not override:
  132. @code
  133. class u {
  134. friend class boost::contract::access;
  135. void invariant() const { // Optional (as for static and volatile).
  136. BOOST_CONTRACT_ASSERT(...);
  137. ...
  138. }
  139. public:
  140. void f(...) {
  141. boost::contract::old_ptr<old_type> old_var;
  142. boost::contract::check c = boost::contract::public_function(this)
  143. .precondition([&] { // Optional.
  144. BOOST_CONTRACT_ASSERT(...);
  145. ...
  146. })
  147. .old([&] { // Optional.
  148. old_var = BOOST_CONTRACT_OLDOF(old_expr);
  149. ...
  150. })
  151. .postcondition([&] { // Optional.
  152. BOOST_CONTRACT_ASSERT(...);
  153. ...
  154. })
  155. .except([&] { // Optional.
  156. BOOST_CONTRACT_ASSERT(...);
  157. ...
  158. })
  159. ;
  160. ... // Function body.
  161. }
  162. ...
  163. };
  164. @endcode
  165. For optimization, this can be omitted for public functions that do not have
  166. preconditions, postconditions and exception guarantees, within classes that have
  167. no invariants.
  168. @see @RefSect{tutorial.public_functions, Public Functions}
  169. @param obj The object @c this from the scope of the enclosing public function
  170. declaring the contract.
  171. This object might be mutable, @c const, @c volatile, or
  172. <c>const volatile</c> depending on the cv-qualifier of the enclosing
  173. function (volatile public functions will check volatile class
  174. invariants, see
  175. @RefSect{extras.volatile_public_functions,
  176. Volatile Public Functions}).
  177. @tparam Class The type of the class containing the public function declaring
  178. the contract.
  179. (Usually this template parameter is automatically deduced by C++
  180. and it does not need to be explicitly specified by programmers.)
  181. @return The result of this function must be assigned to a variable of type
  182. @RefClass{boost::contract::check} declared explicitly (i.e., without
  183. using C++11 @c auto declarations) and locally just before the code of
  184. the public function body (otherwise this library will generate a
  185. run-time error, see @RefMacro{BOOST_CONTRACT_ON_MISSING_CHECK_DECL}).
  186. */
  187. template<class Class>
  188. specify_precondition_old_postcondition_except<> public_function(Class* obj) {
  189. #if BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_
  190. return specify_precondition_old_postcondition_except<>(
  191. new boost::contract::detail::public_function<
  192. boost::contract::detail::none,
  193. boost::contract::detail::none,
  194. boost::contract::detail::none,
  195. Class
  196. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA(
  197. BOOST_CONTRACT_MAX_ARGS)
  198. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(1,
  199. BOOST_CONTRACT_MAX_ARGS,
  200. boost::contract::detail::none
  201. )
  202. >(
  203. static_cast<boost::contract::virtual_*>(0),
  204. obj,
  205. boost::contract::detail::none::value()
  206. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA(
  207. BOOST_CONTRACT_MAX_ARGS)
  208. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(1,
  209. BOOST_CONTRACT_MAX_ARGS,
  210. boost::contract::detail::none::value()
  211. )
  212. )
  213. );
  214. #else
  215. return specify_precondition_old_postcondition_except<>();
  216. #endif
  217. }
  218. /** @cond */
  219. // For non-static, virtual, and non-overriding public functions (PRIVATE macro).
  220. #define BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_NO_OVERRIDE_( \
  221. has_virtual_result) \
  222. template< \
  223. BOOST_PP_EXPR_IIF(has_virtual_result, typename VirtualResult) \
  224. BOOST_PP_COMMA_IF(has_virtual_result) \
  225. class Class \
  226. > \
  227. specify_precondition_old_postcondition_except< \
  228. BOOST_PP_EXPR_IIF(has_virtual_result, VirtualResult)> \
  229. public_function( \
  230. virtual_* v \
  231. BOOST_PP_COMMA_IF(has_virtual_result) \
  232. BOOST_PP_EXPR_IIF(has_virtual_result, VirtualResult& r) \
  233. , Class* obj \
  234. ) { \
  235. BOOST_PP_IIF(BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_, \
  236. /* no F... so cannot enforce contracted F returns VirtualResult */ \
  237. return (specify_precondition_old_postcondition_except< \
  238. BOOST_PP_EXPR_IIF(has_virtual_result, VirtualResult)>( \
  239. new boost::contract::detail::public_function< \
  240. boost::contract::detail::none, \
  241. BOOST_PP_IIF(has_virtual_result, \
  242. VirtualResult \
  243. , \
  244. boost::contract::detail::none \
  245. ), \
  246. boost::contract::detail::none, \
  247. Class \
  248. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA( \
  249. BOOST_CONTRACT_MAX_ARGS) \
  250. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(1, \
  251. BOOST_CONTRACT_MAX_ARGS, \
  252. boost::contract::detail::none \
  253. ) \
  254. >( \
  255. v, \
  256. obj, \
  257. BOOST_PP_IIF(has_virtual_result, \
  258. r \
  259. , \
  260. boost::contract::detail::none::value() \
  261. ) \
  262. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA( \
  263. BOOST_CONTRACT_MAX_ARGS) \
  264. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(1, \
  265. BOOST_CONTRACT_MAX_ARGS, \
  266. boost::contract::detail::none::value() \
  267. ) \
  268. ) \
  269. )); \
  270. , \
  271. return specify_precondition_old_postcondition_except< \
  272. BOOST_PP_EXPR_IIF(has_virtual_result, VirtualResult)>(); \
  273. ) \
  274. }
  275. /** @endcond */
  276. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  277. /**
  278. Program contracts for void virtual public functions that do not override.
  279. This is used to specify preconditions, postconditions, exception guarantees,
  280. old value copies at body, and check class invariants for public functions
  281. that are virtual, do not override, and return @c void:
  282. @code
  283. class u {
  284. friend class boost::contract::access;
  285. void invariant() const { // Optional (as for static and volatile).
  286. BOOST_CONTRACT_ASSERT(...);
  287. ...
  288. }
  289. public:
  290. void f(..., boost::contract::virtual_* v = 0) {
  291. boost::contract::old_ptr<old_type> old_var;
  292. boost::contract::check c = boost::contract::public_function(v, this)
  293. .precondition([&] { // Optional.
  294. BOOST_CONTRACT_ASSERT(...);
  295. ...
  296. })
  297. .old([&] { // Optional.
  298. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  299. ...
  300. })
  301. .postcondition([&] { // Optional.
  302. BOOST_CONTRACT_ASSERT(...);
  303. ...
  304. })
  305. .except([&] { // Optional.
  306. BOOST_CONTRACT_ASSERT(...);
  307. ...
  308. })
  309. ;
  310. ... // Function body.
  311. }
  312. ...
  313. };
  314. @endcode
  315. A virtual public function should always call
  316. @RefFunc{boost::contract::public_function} otherwise this library will not
  317. be able to correctly use it for subcontracting.
  318. @see @RefSect{tutorial.virtual_public_functions, Virtual Public Functions}
  319. @param v The trailing parameter of type
  320. @RefClass{boost::contract::virtual_}<c>*</c> and default value
  321. @c 0 from the enclosing virtual public function.
  322. @param obj The object @c this from the scope of the enclosing virtual
  323. public function declaring the contract.
  324. This object might be mutable, @c const, @c volatile, or
  325. <c>const volatile</c> depending on the cv-qualifier of the
  326. enclosing function (volatile public functions will check
  327. volatile class invariants, see
  328. @RefSect{extras.volatile_public_functions,
  329. Volatile Public Functions}).
  330. @tparam Class The type of the class containing the virtual public function
  331. declaring the contract.
  332. (Usually this template parameter is automatically deduced by
  333. C++ and it does not need to be explicitly specified by
  334. programmers.)
  335. @return The result of this function must be assigned to a variable of type
  336. @RefClass{boost::contract::check} declared explicitly (i.e., without
  337. using C++11 @c auto declarations) and locally just before the code
  338. of the public function body (otherwise this library will generate a
  339. run-time error, see
  340. @RefMacro{BOOST_CONTRACT_ON_MISSING_CHECK_DECL}).
  341. */
  342. template<class Class>
  343. specify_precondition_old_postcondition_except<> public_function(
  344. virtual_* v, Class* obj);
  345. /**
  346. Program contracts for non-void virtual public functions that do not
  347. override.
  348. This is used to specify preconditions, postconditions, exception guarantees,
  349. old value copies at body, and check class invariants for public functions
  350. that are virtual, do not override, and do not return @c void:
  351. @code
  352. class u {
  353. friend class boost::contract::access;
  354. void invariant() const { // Optional (as for static and volatile).
  355. BOOST_CONTRACT_ASSERT(...);
  356. ...
  357. }
  358. public:
  359. t f(..., boost::contract::virtual_* v = 0) {
  360. t result;
  361. boost::contract::old_ptr<old_type> old_var;
  362. boost::contract::check c = boost::contract::public_function(
  363. v, result, this)
  364. .precondition([&] { // Optional.
  365. BOOST_CONTRACT_ASSERT(...);
  366. ...
  367. })
  368. .old([&] { // Optional.
  369. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  370. ...
  371. })
  372. .postcondition([&] (t const& result) { // Optional.
  373. BOOST_CONTRACT_ASSERT(...);
  374. ...
  375. })
  376. .except([&] { // Optional.
  377. BOOST_CONTRACT_ASSERT(...);
  378. ...
  379. })
  380. ;
  381. ... // Function body (use `return result = return_expr`).
  382. }
  383. ...
  384. };
  385. @endcode
  386. A virtual public function should always call
  387. @RefFunc{boost::contract::public_function} otherwise this library will not
  388. be able to correctly use it for subcontracting.
  389. @see @RefSect{tutorial.virtual_public_functions, Virtual Public Functions}
  390. @param v The trailing parameter of type
  391. @RefClass{boost::contract::virtual_}<c>*</c> and default value
  392. @c 0 from the enclosing virtual public function.
  393. @param r A reference to the return value of the enclosing virtual public
  394. function declaring the contract.
  395. This is usually a local variable declared by the enclosing
  396. virtual public function just before the contract, but
  397. programmers must set it to the actual value being returned by
  398. the function at each @c return statement.
  399. @param obj The object @c this from the scope of the enclosing virtual
  400. public function declaring the contract.
  401. This object might be mutable, @c const, @c volatile, or
  402. <c>const volatile</c> depending on the cv-qualifier of the
  403. enclosing function (volatile public functions will check
  404. volatile class invariants, see
  405. @RefSect{extras.volatile_public_functions,
  406. Volatile Public Functions}).
  407. @tparam VirtualResult This type must be the same as, or compatible with,
  408. the return type of the enclosing virtual public
  409. function declaring the contract (this library might
  410. not be able to generate a compile-time error if
  411. these types mismatch, but in general that will cause
  412. run-time errors or undefined behaviour).
  413. Alternatively,
  414. <c>boost::optional<<i>return-type</i>></c> can also
  415. be used (see
  416. @RefSect{advanced.optional_return_values,
  417. Optional Return Values}).
  418. (Usually this template parameter is automatically
  419. deduced by C++ and it does not need to be explicitly
  420. specified by programmers.)
  421. @tparam Class The type of the class containing the virtual public function
  422. declaring the contract.
  423. (Usually this template parameter is automatically deduced by
  424. C++ and it does not need to be explicitly specified by
  425. programmers.)
  426. @return The result of this function must be assigned to a variable of type
  427. @RefClass{boost::contract::check} declared explicitly (i.e., without
  428. using C++11 @c auto declarations) and locally just before the code
  429. of the public function body (otherwise this library will generate a
  430. run-time error, see
  431. @RefMacro{BOOST_CONTRACT_ON_MISSING_CHECK_DECL}).
  432. */
  433. template<typename VirtualResult, class Class>
  434. specify_precondition_old_postcondition_except<VirtualResult>
  435. public_function(virtual_* v, VirtualResult& r, Class* obj);
  436. #else
  437. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_NO_OVERRIDE_(
  438. /* has_virtual_result = */ 0)
  439. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_NO_OVERRIDE_(
  440. /* has_virtual_result = */ 1)
  441. #endif
  442. /** @cond */
  443. // For non-static, virtual, and overriding public functions (PRIVATE macro).
  444. #define BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_Z_( \
  445. z, arity, arity_compl, has_virtual_result) \
  446. BOOST_CONTRACT_DETAIL_DECL_OVERRIDING_PUBLIC_FUNCTION_Z(z, \
  447. arity, /* is_friend = */ 0, has_virtual_result, \
  448. Override, VirtualResult, F, Class, Args, \
  449. v, r, /* f */ BOOST_PP_EMPTY(), obj, args \
  450. ) { \
  451. BOOST_PP_IIF(BOOST_CONTRACT_PUBLIC_FUNCTIONS_IMPL_, \
  452. { /* extra scope paren to expand STATIC_STATIC emu on same line */ \
  453. /* assert not strictly necessary as compilation will fail */ \
  454. /* anyways, but helps limiting cryptic compiler's errors */ \
  455. BOOST_STATIC_ASSERT_MSG( \
  456. /* -2 for both `this` and `virtual_*` extra parameters */ \
  457. ( \
  458. boost::function_types::function_arity<F>::value - 2 \
  459. == \
  460. BOOST_CONTRACT_DETAIL_TVARIADIC_SIZEOF(arity, Args) \
  461. ), \
  462. "missing one or more arguments for specified function" \
  463. ); \
  464. } \
  465. { /* extra scope paren to expand STATIC_STATIC emu on same line */ \
  466. /* assert consistency of F's result type and VirtualResult */ \
  467. BOOST_PP_IIF(has_virtual_result, \
  468. BOOST_STATIC_ASSERT_MSG \
  469. , \
  470. BOOST_PP_TUPLE_EAT(2) \
  471. )( \
  472. (boost::is_same< \
  473. typename boost::remove_reference<typename boost:: \
  474. function_types::result_type<F>::type>::type, \
  475. typename boost::contract::detail:: \
  476. remove_value_reference_if_optional< \
  477. VirtualResult \
  478. >::type \
  479. >::value), \
  480. "mismatching result type for specified function" \
  481. ); \
  482. } \
  483. { /* extra scope paren to expand STATIC_STATIC emu on same line */ \
  484. /* assert this so lib can check and enforce override */ \
  485. BOOST_STATIC_ASSERT_MSG( \
  486. boost::contract::access::has_base_types<Class>::value, \
  487. "enclosing class missing 'base-types' typedef" \
  488. ); \
  489. } \
  490. return (specify_precondition_old_postcondition_except< \
  491. BOOST_PP_EXPR_IIF(has_virtual_result, VirtualResult)>( \
  492. new boost::contract::detail::public_function< \
  493. Override, \
  494. BOOST_PP_IIF(has_virtual_result, \
  495. VirtualResult \
  496. , \
  497. boost::contract::detail::none \
  498. ), \
  499. F, \
  500. Class \
  501. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(arity) \
  502. BOOST_CONTRACT_DETAIL_TVARIADIC_ARGS_Z(z, arity, Args) \
  503. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA(arity_compl) \
  504. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(z, arity_compl, \
  505. boost::contract::detail::none) \
  506. >( \
  507. v, \
  508. obj, \
  509. BOOST_PP_IIF(has_virtual_result, \
  510. r \
  511. , \
  512. boost::contract::detail::none::value() \
  513. ) \
  514. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(arity) \
  515. BOOST_CONTRACT_DETAIL_TVARIADIC_ARGS_Z(z, arity, args) \
  516. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA(arity_compl) \
  517. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(z, arity_compl, \
  518. boost::contract::detail::none::value()) \
  519. ) \
  520. )); \
  521. , \
  522. return specify_precondition_old_postcondition_except< \
  523. BOOST_PP_EXPR_IIF(has_virtual_result, VirtualResult)>(); \
  524. ) \
  525. }
  526. /** @endcond */
  527. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  528. /**
  529. Program contracts for void public functions overrides (virtual or not).
  530. This is used to specify preconditions, postconditions, exception guarantees,
  531. old value copies at body, and check class invariants for public function
  532. overrides (virtual or not) that return @c void:
  533. @code
  534. class u
  535. #define BASES private boost::contract::constructor_precondition<u>, \
  536. public b, private w
  537. : BASES
  538. {
  539. friend class boost::contract::access;
  540. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  541. #undef BASES
  542. void invariant() const { // Optional (as for static and volatile).
  543. BOOST_CONTRACT_ASSERT(...);
  544. ...
  545. }
  546. BOOST_CONTRACT_OVERRIDES(f)
  547. public:
  548. // Override from `b::f`.
  549. void f(t_1 a_1, ..., t_n a_n, boost::contract::virtual_* v = 0) {
  550. boost::contract::old_ptr<old_type> old_var;
  551. boost::contract::check c = boost::contract::public_function<
  552. override_f>(v, &u::f, this, a_1, ..., a_n)
  553. .precondition([&] { // Optional.
  554. BOOST_CONTRACT_ASSERT(...);
  555. ...
  556. })
  557. .old([&] { // Optional.
  558. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  559. ...
  560. })
  561. .postcondition([&] { // Optional.
  562. BOOST_CONTRACT_ASSERT(...);
  563. ...
  564. })
  565. .except([&] { // Optional.
  566. BOOST_CONTRACT_ASSERT(...);
  567. ...
  568. })
  569. ;
  570. ... // Function body.
  571. }
  572. ...
  573. };
  574. @endcode
  575. A public function override should always call
  576. @RefFunc{boost::contract::public_function} otherwise this library will not
  577. be able to correctly use it for subcontracting.
  578. @see @RefSect{tutorial.public_function_overrides__subcontracting_,
  579. Public Function Overrides}
  580. @param v The trailing parameter of type
  581. @RefClass{boost::contract::virtual_}<c>*</c> and default value
  582. @c 0 from the enclosing public function override.
  583. @param f A pointer to the enclosing public function override declaring
  584. the contract (but see @RefSect{advanced.function_overloads,
  585. Function Overloads}).
  586. @param obj The object @c this from the scope of the enclosing public
  587. function override declaring the contract.
  588. This object might be mutable, @c const, @c volatile, or
  589. <c>const volatile</c> depending on the cv-qualifier of the
  590. enclosing function (volatile public functions will check
  591. volatile class invariants, see
  592. @RefSect{extras.volatile_public_functions,
  593. Volatile Public Functions}).
  594. @param args All arguments passed to the enclosing public function override
  595. declaring the contract (by reference and in the order they
  596. appear in the enclosing function declaration), but excluding the
  597. trailing argument @c v.
  598. @tparam Override The type trait <c>override_<i>function-name</i></c>
  599. declared using the @RefMacro{BOOST_CONTRACT_OVERRIDE} or
  600. related macros.
  601. This template parameter must be explicitly specified
  602. (because there is no function argument from which it can
  603. be automatically deduced by C++).
  604. @tparam F The function pointer type of the enclosing public function
  605. override declaring the contract.
  606. (Usually this template parameter is automatically deduced by
  607. C++ and it does not need to be explicitly specified by
  608. programmers.)
  609. @tparam Class The type of the class containing the virtual public function
  610. declaring the contract.
  611. (Usually this template parameter is automatically deduced by
  612. C++ and it does not need to be explicitly specified by
  613. programmers.)
  614. @tparam Args The types of all parameters passed to the enclosing public
  615. function override declaring the contract, but excluding the
  616. trailing parameter type <c>boost::contract::virtual_*</c>.
  617. On compilers that do not support variadic templates, this
  618. library internally implements this function using
  619. preprocessor meta-programming (in this case, the maximum
  620. number of supported arguments is defined by
  621. @RefMacro{BOOST_CONTRACT_MAX_ARGS}).
  622. (Usually these template parameters are automatically deduced
  623. by C++ and they do not need to be explicitly specified by
  624. programmers.)
  625. @return The result of this function must be assigned to a variable of type
  626. @RefClass{boost::contract::check} declared explicitly (i.e., without
  627. using C++11 @c auto declarations) and locally just before the code
  628. of the public function body (otherwise this library will generate a
  629. run-time error, see
  630. @RefMacro{BOOST_CONTRACT_ON_MISSING_CHECK_DECL}).
  631. */
  632. template<class Override, typename F, class Class, typename... Args>
  633. specify_precondition_old_postcondition_except<> public_function(
  634. virtual_* v, F f, Class* obj, Args&... args);
  635. /**
  636. Program contracts for non-void public functions overrides (virtual or not).
  637. This is used to specify preconditions, postconditions, exception guarantees,
  638. old value copies at body, and check class invariants for public function
  639. overrides (virtual or not) that do not return @c void:
  640. @code
  641. class u
  642. #define BASES private boost::contract::constructor_precondition<u>, \
  643. public b, private w
  644. : BASES
  645. {
  646. friend class boost::contract::access;
  647. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  648. #undef BASES
  649. void invariant() const { // Optional (as for static and volatile).
  650. BOOST_CONTRACT_ASSERT(...);
  651. ...
  652. }
  653. BOOST_CONTRACT_OVERRIDES(f)
  654. public:
  655. // Override from `b::f`.
  656. t f(t_1 a_1, ..., t_n a_n, boost::contract::virtual_* v = 0) {
  657. t result;
  658. boost::contract::old_ptr<old_type> old_var;
  659. boost::contract::check c = boost::contract::public_function<
  660. override_f>(v, result, &u::f, this, a_1, ..., a_n)
  661. .precondition([&] { // Optional.
  662. BOOST_CONTRACT_ASSERT(...);
  663. ...
  664. })
  665. .old([&] { // Optional.
  666. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  667. ...
  668. })
  669. .postcondition([&] (t const& result) { // Optional.
  670. BOOST_CONTRACT_ASSERT(...);
  671. ...
  672. })
  673. .except([&] { // Optional.
  674. BOOST_CONTRACT_ASSERT(...);
  675. ...
  676. })
  677. ;
  678. ... // Function body (use `return result = return_expr`).
  679. }
  680. ...
  681. };
  682. @endcode
  683. A public function override should always call
  684. @RefFunc{boost::contract::public_function} otherwise this library will not
  685. be able to correctly use it for subcontracting.
  686. @see @RefSect{tutorial.public_function_overrides__subcontracting_,
  687. Public Function Overrides}
  688. @param v The trailing parameter of type
  689. @RefClass{boost::contract::virtual_}<c>*</c> and default value
  690. @c 0 from the enclosing public function override.
  691. @param r A reference to the return value of the enclosing public function
  692. override declaring the contract.
  693. This is usually a local variable declared by the enclosing
  694. public function override just before the contract, but
  695. programmers must set it to the actual value being returned by
  696. the function at each @c return statement.
  697. @param f A pointer to the enclosing public function override declaring
  698. the contract (but see @RefSect{advanced.function_overloads,
  699. Function Overloads}).
  700. @param obj The object @c this from the scope of the enclosing public
  701. function override declaring the contract.
  702. This object might be mutable, @c const, @c volatile, or
  703. <c>const volatile</c> depending on the cv-qualifier of the
  704. enclosing function (volatile public functions will check
  705. volatile class invariants, see
  706. @RefSect{extras.volatile_public_functions,
  707. Volatile Public Functions}).
  708. @param args All arguments passed to the enclosing public function override
  709. declaring the contract (by reference and in the order they
  710. appear in the enclosing function declaration), but excluding the
  711. trailing argument @c v.
  712. @tparam Override The type trait <c>override_<i>function-name</i></c>
  713. declared using the @RefMacro{BOOST_CONTRACT_OVERRIDE} or
  714. related macros.
  715. This template parameter must be explicitly specified
  716. (because there is no function argument from which it can
  717. be automatically deduced by C++).
  718. @tparam VirtualResult This type must be the same as, or compatible with,
  719. the return type of the enclosing public function
  720. override declaring the contract (this library might
  721. not be able to generate a compile-time error if
  722. these types mismatch, but in general that will cause
  723. run-time errors or undefined behaviour).
  724. Alternatively,
  725. <c>boost::optional<<i>return-type</i>></c> can also
  726. be used (see
  727. @RefSect{advanced.optional_return_values,
  728. Optional Return Values}).
  729. (Usually this template parameter is automatically
  730. deduced by C++ and it does not need to be explicitly
  731. specified by programmers.)
  732. @tparam F The function pointer type of the enclosing public function
  733. override declaring the contract.
  734. (Usually this template parameter is automatically deduced by
  735. C++ and it does not need to be explicitly specified by
  736. programmers.)
  737. @tparam Class The type of the class containing the virtual public function
  738. declaring the contract.
  739. (Usually this template parameter is automatically deduced by
  740. C++ and it does not need to be explicitly specified by
  741. programmers.)
  742. @tparam Args The types of all parameters passed to the enclosing public
  743. function override declaring the contract, but excluding the
  744. trailing parameter type <c>boost::contract::virtual_*</c>.
  745. On compilers that do not support variadic templates, this
  746. library internally implements this function using
  747. preprocessor meta-programming (in this case, the maximum
  748. number of supported arguments is defined by
  749. @RefMacro{BOOST_CONTRACT_MAX_ARGS}).
  750. (Usually these template parameters are automatically deduced
  751. by C++ and they do not need to be explicitly specified by
  752. programmers.)
  753. @return The result of this function must be assigned to a variable of type
  754. @RefClass{boost::contract::check} declared explicitly (i.e., without
  755. using C++11 @c auto declarations) and locally just before the code
  756. of the public function body (otherwise this library will generate a
  757. run-time error, see
  758. @RefMacro{BOOST_CONTRACT_ON_MISSING_CHECK_DECL}).
  759. */
  760. template<class Override, typename VirtualResult, typename F, class Class,
  761. typename... Args>
  762. specify_precondition_old_postcondition_except<VirtualResult>
  763. public_function(virtual_* v, VirtualResult& r, F f, Class* obj,
  764. Args&... args);
  765. #elif BOOST_CONTRACT_DETAIL_TVARIADIC
  766. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_Z_(1, /* arity = */ ~,
  767. /* arity_compl = */ ~, /* has_virtual_result = */ 0)
  768. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_Z_(1, /* arity = */ ~,
  769. /* arity_compl = */ ~, /* has_virtual_result = */ 1)
  770. #else
  771. /* PRIVATE */
  772. #define BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_ARITY_( \
  773. z, arity, unused) \
  774. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDES_(z, arity, \
  775. BOOST_PP_SUB(BOOST_CONTRACT_MAX_ARGS, arity), ~)
  776. #define BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDES_(z, \
  777. arity, arity_compl, unused) \
  778. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_Z_(z, \
  779. arity, arity_compl, /* has_virtual_result = */ 0) \
  780. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_Z_(z, \
  781. arity, arity_compl, /* has_virtual_result = */ 1)
  782. /* CODE */
  783. BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_CONTRACT_MAX_ARGS),
  784. BOOST_CONTRACT_PUBLIC_FUNCTION_VIRTUAL_OVERRIDE_ARITY_, ~)
  785. #endif
  786. } } // namespace
  787. #endif // #include guard