exception.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. #ifndef BOOST_CONTRACT_EXCEPTION_HPP_
  2. #define BOOST_CONTRACT_EXCEPTION_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. Handle contract assertion failures.
  9. */
  10. // IMPORTANT: Included by contract_macro.hpp so trivial headers only.
  11. #include <boost/contract/core/config.hpp>
  12. #include <boost/contract/detail/declspec.hpp> // No compile-time overhead.
  13. #include <boost/function.hpp>
  14. #include <boost/config.hpp>
  15. #include <exception>
  16. #include <string>
  17. // NOTE: This code should not change (not even its impl) based on the
  18. // CONTRACT_NO_... macros. For example, preconditions_failure() should still
  19. // all the set precondition failure handler even when NO_PRECONDITIONS is
  20. // #defined, because user code might explicitly call precondition_failure()
  21. // (for whatever reason...). Otherwise, the public API of this lib will change.
  22. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  23. // Needed for `std::` prefix to show (but removed via `EXCLUDE_SYMBOLS=std`).
  24. namespace std {
  25. class exception {};
  26. class bad_cast {};
  27. }
  28. #endif
  29. namespace boost { namespace contract {
  30. /**
  31. Public base class for all exceptions directly thrown by this library.
  32. This class does not inherit from @c std::exception because exceptions deriving
  33. from this class will do that (inheriting from @c std::exception,
  34. @c std::bad_cast, etc.).
  35. @see @RefClass{boost::contract::assertion_failure},
  36. @RefClass{boost::contract::bad_virtual_result_cast},
  37. etc.
  38. */
  39. class BOOST_CONTRACT_DETAIL_DECLSPEC exception {
  40. public:
  41. /**
  42. Destruct this object.
  43. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  44. */
  45. virtual ~exception() /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */;
  46. };
  47. #ifdef BOOST_MSVC
  48. #pragma warning(push)
  49. #pragma warning(disable: 4275) // Bases w/o DLL spec (bad_cast, etc).
  50. #pragma warning(disable: 4251) // Members w/o DLL spec (string for what_).
  51. #endif
  52. /**
  53. Exception thrown when inconsistent return values are passed to overridden
  54. virtual public functions.
  55. This exception is thrown when programmers pass to this library return value
  56. parameters for public function overrides in derived classes that are not
  57. consistent with the return type parameter passed for the virtual public function
  58. being overridden from the base classes.
  59. This allows this library to give more descriptive error messages in such cases
  60. of misuse.
  61. This exception is internally thrown by this library and programmers should not
  62. need to throw it from user code.
  63. @see @RefSect{tutorial.public_function_overrides__subcontracting_,
  64. Public Function Overrides}
  65. */
  66. class BOOST_CONTRACT_DETAIL_DECLSPEC bad_virtual_result_cast : // Copy (as str).
  67. public std::bad_cast, public boost::contract::exception {
  68. public:
  69. /**
  70. Construct this object with the name of the from- and to- result types.
  71. @param from_type_name Name of the from-type (source of the cast).
  72. @param to_type_name Name of the to-type (destination of the cast).
  73. */
  74. explicit bad_virtual_result_cast(char const* from_type_name,
  75. char const* to_type_name);
  76. /**
  77. Destruct this object.
  78. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  79. */
  80. ~bad_virtual_result_cast()
  81. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */
  82. BOOST_OVERRIDE;
  83. /**
  84. Description for this error (containing both from- and to- type names).
  85. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  86. */
  87. char const* what() const
  88. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */
  89. BOOST_OVERRIDE;
  90. /** @cond */
  91. private:
  92. std::string what_;
  93. /** @endcond */
  94. };
  95. /**
  96. Exception typically used to report a contract assertion failure.
  97. This exception is thrown by code expanded by @RefMacro{BOOST_CONTRACT_ASSERT}
  98. (but it can also be thrown by user code programmed manually without that macro).
  99. This exception is typically used to report contract assertion failures because
  100. it contains detailed information about the file name, line number, and source
  101. code of the asserted condition (so it can be used by this library to provide
  102. detailed error messages when handling contract assertion failures).
  103. However, any other exception can be used to report a contract assertion failure
  104. (including user-defined exceptions).
  105. This library will call the appropriate contract failure handler function
  106. (@RefFunc{boost::contract::precondition_failure}, etc.) when this or any other
  107. exception is thrown while checking contracts (by default, these failure handler
  108. functions print an error message to @c std::cerr and terminate the program, but
  109. they can be customized to take any other action).
  110. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  111. @RefSect{extras.no_macros__and_no_variadic_macros_, No Macros}
  112. */
  113. class BOOST_CONTRACT_DETAIL_DECLSPEC assertion_failure : // Copy (as str, etc.).
  114. public std::exception, public boost::contract::exception {
  115. public:
  116. /**
  117. Construct this object with file name, line number, and source code text of
  118. an assertion condition (all optional).
  119. This constructor can also be used to specify no information (default
  120. constructor), or to specify only file name and line number but not source
  121. code text (because of the parameter default values).
  122. @param file Name of the file containing the assertion (usually set using
  123. <c>__FILE__</c>).
  124. @param line Number of the line containing the assertion (usually set using
  125. <c>__LINE__</c>).
  126. @param code Text listing the source code of the assertion condition.
  127. */
  128. explicit assertion_failure(char const* file = "", unsigned long line = 0,
  129. char const* code = "");
  130. /**
  131. Construct this object only with the source code text of the assertion
  132. condition.
  133. @param code Text listing the source code of the assertion condition.
  134. */
  135. explicit assertion_failure(char const* code);
  136. /**
  137. Destruct this object.
  138. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  139. */
  140. ~assertion_failure()
  141. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */
  142. BOOST_OVERRIDE;
  143. /**
  144. String describing the failed assertion.
  145. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  146. @return A string formatted similarly to the following:
  147. <c>assertion "`code()`" failed: file "`file()`", line \`line()\`</c>
  148. (where `` indicate execution quotes).
  149. File, line, and code will be omitted from this string if they were
  150. not specified when constructing this object.
  151. */
  152. char const* what() const
  153. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */
  154. BOOST_OVERRIDE;
  155. /**
  156. Name of the file containing the assertion.
  157. @return File name as specified at construction (or @c "" if no file was
  158. specified).
  159. */
  160. char const* file() const;
  161. /**
  162. Number of the line containing the assertion.
  163. @return Line number as specified at construction (or @c 0 if no line number
  164. was specified).
  165. */
  166. unsigned long line() const;
  167. /**
  168. Text listing the source code of the assertion condition.
  169. @return Assertion condition source code as specified at construction (or
  170. @c "" if no source code text was specified).
  171. */
  172. char const* code() const;
  173. /** @cond */
  174. private:
  175. void init();
  176. char const* file_;
  177. unsigned long line_;
  178. char const* code_;
  179. std::string what_;
  180. /** @endcond */
  181. };
  182. #ifdef BOOST_MSVC
  183. #pragma warning(pop)
  184. #endif
  185. /**
  186. Indicate the kind of operation where the contract assertion failed.
  187. This is passed as a parameter to the assertion failure handler functions.
  188. For example, it might be necessary to know in which operation an assertion
  189. failed to make sure exceptions are never thrown from destructors, not even
  190. when contract failure handlers are programmed by users to throw exceptions
  191. instead of terminating the program.
  192. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure}
  193. */
  194. enum from {
  195. /** Assertion failed when checking contracts for constructors. */
  196. from_constructor,
  197. /** Assertion failed when checking contracts for destructors . */
  198. from_destructor,
  199. /**
  200. Assertion failed when checking contracts for functions (members or not,
  201. public or not).
  202. */
  203. from_function
  204. };
  205. /**
  206. Type of assertion failure handler functions (with @c from parameter).
  207. Assertion failure handler functions specified by this type must be functors
  208. returning @c void and taking a single parameter of type
  209. @RefEnum{boost::contract::from}.
  210. For example, this is used to specify contract failure handlers for class
  211. invariants, preconditions, postconditions, and exception guarantees.
  212. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure}
  213. */
  214. typedef boost::function<void (from)> from_failure_handler;
  215. /**
  216. Type of assertion failure handler functions (without @c from parameter).
  217. Assertion failure handler functions specified by this type must be nullary
  218. functors returning @c void.
  219. For example, this is used to specify contract failure handlers for
  220. implementation checks.
  221. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure}
  222. */
  223. typedef boost::function<void ()> failure_handler;
  224. /** @cond */
  225. namespace exception_ {
  226. // Check failure.
  227. BOOST_CONTRACT_DETAIL_DECLSPEC
  228. failure_handler const& set_check_failure_unlocked(failure_handler const& f)
  229. BOOST_NOEXCEPT_OR_NOTHROW;
  230. BOOST_CONTRACT_DETAIL_DECLSPEC
  231. failure_handler const& set_check_failure_locked(failure_handler const& f)
  232. BOOST_NOEXCEPT_OR_NOTHROW;
  233. BOOST_CONTRACT_DETAIL_DECLSPEC
  234. failure_handler get_check_failure_unlocked() BOOST_NOEXCEPT_OR_NOTHROW;
  235. BOOST_CONTRACT_DETAIL_DECLSPEC
  236. failure_handler get_check_failure_locked() BOOST_NOEXCEPT_OR_NOTHROW;
  237. BOOST_CONTRACT_DETAIL_DECLSPEC
  238. void check_failure_unlocked() /* can throw */;
  239. BOOST_CONTRACT_DETAIL_DECLSPEC
  240. void check_failure_locked() /* can throw */;
  241. // Precondition failure.
  242. BOOST_CONTRACT_DETAIL_DECLSPEC
  243. from_failure_handler const& set_pre_failure_unlocked(
  244. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  245. BOOST_CONTRACT_DETAIL_DECLSPEC
  246. from_failure_handler const& set_pre_failure_locked(
  247. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  248. BOOST_CONTRACT_DETAIL_DECLSPEC
  249. from_failure_handler get_pre_failure_unlocked() BOOST_NOEXCEPT_OR_NOTHROW;
  250. BOOST_CONTRACT_DETAIL_DECLSPEC
  251. from_failure_handler get_pre_failure_locked() BOOST_NOEXCEPT_OR_NOTHROW;
  252. BOOST_CONTRACT_DETAIL_DECLSPEC
  253. void pre_failure_unlocked(from where) /* can throw */;
  254. BOOST_CONTRACT_DETAIL_DECLSPEC
  255. void pre_failure_locked(from where) /* can throw */;
  256. // Postcondition failure.
  257. BOOST_CONTRACT_DETAIL_DECLSPEC
  258. from_failure_handler const& set_post_failure_unlocked(
  259. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  260. BOOST_CONTRACT_DETAIL_DECLSPEC
  261. from_failure_handler const& set_post_failure_locked(
  262. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  263. BOOST_CONTRACT_DETAIL_DECLSPEC
  264. from_failure_handler get_post_failure_unlocked() BOOST_NOEXCEPT_OR_NOTHROW;
  265. BOOST_CONTRACT_DETAIL_DECLSPEC
  266. from_failure_handler get_post_failure_locked() BOOST_NOEXCEPT_OR_NOTHROW;
  267. BOOST_CONTRACT_DETAIL_DECLSPEC
  268. void post_failure_unlocked(from where) /* can throw */;
  269. BOOST_CONTRACT_DETAIL_DECLSPEC
  270. void post_failure_locked(from where) /* can throw */;
  271. // Except failure.
  272. BOOST_CONTRACT_DETAIL_DECLSPEC
  273. from_failure_handler const& set_except_failure_unlocked(
  274. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  275. BOOST_CONTRACT_DETAIL_DECLSPEC
  276. from_failure_handler const& set_except_failure_locked(
  277. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  278. BOOST_CONTRACT_DETAIL_DECLSPEC
  279. from_failure_handler get_except_failure_unlocked()
  280. BOOST_NOEXCEPT_OR_NOTHROW;
  281. BOOST_CONTRACT_DETAIL_DECLSPEC
  282. from_failure_handler get_except_failure_locked() BOOST_NOEXCEPT_OR_NOTHROW;
  283. BOOST_CONTRACT_DETAIL_DECLSPEC
  284. void except_failure_unlocked(from where) /* can throw */;
  285. BOOST_CONTRACT_DETAIL_DECLSPEC
  286. void except_failure_locked(from where) /* can throw */;
  287. // Old-copy failure.
  288. BOOST_CONTRACT_DETAIL_DECLSPEC
  289. from_failure_handler const& set_old_failure_unlocked(
  290. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  291. BOOST_CONTRACT_DETAIL_DECLSPEC
  292. from_failure_handler const& set_old_failure_locked(
  293. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  294. BOOST_CONTRACT_DETAIL_DECLSPEC
  295. from_failure_handler get_old_failure_unlocked() BOOST_NOEXCEPT_OR_NOTHROW;
  296. BOOST_CONTRACT_DETAIL_DECLSPEC
  297. from_failure_handler get_old_failure_locked() BOOST_NOEXCEPT_OR_NOTHROW;
  298. BOOST_CONTRACT_DETAIL_DECLSPEC
  299. void old_failure_unlocked(from where) /* can throw */;
  300. BOOST_CONTRACT_DETAIL_DECLSPEC
  301. void old_failure_locked(from where) /* can throw */;
  302. // Entry invariant failure.
  303. BOOST_CONTRACT_DETAIL_DECLSPEC
  304. from_failure_handler const& set_entry_inv_failure_unlocked(
  305. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  306. BOOST_CONTRACT_DETAIL_DECLSPEC
  307. from_failure_handler const& set_entry_inv_failure_locked(
  308. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  309. BOOST_CONTRACT_DETAIL_DECLSPEC
  310. from_failure_handler get_entry_inv_failure_unlocked()
  311. BOOST_NOEXCEPT_OR_NOTHROW;
  312. BOOST_CONTRACT_DETAIL_DECLSPEC
  313. from_failure_handler get_entry_inv_failure_locked()
  314. BOOST_NOEXCEPT_OR_NOTHROW;
  315. BOOST_CONTRACT_DETAIL_DECLSPEC
  316. void entry_inv_failure_unlocked(from where) /* can throw */;
  317. BOOST_CONTRACT_DETAIL_DECLSPEC
  318. void entry_inv_failure_locked(from where) /* can throw */;
  319. // Exit invariant failure.
  320. BOOST_CONTRACT_DETAIL_DECLSPEC
  321. from_failure_handler const& set_exit_inv_failure_unlocked(
  322. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  323. BOOST_CONTRACT_DETAIL_DECLSPEC
  324. from_failure_handler const&set_exit_inv_failure_locked(
  325. from_failure_handler const& f) BOOST_NOEXCEPT_OR_NOTHROW;
  326. BOOST_CONTRACT_DETAIL_DECLSPEC
  327. from_failure_handler get_exit_inv_failure_unlocked()
  328. BOOST_NOEXCEPT_OR_NOTHROW;
  329. BOOST_CONTRACT_DETAIL_DECLSPEC
  330. from_failure_handler get_exit_inv_failure_locked()
  331. BOOST_NOEXCEPT_OR_NOTHROW;
  332. BOOST_CONTRACT_DETAIL_DECLSPEC
  333. void exit_inv_failure_unlocked(from where) /* can throw */;
  334. BOOST_CONTRACT_DETAIL_DECLSPEC
  335. void exit_inv_failure_locked(from where) /* can throw */;
  336. }
  337. /** @endcond */
  338. } } // namespace
  339. /** @cond */
  340. #ifdef BOOST_CONTRACT_HEADER_ONLY
  341. // NOTE: This header must be included in the middle of this file (because
  342. // its impl depends on both from and assert_failure types). This is not
  343. // ideal, but it is better than splitting this file into multiple
  344. // independent ones because all content in this file is logically related
  345. // from the user prospective.
  346. #include <boost/contract/detail/inlined/core/exception.hpp>
  347. #endif
  348. /** @endcond */
  349. namespace boost { namespace contract {
  350. // Following must be inline for static linkage (no DYN_LINK and no HEADER_ONLY).
  351. /**
  352. Set failure handler for implementation checks.
  353. Set a new failure handler and returns it.
  354. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  355. @param f New failure handler functor to set.
  356. @return Same failure handler functor @p f passed as parameter (e.g., for
  357. concatenating function calls).
  358. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  359. @RefSect{advanced.implementation_checks, Implementation Checks}
  360. */
  361. inline failure_handler const& set_check_failure(failure_handler const& f)
  362. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  363. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  364. return exception_::set_check_failure_locked(f);
  365. #else
  366. return exception_::set_check_failure_unlocked(f);
  367. #endif
  368. }
  369. /**
  370. Return failure handler currently set for implementation checks.
  371. This is often called only internally by this library.
  372. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  373. @return A copy of the failure handler currently set.
  374. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  375. @RefSect{advanced.implementation_checks, Implementation Checks}
  376. */
  377. inline failure_handler get_check_failure()
  378. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  379. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  380. return exception_::get_check_failure_locked();
  381. #else
  382. return exception_::get_check_failure_unlocked();
  383. #endif
  384. }
  385. /**
  386. Call failure handler for implementation checks.
  387. This is often called only internally by this library.
  388. @b Throws: This can throw in case programmers specify a failure handler that
  389. throws exceptions on implementation check failures (not the
  390. default).
  391. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  392. @RefSect{advanced.implementation_checks, Implementation Checks}
  393. */
  394. inline void check_failure() /* can throw */ {
  395. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  396. exception_::check_failure_locked();
  397. #else
  398. exception_::check_failure_unlocked();
  399. #endif
  400. }
  401. /**
  402. Set failure handler for preconditions.
  403. Set a new failure handler and returns it.
  404. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  405. @param f New failure handler functor to set.
  406. @return Same failure handler functor @p f passed as parameter (e.g., for
  407. concatenating function calls).
  408. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  409. @RefSect{tutorial.preconditions, Preconditions}
  410. */
  411. inline from_failure_handler const& set_precondition_failure(from_failure_handler
  412. const& f) /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  413. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  414. return exception_::set_pre_failure_locked(f);
  415. #else
  416. return exception_::set_pre_failure_unlocked(f);
  417. #endif
  418. }
  419. /**
  420. Return failure handler currently set for preconditions.
  421. This is often called only internally by this library.
  422. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  423. @return A copy of the failure handler currently set.
  424. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  425. @RefSect{tutorial.preconditions, Preconditions}
  426. */
  427. inline from_failure_handler get_precondition_failure()
  428. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  429. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  430. return exception_::get_pre_failure_locked();
  431. #else
  432. return exception_::get_pre_failure_unlocked();
  433. #endif
  434. }
  435. /**
  436. Call failure handler for preconditions.
  437. This is often called only internally by this library.
  438. @b Throws: This can throw in case programmers specify a failure handler that
  439. throws exceptions on contract assertion failures (not the default).
  440. @param where Operation that failed the contract assertion (when this function
  441. is called by this library, this parameter will never be
  442. @c from_destructor because destructors do not have
  443. preconditions).
  444. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  445. @RefSect{tutorial.preconditions, Preconditions}
  446. */
  447. inline void precondition_failure(from where) /* can throw */ {
  448. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  449. exception_::pre_failure_locked(where);
  450. #else
  451. exception_::pre_failure_unlocked(where);
  452. #endif
  453. }
  454. /**
  455. Set failure handler for postconditions.
  456. Set a new failure handler and returns it.
  457. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  458. @param f New failure handler functor to set.
  459. @return Same failure handler functor @p f passed as parameter (e.g., for
  460. concatenating function calls).
  461. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  462. @RefSect{tutorial.postconditions, Postconditions}
  463. */
  464. inline from_failure_handler const& set_postcondition_failure(
  465. from_failure_handler const& f
  466. ) /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  467. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  468. return exception_::set_post_failure_locked(f);
  469. #else
  470. return exception_::set_post_failure_unlocked(f);
  471. #endif
  472. }
  473. /**
  474. Return failure handler currently set for postconditions.
  475. This is often called only internally by this library.
  476. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  477. @return A copy of the failure handler currently set.
  478. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  479. @RefSect{tutorial.postconditions, Postconditions}
  480. */
  481. inline from_failure_handler get_postcondition_failure()
  482. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  483. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  484. return exception_::get_post_failure_locked();
  485. #else
  486. return exception_::get_post_failure_unlocked();
  487. #endif
  488. }
  489. /**
  490. Call failure handler for postconditions.
  491. This is often called only internally by this library.
  492. @b Throws: This can throw in case programmers specify a failure handler that
  493. throws exceptions on contract assertion failures (not the default).
  494. @param where Operation that failed the contract assertion (e.g., this might
  495. be useful to program failure handler functors that never throw
  496. from destructors, not even when they are programmed by users to
  497. throw exceptions instead of terminating the program).
  498. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  499. @RefSect{tutorial.postconditions, Postconditions}
  500. */
  501. inline void postcondition_failure(from where) /* can throw */ {
  502. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  503. exception_::post_failure_locked(where);
  504. #else
  505. exception_::post_failure_unlocked(where);
  506. #endif
  507. }
  508. /**
  509. Set failure handler for exception guarantees.
  510. Set a new failure handler and returns it.
  511. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  512. @param f New failure handler functor to set.
  513. @return Same failure handler functor @p f passed as parameter (e.g., for
  514. concatenating function calls).
  515. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  516. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  517. */
  518. inline from_failure_handler const& set_except_failure(from_failure_handler
  519. const& f) /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  520. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  521. return exception_::set_except_failure_locked(f);
  522. #else
  523. return exception_::set_except_failure_unlocked(f);
  524. #endif
  525. }
  526. /**
  527. Return failure handler currently set for exception guarantees.
  528. This is often called only internally by this library.
  529. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  530. @return A copy of the failure handler currently set.
  531. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  532. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  533. */
  534. inline from_failure_handler get_except_failure()
  535. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  536. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  537. return exception_::get_except_failure_locked();
  538. #else
  539. return exception_::get_except_failure_unlocked();
  540. #endif
  541. }
  542. /**
  543. Call failure handler for exception guarantees.
  544. This is often called only internally by this library.
  545. @b Throws: This can throw in case programmers specify a failure handler that
  546. throws exceptions on contract assertion failures (not the default),
  547. however:
  548. @warning When this failure handler is called there is already an active
  549. exception (the one that caused the exception guarantees to be
  550. checked in the first place).
  551. Therefore, programming this failure handler to throw yet another
  552. exception will force C++ to automatically terminate the program.
  553. @param where Operation that failed the contract assertion.
  554. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  555. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  556. */
  557. inline void except_failure(from where) /* can throw */ {
  558. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  559. exception_::except_failure_locked(where);
  560. #else
  561. exception_::except_failure_unlocked(where);
  562. #endif
  563. }
  564. /**
  565. Set failure handler for old values copied at body.
  566. Set a new failure handler and returns it.
  567. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  568. @param f New failure handler functor to set.
  569. @return Same failure handler functor @p f passed as parameter (e.g., for
  570. concatenating function calls).
  571. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  572. @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body}
  573. */
  574. inline from_failure_handler const& set_old_failure(from_failure_handler const&
  575. f) /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  576. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  577. return exception_::set_old_failure_locked(f);
  578. #else
  579. return exception_::set_old_failure_unlocked(f);
  580. #endif
  581. }
  582. /**
  583. Return failure handler currently set for old values copied at body.
  584. This is often called only internally by this library.
  585. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  586. @return A copy of the failure handler currently set.
  587. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  588. @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body}
  589. */
  590. inline from_failure_handler get_old_failure()
  591. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  592. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  593. return exception_::get_old_failure_locked();
  594. #else
  595. return exception_::get_old_failure_unlocked();
  596. #endif
  597. }
  598. /**
  599. Call failure handler for old values copied at body.
  600. This is often called only internally by this library.
  601. @b Throws: This can throw in case programmers specify a failure handler that
  602. throws exceptions on contract assertion failures (not the default).
  603. @param where Operation that failed the old value copy (e.g., this might
  604. be useful to program failure handler functors that never throw
  605. from destructors, not even when they are programmed by users to
  606. throw exceptions instead of terminating the program).
  607. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  608. @RefSect{advanced.old_values_copied_at_body, Old Values Copied at Body}
  609. */
  610. inline void old_failure(from where) /* can throw */ {
  611. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  612. exception_::old_failure_locked(where);
  613. #else
  614. exception_::old_failure_unlocked(where);
  615. #endif
  616. }
  617. /**
  618. Set failure handler for class invariants at entry.
  619. Set a new failure handler and returns it.
  620. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  621. @param f New failure handler functor to set.
  622. @return Same failure handler functor @p f passed as parameter (e.g., for
  623. concatenating function calls).
  624. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  625. @RefSect{tutorial.class_invariants, Class Invariants},
  626. @RefSect{extras.volatile_public_functions,
  627. Volatile Public Functions}
  628. */
  629. inline from_failure_handler const& set_entry_invariant_failure(
  630. from_failure_handler const& f
  631. )/** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  632. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  633. return exception_::set_entry_inv_failure_locked(f);
  634. #else
  635. return exception_::set_entry_inv_failure_unlocked(f);
  636. #endif
  637. }
  638. /**
  639. Return failure handler currently set for class invariants at entry.
  640. This is often called only internally by this library.
  641. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  642. @return A copy of the failure handler currently set.
  643. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  644. @RefSect{tutorial.class_invariants, Class Invariants},
  645. @RefSect{extras.volatile_public_functions,
  646. Volatile Public Functions}
  647. */
  648. inline from_failure_handler get_entry_invariant_failure()
  649. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  650. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  651. return exception_::get_entry_inv_failure_locked();
  652. #else
  653. return exception_::get_entry_inv_failure_unlocked();
  654. #endif
  655. }
  656. /**
  657. Call failure handler for class invariants at entry.
  658. This is often called only internally by this library.
  659. @b Throws: This can throw in case programmers specify a failure handler that
  660. throws exceptions on contract assertion failures (not the default).
  661. @param where Operation that failed the contract assertion (e.g., this might
  662. be useful to program failure handler functors that never throw
  663. from destructors, not even when they are programmed by users to
  664. throw exceptions instead of terminating the program).
  665. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  666. @RefSect{tutorial.class_invariants, Class Invariants},
  667. @RefSect{extras.volatile_public_functions,
  668. Volatile Public Functions}
  669. */
  670. inline void entry_invariant_failure(from where) /* can throw */ {
  671. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  672. return exception_::entry_inv_failure_locked(where);
  673. #else
  674. return exception_::entry_inv_failure_unlocked(where);
  675. #endif
  676. }
  677. /**
  678. Set failure handler for class invariants at exit.
  679. Set a new failure handler and returns it.
  680. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  681. @param f New failure handler functor to set.
  682. @return Same failure handler functor @p f passed as parameter (e.g., for
  683. concatenating function calls).
  684. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  685. @RefSect{tutorial.class_invariants, Class Invariants},
  686. @RefSect{extras.volatile_public_functions,
  687. Volatile Public Functions}
  688. */
  689. inline from_failure_handler const& set_exit_invariant_failure(
  690. from_failure_handler const& f
  691. ) /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  692. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  693. return exception_::set_exit_inv_failure_locked(f);
  694. #else
  695. return exception_::set_exit_inv_failure_unlocked(f);
  696. #endif
  697. }
  698. /**
  699. Return failure handler currently set for class invariants at exit.
  700. This is often called only internally by this library.
  701. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  702. @return A copy of the failure handler currently set.
  703. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  704. @RefSect{tutorial.class_invariants, Class Invariants},
  705. @RefSect{extras.volatile_public_functions,
  706. Volatile Public Functions}
  707. */
  708. inline from_failure_handler get_exit_invariant_failure()
  709. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */ {
  710. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  711. return exception_::get_exit_inv_failure_locked();
  712. #else
  713. return exception_::get_exit_inv_failure_unlocked();
  714. #endif
  715. }
  716. /**
  717. Call failure handler for class invariants at exit.
  718. This is often called only internally by this library.
  719. @b Throws: This can throw in case programmers specify a failure handler that
  720. throws exceptions on contract assertion failures (not the default).
  721. @param where Operation that failed the contract assertion (e.g., this might
  722. be useful to program failure handler functors that never throw
  723. from destructors, not even when they are programmed by users to
  724. throw exceptions instead of terminating the program).
  725. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  726. @RefSect{tutorial.class_invariants, Class Invariants},
  727. @RefSect{extras.volatile_public_functions,
  728. Volatile Public Functions}
  729. */
  730. inline void exit_invariant_failure(from where) /* can throw */ {
  731. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  732. exception_::exit_inv_failure_locked(where);
  733. #else
  734. exception_::exit_inv_failure_unlocked(where);
  735. #endif
  736. }
  737. /**
  738. Set failure handler for class invariants (at both entry and exit).
  739. This is provided for convenience and it is equivalent to call both
  740. @RefFunc{boost::contract::set_entry_invariant_failure} and
  741. @RefFunc{boost::contract::set_exit_invariant_failure} with the same functor
  742. parameter @p f.
  743. @b Throws: This is declared @c noexcept (or @c throw() before C++11).
  744. @param f New failure handler functor to set for both entry and exit invariants.
  745. @return Same failure handler functor @p f passed as parameter (e.g., for
  746. concatenating function calls).
  747. @see @RefSect{advanced.throw_on_failures__and__noexcept__, Throw on Failure},
  748. @RefSect{tutorial.class_invariants, Class Invariants},
  749. @RefSect{extras.volatile_public_functions,
  750. Volatile Public Functions}
  751. */
  752. /** @cond */ BOOST_CONTRACT_DETAIL_DECLSPEC /** @endcond */
  753. from_failure_handler const& set_invariant_failure(from_failure_handler const& f)
  754. /** @cond */ BOOST_NOEXCEPT_OR_NOTHROW /** @endcond */;
  755. } } // namespace
  756. #endif // #include guard