exception.hpp 33 KB

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