public_function.hpp 37 KB

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