specify.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #ifndef BOOST_CONTRACT_SPECIFY_HPP_
  2. #define BOOST_CONTRACT_SPECIFY_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. Specify preconditions, old values copied at body, postconditions, and exception
  9. guarantees
  10. Preconditions, old values copied at body, postconditions, and exception
  11. guarantees are all optionals but, when they are specified, they need to be
  12. specified in that order.
  13. */
  14. #include <boost/contract/core/config.hpp>
  15. #include <boost/contract/detail/decl.hpp>
  16. #if !defined(BOOST_CONTRACT_NO_CONDITIONS) || \
  17. defined(BOOST_CONTRACT_STATIC_LINK)
  18. #include <boost/contract/detail/condition/cond_base.hpp>
  19. #include <boost/contract/detail/condition/cond_post.hpp>
  20. #include <boost/contract/detail/auto_ptr.hpp>
  21. #include <boost/contract/detail/none.hpp>
  22. #endif
  23. #if !defined(BOOST_CONTRACT_NO_PRECONDITIONS) || \
  24. !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  25. !defined(BOOST_CONTRACT_NO_EXCEPTS)
  26. #include <boost/contract/detail/debug.hpp>
  27. #endif
  28. #include <boost/config.hpp>
  29. // NOTE: No inheritance for faster run-times (macros to avoid duplicated code).
  30. /* PRIVATE */
  31. /* @cond */
  32. // NOTE: Private copy ops below will force compile-time error is `auto c = ...`
  33. // is used instead of `check c = ...` but only up to C++17. C++17 strong copy
  34. // elision on function return values prevents this lib from generating a
  35. // compile-time error in those cases, but the lib will still generate a run-time
  36. // error according with ON_MISSING_CHECK_DECL. Furthermore, on some C++98
  37. // compilers, this private copy ctor gives a warning (because of lack of copy
  38. // optimization on those compilers), this warning can be ignored.
  39. #if !defined(BOOST_CONTRACT_NO_CONDITIONS) || \
  40. defined(BOOST_CONTRACT_STATIC_LINK)
  41. #define BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(class_type, cond_type) \
  42. private: \
  43. boost::contract::detail::auto_ptr<cond_type > cond_; \
  44. explicit class_type(cond_type* cond) : cond_(cond) {} \
  45. class_type(class_type const& other) : cond_(other.cond_) {} \
  46. class_type& operator=(class_type const& other) { \
  47. cond_ = other.cond_; \
  48. return *this; \
  49. }
  50. #define BOOST_CONTRACT_SPECIFY_COND_RELEASE_ cond_.release()
  51. #else
  52. #define BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(class_type, cond_type) \
  53. private: \
  54. class_type() {} \
  55. class_type(class_type const&) {} \
  56. class_type& operator=(class_type const&) { return *this; }
  57. #define BOOST_CONTRACT_SPECIFY_COND_RELEASE_ /* nothing */
  58. #endif
  59. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  60. #define BOOST_CONTRACT_SPECIFY_PRECONDITION_IMPL_ \
  61. BOOST_CONTRACT_DETAIL_DEBUG(cond_); \
  62. cond_->set_pre(f); \
  63. return specify_old_postcondition_except<VirtualResult>( \
  64. BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  65. #else
  66. #define BOOST_CONTRACT_SPECIFY_PRECONDITION_IMPL_ \
  67. return specify_old_postcondition_except<VirtualResult>( \
  68. BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  69. #endif
  70. #ifndef BOOST_CONTRACT_NO_OLDS
  71. #define BOOST_CONTRACT_SPECIFY_OLD_IMPL_ \
  72. BOOST_CONTRACT_DETAIL_DEBUG(cond_); \
  73. cond_->set_old(f); \
  74. return specify_postcondition_except<VirtualResult>( \
  75. BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  76. #else
  77. #define BOOST_CONTRACT_SPECIFY_OLD_IMPL_ \
  78. return specify_postcondition_except<VirtualResult>( \
  79. BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  80. #endif
  81. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  82. #define BOOST_CONTRACT_SPECIFY_POSTCONDITION_IMPL_ \
  83. BOOST_CONTRACT_DETAIL_DEBUG(cond_); \
  84. cond_->set_post(f); \
  85. return specify_except(BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  86. #else
  87. #define BOOST_CONTRACT_SPECIFY_POSTCONDITION_IMPL_ \
  88. return specify_except(BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  89. #endif
  90. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  91. #define BOOST_CONTRACT_SPECIFY_EXCEPT_IMPL_ \
  92. BOOST_CONTRACT_DETAIL_DEBUG(cond_); \
  93. cond_->set_except(f); \
  94. return specify_nothing(BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  95. #else
  96. #define BOOST_CONTRACT_SPECIFY_EXCEPT_IMPL_ \
  97. return specify_nothing(BOOST_CONTRACT_SPECIFY_COND_RELEASE_);
  98. #endif
  99. /* @endcond */
  100. /* CODE */
  101. namespace boost {
  102. namespace contract {
  103. class virtual_;
  104. template<typename VR>
  105. class specify_precondition_old_postcondition_except;
  106. template<typename VR>
  107. class specify_old_postcondition_except;
  108. template<typename VR>
  109. class specify_postcondition_except;
  110. class specify_except;
  111. }
  112. }
  113. namespace boost { namespace contract {
  114. /**
  115. Used to prevent setting other contract conditions after exception guarantees.
  116. This class has no member function so it is used to prevent specifying additional
  117. functors to check any other contract.
  118. This object is internally constructed by the library when users specify
  119. contracts calling @RefFunc{boost::contract::function} and similar functions
  120. (that is why this class does not have a public constructor).
  121. @see @RefSect{tutorial, Tutorial}
  122. */
  123. class specify_nothing { // Privately copyable (as *).
  124. public:
  125. /**
  126. Destruct this object.
  127. @b Throws: This is declared @c noexcept(false) since C++11 to allow users
  128. to program failure handlers that throw exceptions on contract
  129. assertion failures (not the default, see
  130. @RefSect{advanced.throw_on_failures__and__noexcept__,
  131. Throw on Failure}).
  132. */
  133. ~specify_nothing() BOOST_NOEXCEPT_IF(false) {}
  134. // No set member function here.
  135. /** @cond */
  136. private:
  137. BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(specify_nothing,
  138. boost::contract::detail::cond_base)
  139. // Friends (used to limit library's public API).
  140. friend class check;
  141. template<typename VR>
  142. friend class specify_precondition_old_postcondition_except;
  143. template<typename VR>
  144. friend class specify_old_postcondition_except;
  145. template<typename VR>
  146. friend class specify_postcondition_except;
  147. friend class specify_except;
  148. /** @endcond */
  149. };
  150. /**
  151. Allow to specify exception guarantees.
  152. Allow to specify the functor this library will call to check exception
  153. guarantees.
  154. This object is internally constructed by the library when users specify
  155. contracts calling @RefFunc{boost::contract::function} and similar functions
  156. (that is why this class does not have a public constructor).
  157. @see @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  158. */
  159. class specify_except { // Privately copyable (as *).
  160. public:
  161. /**
  162. Destruct this object.
  163. @b Throws: This is declared @c noexcept(false) since C++11 to allow users
  164. to program failure handlers that throw exceptions on contract
  165. assertion failures (not the default, see
  166. @RefSect{advanced.throw_on_failures__and__noexcept__,
  167. Throw on Failure}).
  168. */
  169. ~specify_except() BOOST_NOEXCEPT_IF(false) {}
  170. /**
  171. Allow to specify exception guarantees.
  172. @param f Nullary functor called by this library to check exception
  173. guarantees @c f().
  174. Assertions within this functor are usually programmed using
  175. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  176. call to this functor indicates a contract assertion failure (and
  177. will result in this library calling
  178. @RefFunc{boost::contract::except_failure}).
  179. This functor should capture variables by (constant) references
  180. (to access the values they will have at function exit).
  181. @return After exception guarantees have been specified, the object returned
  182. by this function does not allow to specify any additional contract.
  183. */
  184. template<typename F>
  185. specify_nothing except(
  186. F const&
  187. #if !defined(BOOST_CONTRACT_NO_EXCEPTS) || \
  188. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  189. f
  190. #endif // Else, no name (avoid unused param warning).
  191. ) { BOOST_CONTRACT_SPECIFY_EXCEPT_IMPL_ }
  192. /** @cond */
  193. private:
  194. BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(specify_except,
  195. boost::contract::detail::cond_base)
  196. // Friends (used to limit library's public API).
  197. friend class check;
  198. template<typename VR>
  199. friend class specify_precondition_old_postcondition_except;
  200. template<typename VR>
  201. friend class specify_old_postcondition_except;
  202. template<typename VR>
  203. friend class specify_postcondition_except;
  204. /** @endcond */
  205. };
  206. /**
  207. Allow to specify postconditions or exception guarantees.
  208. Allow to specify functors this library will call to check postconditions or
  209. exception guarantees.
  210. This object is internally constructed by the library when users specify
  211. contracts calling @RefFunc{boost::contract::function} and similar functions
  212. (that is why this class does not have a public constructor).
  213. @see @RefSect{tutorial.postconditions, Postconditions},
  214. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  215. @tparam VirtualResult Return type of the enclosing function declaring the
  216. contract if that is either a virtual or an
  217. overriding public function, otherwise this is always
  218. @c void.
  219. (Usually this template parameter is automatically
  220. deduced by C++ and it does not need to be explicitly
  221. specified by programmers.)
  222. */
  223. template<typename VirtualResult = void>
  224. class specify_postcondition_except { // Privately copyable (as *).
  225. public:
  226. /**
  227. Destruct this object.
  228. @b Throws: This is declared @c noexcept(false) since C++11 to allow users
  229. to program failure handlers that throw exceptions on contract
  230. assertion failures (not the default, see
  231. @RefSect{advanced.throw_on_failures__and__noexcept__,
  232. Throw on Failure}).
  233. */
  234. ~specify_postcondition_except() BOOST_NOEXCEPT_IF(false) {}
  235. /**
  236. Allow to specify postconditions.
  237. @param f Functor called by this library to check postconditions
  238. @c f() or @c f(result).
  239. Assertions within this functor are usually programmed using
  240. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  241. call to this functor indicates a contract assertion failure (and
  242. will result in this library calling
  243. @RefFunc{boost::contract::postcondition_failure}).
  244. This functor should capture variables by (constant) references
  245. (to access the values they will have at function exit).
  246. This functor must be a nullary functor @c f() if
  247. @c VirtualResult is @c void, otherwise it must be a unary
  248. functor @c f(result) accepting the return value @c result as a
  249. parameter of type <c>VirtualResult const&</c> (to avoid extra
  250. copies of the return value, or of type @c VirtualResult or
  251. <c>VirtualResult const</c> if extra copies of the return value
  252. are irrelevant).
  253. @return After postconditions have been specified, the object returned by
  254. this function allows to optionally specify exception guarantees.
  255. */
  256. template<typename F>
  257. specify_except postcondition(
  258. F const&
  259. #if !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  260. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  261. f
  262. #endif // Else, no name (avoid unused param warning).
  263. ) {
  264. BOOST_CONTRACT_SPECIFY_POSTCONDITION_IMPL_
  265. }
  266. /**
  267. Allow to specify exception guarantees.
  268. @param f Nullary functor called by this library to check exception
  269. guarantees @c f().
  270. Assertions within this functor are usually programmed using
  271. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  272. call to this functor indicates a contract assertion failure (and
  273. will result in this library calling
  274. @RefFunc{boost::contract::except_failure}).
  275. This functor should capture variables by (constant) references
  276. (to access the values they will have at function exit).
  277. @return After exception guarantees have been specified, the object returned
  278. by this function does not allow to specify any additional contract.
  279. */
  280. template<typename F>
  281. specify_nothing except(
  282. F const&
  283. #if !defined(BOOST_CONTRACT_NO_EXCEPTS) || \
  284. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  285. f
  286. #endif // Else, no name (avoid unused param warning).
  287. ) { BOOST_CONTRACT_SPECIFY_EXCEPT_IMPL_ }
  288. /** @cond */
  289. private:
  290. BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(
  291. specify_postcondition_except,
  292. boost::contract::detail::cond_post<typename
  293. boost::contract::detail::none_if_void<VirtualResult>::type>
  294. )
  295. // Friends (used to limit library's public API).
  296. friend class check;
  297. friend class specify_precondition_old_postcondition_except<VirtualResult>;
  298. friend class specify_old_postcondition_except<VirtualResult>;
  299. /** @endcond */
  300. };
  301. /**
  302. Allow to specify old values copied at body, postconditions, and exception
  303. guarantees.
  304. Allow to specify functors this library will call to copy old values at body,
  305. check postconditions, and check exception guarantees.
  306. This object is internally constructed by the library when users specify
  307. contracts calling @RefFunc{boost::contract::function} and similar functions
  308. (that is why this class does not have a public constructor).
  309. @see @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body},
  310. @RefSect{tutorial.postconditions, Postconditions},
  311. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  312. @tparam VirtualResult Return type of the enclosing function declaring the
  313. contract if that is either a virtual or an
  314. overriding public function, otherwise this is always
  315. @c void.
  316. (Usually this template parameter is automatically
  317. deduced by C++ and it does not need to be explicitly
  318. specified by programmers.)
  319. */
  320. template<typename VirtualResult = void>
  321. class specify_old_postcondition_except { // Privately copyable (as *).
  322. public:
  323. /**
  324. Destruct this object.
  325. @b Throws: This is declared @c noexcept(false) since C++11 to allow users
  326. to program failure handlers that throw exceptions on contract
  327. assertion failures (not the default, see
  328. @RefSect{advanced.throw_on_failures__and__noexcept__,
  329. Throw on Failure}).
  330. */
  331. ~specify_old_postcondition_except() BOOST_NOEXCEPT_IF(false) {}
  332. /**
  333. Allow to specify old values copied at body.
  334. It should often be sufficient to initialize old value pointers as soon as
  335. they are declared, without using this function (see
  336. @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body}).
  337. @param f Nullary functor called by this library @c f() to assign old
  338. value copies just before the body is executed but after entry
  339. invariants (when they apply) and preconditions are checked.
  340. Old value pointers within this functor call are usually assigned
  341. using @RefMacro{BOOST_CONTRACT_OLDOF}.
  342. Any exception thrown by a call to this functor will result in
  343. this library calling @RefFunc{boost::contract::old_failure}
  344. (because old values could not be copied to check postconditions
  345. and exception guarantees).
  346. This functor should capture old value pointers by references so
  347. they can be assigned (all other variables needed to evaluate old
  348. value expressions can be captured by (constant) value, or better
  349. by (constant) reference to avoid extra copies).
  350. @return After old values copied at body have been specified, the object
  351. returned by this function allows to optionally specify
  352. postconditions and exception guarantees.
  353. */
  354. template<typename F>
  355. specify_postcondition_except<VirtualResult> old(
  356. F const&
  357. #if !defined(BOOST_CONTRACT_NO_OLDS) || \
  358. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  359. f
  360. #endif // Else, no name (avoid unused param warning).
  361. ) {
  362. BOOST_CONTRACT_SPECIFY_OLD_IMPL_
  363. }
  364. /**
  365. Allow to specify postconditions.
  366. @param f Functor called by this library to check postconditions
  367. @c f() or @c f(result).
  368. Assertions within this functor are usually programmed using
  369. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  370. call to this functor indicates a contract assertion failure (and
  371. will result in this library calling
  372. @RefFunc{boost::contract::postcondition_failure}).
  373. This functor should capture variables by (constant) references
  374. (to access the values they will have at function exit).
  375. This functor must be a nullary functor @c f() if
  376. @c VirtualResult is @c void, otherwise it must be a unary
  377. functor @c f(result) accepting the return value @c result as a
  378. parameter of type <c>VirtualResult const&</c> (to avoid extra
  379. copies of the return value, or of type @c VirtualResult or
  380. <c>VirtualResult const</c> if extra copies of the return value
  381. are irrelevant).
  382. @return After postconditions have been specified, the object returned by
  383. this function allows to optionally specify exception guarantees.
  384. */
  385. template<typename F>
  386. specify_except postcondition(
  387. F const&
  388. #if !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  389. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  390. f
  391. #endif // Else, no name (avoid unused param warning).
  392. ) {
  393. BOOST_CONTRACT_SPECIFY_POSTCONDITION_IMPL_
  394. }
  395. /**
  396. Allow to specify exception guarantees.
  397. @param f Nullary functor called by this library to check exception
  398. guarantees @c f().
  399. Assertions within this functor are usually programmed using
  400. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  401. call to this functor indicates a contract assertion failure (and
  402. will result in this library calling
  403. @RefFunc{boost::contract::except_failure}).
  404. This functor should capture variables by (constant) references
  405. (to access the values they will have at function exit).
  406. @return After exception guarantees have been specified, the object returned
  407. by this function does not allow to specify any additional contract.
  408. */
  409. template<typename F>
  410. specify_nothing except(
  411. F const&
  412. #if !defined(BOOST_CONTRACT_NO_EXCEPTS) || \
  413. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  414. f
  415. #endif // Else, no name (avoid unused param warning).
  416. ) { BOOST_CONTRACT_SPECIFY_EXCEPT_IMPL_ }
  417. /** @cond */
  418. private:
  419. BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(
  420. specify_old_postcondition_except,
  421. boost::contract::detail::cond_post<typename
  422. boost::contract::detail::none_if_void<VirtualResult>::type>
  423. )
  424. // Friends (used to limit library's public API).
  425. friend class check;
  426. friend class specify_precondition_old_postcondition_except<VirtualResult>;
  427. template<class C>
  428. friend specify_old_postcondition_except<> constructor(C*);
  429. template<class C>
  430. friend specify_old_postcondition_except<> destructor(C*);
  431. /** @endcond */
  432. };
  433. /**
  434. Allow to specify preconditions, old values copied at body, postconditions, and
  435. exception guarantees.
  436. Allow to specify functors this library will call to check preconditions, copy
  437. old values at body, check postconditions, and check exception guarantees.
  438. This object is internally constructed by the library when users specify
  439. contracts calling @RefFunc{boost::contract::function} and similar functions
  440. (that is why this class does not have a public constructor).
  441. @see @RefSect{tutorial.preconditions, Preconditions},
  442. @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body},
  443. @RefSect{tutorial.postconditions, Postconditions},
  444. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  445. @tparam VirtualResult Return type of the enclosing function declaring the
  446. contract if that is either a virtual or an
  447. overriding public function, otherwise this is always
  448. @c void.
  449. (Usually this template parameter is automatically
  450. deduced by C++ and it does not need to be explicitly
  451. specified by programmers.)
  452. */
  453. template<
  454. typename VirtualResult /* = void (already in fwd decl from decl.hpp) */
  455. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  456. = void
  457. #endif
  458. >
  459. class specify_precondition_old_postcondition_except { // Priv. copyable (as *).
  460. public:
  461. /**
  462. Destruct this object.
  463. @b Throws: This is declared @c noexcept(false) since C++11 to allow users
  464. to program failure handlers that throw exceptions on contract
  465. assertion failures (not the default, see
  466. @RefSect{advanced.throw_on_failures__and__noexcept__,
  467. Throw on Failure}).
  468. */
  469. ~specify_precondition_old_postcondition_except() BOOST_NOEXCEPT_IF(false) {}
  470. /**
  471. Allow to specify preconditions.
  472. @param f Nullary functor called by this library to check preconditions
  473. @c f().
  474. Assertions within this functor are usually programmed using
  475. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  476. call to this functor indicates a contract assertion failure (and
  477. will result in this library calling
  478. @RefFunc{boost::contract::precondition_failure}).
  479. This functor should capture variables by (constant) value, or
  480. better by (constant) reference (to avoid extra copies).
  481. @return After preconditions have been specified, the object returned by this
  482. function allows to optionally specify old values copied at body,
  483. postconditions, and exception guarantees.
  484. */
  485. template<typename F>
  486. specify_old_postcondition_except<VirtualResult> precondition(
  487. F const&
  488. #if !defined(BOOST_CONTRACT_NO_PRECONDITIONS) || \
  489. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  490. f
  491. #endif // Else, no name (avoid unused param warning).
  492. ) {
  493. BOOST_CONTRACT_SPECIFY_PRECONDITION_IMPL_
  494. }
  495. /**
  496. Allow to specify old values copied at body.
  497. It should often be sufficient to initialize old value pointers as soon as
  498. they are declared, without using this function (see
  499. @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body}).
  500. @param f Nullary functor called by this library @c f() to assign old
  501. value copies just before the body is executed but after entry
  502. invariants (when they apply) and preconditions are checked.
  503. Old value pointers within this functor call are usually assigned
  504. using @RefMacro{BOOST_CONTRACT_OLDOF}.
  505. Any exception thrown by a call to this functor will result in
  506. this library calling @RefFunc{boost::contract::old_failure}
  507. (because old values could not be copied to check postconditions
  508. and exception guarantees).
  509. This functor should capture old value pointers by references so
  510. they can be assigned (all other variables needed to evaluate old
  511. value expressions can be captured by (constant) value, or better
  512. by (constant) reference to avoid extra copies).
  513. @return After old values copied at body have been specified, the object
  514. returned by this functions allows to optionally specify
  515. postconditions and exception guarantees.
  516. */
  517. template<typename F>
  518. specify_postcondition_except<VirtualResult> old(
  519. F const&
  520. #if !defined(BOOST_CONTRACT_NO_OLDS) || \
  521. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  522. f
  523. #endif // Else, no name (avoid unused param warning).
  524. ) {
  525. BOOST_CONTRACT_SPECIFY_OLD_IMPL_
  526. }
  527. /**
  528. Allow to specify postconditions.
  529. @param f Functor called by this library to check postconditions
  530. @c f() or @c f(result).
  531. Assertions within this functor are usually programmed using
  532. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  533. call to this functor indicates a contract assertion failure (and
  534. will result in this library calling
  535. @RefFunc{boost::contract::postcondition_failure}).
  536. This functor should capture variables by (constant) references
  537. (to access the values they will have at function exit).
  538. This functor must be a nullary functor @c f() if
  539. @c VirtualResult is @c void, otherwise it must be a unary
  540. functor @c f(result) accepting the return value @c result as a
  541. parameter of type <c>VirtualResult const&</c> (to avoid extra
  542. copies of the return value, or of type @c VirtualResult or
  543. <c>VirtualResult const</c> if extra copies of the return value
  544. are irrelevant).
  545. @return After postconditions have been specified, the object returned by
  546. this function allows to optionally specify exception guarantees.
  547. */
  548. template<typename F>
  549. specify_except postcondition(
  550. F const&
  551. #if !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  552. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  553. f
  554. #endif // Else, no name (avoid unused param warning).
  555. ) {
  556. BOOST_CONTRACT_SPECIFY_POSTCONDITION_IMPL_
  557. }
  558. /**
  559. Allow to specify exception guarantees.
  560. @param f Nullary functor called by this library to check exception
  561. guarantees @c f().
  562. Assertions within this functor are usually programmed using
  563. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a
  564. call to this functor indicates a contract assertion failure (and
  565. will result in this library calling
  566. @RefFunc{boost::contract::except_failure}).
  567. This functor should capture variables by (constant) references
  568. (to access the values they will have at function exit).
  569. @return After exception guarantees have been specified, the object returned
  570. by this function does not allow to specify any additional contract.
  571. */
  572. template<typename F>
  573. specify_nothing except(
  574. F const&
  575. #if !defined(BOOST_CONTRACT_NO_EXCEPTS) || \
  576. defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
  577. f
  578. #endif // Else, no name (avoid unused param warning).
  579. ) { BOOST_CONTRACT_SPECIFY_EXCEPT_IMPL_ }
  580. /** @cond */
  581. private:
  582. BOOST_CONTRACT_SPECIFY_CLASS_IMPL_(
  583. specify_precondition_old_postcondition_except,
  584. boost::contract::detail::cond_post<typename
  585. boost::contract::detail::none_if_void<VirtualResult>::type>
  586. )
  587. // Friends (used to limit library's public API).
  588. friend class check;
  589. friend specify_precondition_old_postcondition_except<> function();
  590. template<class C>
  591. friend specify_precondition_old_postcondition_except<> public_function();
  592. template<class C>
  593. friend specify_precondition_old_postcondition_except<> public_function(C*);
  594. template<class C>
  595. friend specify_precondition_old_postcondition_except<> public_function(
  596. virtual_*, C*);
  597. template<typename VR, class C>
  598. friend specify_precondition_old_postcondition_except<VR> public_function(
  599. virtual_*, VR&, C*);
  600. BOOST_CONTRACT_DETAIL_DECL_FRIEND_OVERRIDING_PUBLIC_FUNCTIONS_Z(1,
  601. O, VR, F, C, Args, v, r, f, obj, args)
  602. /** @endcond */
  603. };
  604. } } // namespace
  605. #endif // #include guard