exception.hpp 33 KB

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