contract_macro.hpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. #ifndef BOOST_CONTRACT_MACRO_HPP_
  2. #define BOOST_CONTRACT_MACRO_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. Allow to disable contracts to completely remove their compile-time and run-time
  9. overhead.
  10. This header also includes all headers file <c>boost/contract/\*.hpp</c> that are
  11. necessary to use its macros.
  12. Almost all the macros defined in this header file are variadic macros. On
  13. compilers that do not support variadic macros, programmers can manually code
  14. <c>\#ifndef BOOST_CONTRACT_NO_...</c> statements instead (see
  15. @RefSect{extras.disable_contract_compilation__macro_interface_,
  16. Disable Contract Compilation}).
  17. */
  18. // IMPORTANT: Following headers can always be #included (without any #if-guard)
  19. // because they expand to trivial code that does not affect compile-time. These
  20. // headers must always be #included here (without any #if-guard) because they
  21. // define types and macros that are typically left in user code even when
  22. // contracts are disables (these types and macros never affect run-time and
  23. // their definitions are trivial when contracts are disabled so their impact on
  24. // compile-time is negligible).
  25. #include <boost/contract/override.hpp>
  26. #include <boost/contract/base_types.hpp>
  27. #include <boost/contract/core/constructor_precondition.hpp>
  28. #include <boost/contract/core/check_macro.hpp>
  29. #include <boost/contract/core/access.hpp>
  30. #include <boost/contract/core/virtual.hpp>
  31. #include <boost/contract/core/exception.hpp>
  32. #include <boost/contract/core/config.hpp>
  33. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  34. #include <boost/contract/assert.hpp>
  35. #endif
  36. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  37. /**
  38. Program preconditions that can be completely disabled at compile-time.
  39. @c BOOST_CONTRACT_PRECONDITION(f) expands to code equivalent to the
  40. following (note that no code is generated when
  41. @RefMacro{BOOST_CONTRACT_NO_PRECONDITIONS} is defined):
  42. @code
  43. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  44. .precondition(f)
  45. #endif
  46. @endcode
  47. Where:
  48. @arg <c><b>f</b></c> is the nullay functor called by this library to
  49. check preconditions @c f().
  50. Assertions within this functor are usually programmed using
  51. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  52. to this functor indicates a contract assertion failure (and will
  53. result in this library calling
  54. @RefFunc{boost::contract::precondition_failure}).
  55. This functor should capture variables by (constant) value, or better
  56. by (constant) reference (to avoid extra copies).
  57. (This is a variadic macro parameter so it can contain commas not
  58. protected by round parenthesis.)
  59. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  60. Disable Contract Compilation},
  61. @RefSect{tutorial.preconditions, Preconditions}
  62. */
  63. #define BOOST_CONTRACT_PRECONDITION(...)
  64. #elif !defined(BOOST_CONTRACT_NO_PRECONDITIONS)
  65. #define BOOST_CONTRACT_PRECONDITION(...) .precondition(__VA_ARGS__)
  66. #else
  67. #define BOOST_CONTRACT_PRECONDITION(...) /* nothing */
  68. #endif
  69. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  70. /**
  71. Program postconditions that can be completely disabled at compile-time.
  72. @c BOOST_CONTRACT_POSTCONDITION(f) expands to code equivalent to the
  73. following (note that no code is generated when
  74. @RefMacro{BOOST_CONTRACT_NO_POSTCONDITIONS} is defined):
  75. @code
  76. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  77. .postcondition(f)
  78. #endif
  79. @endcode
  80. Where:
  81. @arg <c><b>f</b></c> is the functor called by this library to check
  82. postconditions <c>f(...)</c>.
  83. Assertions within this functor are usually programmed using
  84. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  85. to this functor indicates a contract assertion failure (and will
  86. result in this library calling
  87. @RefFunc{boost::contract::postcondition_failure}).
  88. This functor should capture variables by (constant) references (to
  89. access the values they will have at function exit).
  90. This functor takes the return value (preferably by <c>const&</c>) as
  91. its one single parameter but only for virtual public functions and
  92. public functions overrides, otherwise it takes no parameter.
  93. (This is a variadic macro parameter so it can contain commas not
  94. protected by round parenthesis.)
  95. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  96. Disable Contract Compilation},
  97. @RefSect{tutorial.postconditions, Postconditions}
  98. */
  99. #define BOOST_CONTRACT_POSTCONDITION(...)
  100. #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
  101. #define BOOST_CONTRACT_POSTCONDITION(...) .postcondition(__VA_ARGS__)
  102. #else
  103. #define BOOST_CONTRACT_POSTCONDITION(...) /* nothing */
  104. #endif
  105. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  106. /**
  107. Program exception guarantees that can be completely disabled at
  108. compile-time.
  109. @c BOOST_CONTRACT_EXCEPT(f) expands to code equivalent to the following
  110. (note that no code is generated when @RefMacro{BOOST_CONTRACT_NO_EXCEPTS}
  111. is defined):
  112. @code
  113. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  114. .except(f)
  115. #endif
  116. @endcode
  117. Where:
  118. @arg <c><b>f</b></c> is the nullary functor called by this library to
  119. check exception guarantees @c f().
  120. Assertions within this functor are usually programmed using
  121. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  122. to this functor indicates a contract assertion failure (and will
  123. result in this library calling
  124. @RefFunc{boost::contract::except_failure}).
  125. This functor should capture variables by (constant) references (to
  126. access the values they will have at function exit).
  127. (This is a variadic macro parameter so it can contain commas not
  128. protected by round parenthesis.)
  129. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  130. Disable Contract Compilation},
  131. @RefSect{tutorial.exception_guarantees, Exception Guarantees}
  132. */
  133. #define BOOST_CONTRACT_EXCEPT(...)
  134. #elif !defined(BOOST_CONTRACT_NO_EXCEPTS)
  135. #define BOOST_CONTRACT_EXCEPT(...) .except(__VA_ARGS__)
  136. #else
  137. #define BOOST_CONTRACT_EXCEPT(...) /* nothing */
  138. #endif
  139. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  140. /**
  141. Program old value copies at body that can be completely disabled at
  142. compile-time.
  143. @c BOOST_CONTRACT_OLD(f) expands to code equivalent to the following (note
  144. that no code is generated when @RefMacro{BOOST_CONTRACT_NO_OLDS} is
  145. defined):
  146. @code
  147. #ifndef BOOST_CONTRACT_NO_OLDS
  148. .old(f)
  149. #endif
  150. @endcode
  151. Where:
  152. @arg <c><b>f</b></c> is the nullary functor called by this library
  153. @c f() to assign old value copies just before the body is execute
  154. but after entry invariants (when they apply) and preconditions are
  155. checked.
  156. Old value pointers within this functor call are usually assigned
  157. using @RefMacro{BOOST_CONTRACT_OLDOF}.
  158. Any exception thrown by a call to this functor will result in
  159. this library calling @RefFunc{boost::contract::old_failure} (because
  160. old values could not be copied to check postconditions and exception
  161. guarantees).
  162. This functor should capture old value pointers by references so they
  163. can be assigned (all other variables needed to evaluate old value
  164. expressions can be captured by (constant) value, or better by
  165. (constant) reference to avoid extra copies).
  166. (This is a variadic macro parameter so it can contain commas not
  167. protected by round parenthesis.)
  168. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  169. Disable Contract Compilation},
  170. @RefSect{advanced.old_value_copies_at_body,
  171. Old Value Copies at Body}
  172. */
  173. #define BOOST_CONTRACT_OLD(...)
  174. /**
  175. Program old values that can be completely disabled at compile-time and
  176. require the old value type to be copyable.
  177. This is used to program old value copies for copyable types:
  178. @code
  179. class u {
  180. public:
  181. void f(...) {
  182. BOOST_CONTRACT_OLD_PTR(old_type_a)(old_var_a);
  183. BOOST_CONTRACT_OLD_PTR(old_type_b)(old_var_b, old_expr_b);
  184. BOOST_CONTRACT_PUBLIC_FUNCTION(this)
  185. ...
  186. BOOST_CONTRACT_OLD([&] {
  187. old_var_a = BOOST_CONTRACT_OLDOF(old_expr_a);
  188. ...
  189. })
  190. ...
  191. ;
  192. ... // Function body.
  193. }
  194. virtual void g(..., boost::contract::virtual_* v = 0) {
  195. BOOST_CONTRACT_OLD_PTR(old_type_a)(old_var_a);
  196. BOOST_CONTRACT_OLD_PTR(old_type_b)(v, old_var_b, old_expr_b);
  197. BOOST_CONTRACT_PUBLIC_FUNCTION(v, this)
  198. ...
  199. BOOST_CONTRACT_OLD([&] {
  200. old_var_a = BOOST_CONTRACT_OLDOF(v, old_expr_a);
  201. ...
  202. })
  203. ...
  204. ;
  205. ... // Function body.
  206. }
  207. ...
  208. };
  209. @endcode
  210. This is an overloaded variadic macro and it can be used in the following
  211. different ways (note that no code is generated when
  212. @RefMacro{BOOST_CONTRACT_NO_OLDS} is defined).
  213. 1\. <c>BOOST_CONTRACT_OLD_PTR(old_type)(old_var)</c> expands to code
  214. equivalent to the following (this leaves the old value pointer null):
  215. @code
  216. #ifndef BOOST_CONTRACT_NO_OLDS
  217. // This declaration does not need to use `v`.
  218. boost::contract::old_ptr<old_type> old_var
  219. #endif
  220. @endcode
  221. 2\. <c>BOOST_CONTRACT_OLD_PTR(old_type)(old_var, old_expr)</c> expands to
  222. code equivalent to the following (this initializes the pointer to the
  223. old value copy, but not to be used for virtual public functions and
  224. public function overrides):
  225. @code
  226. #ifndef BOOST_CONTRACT_NO_OLDS
  227. boost::contract::old_ptr<old_type> old_var =
  228. BOOST_CONTRACT_OLDOF(old_expr)
  229. #endif
  230. @endcode
  231. 3\. <c>BOOST_CONTRACT_OLD_PTR(old_type)(v, old_var, old_expr)</c> expands to
  232. code equivalent to the following (this initializes the pointer to the
  233. old value copy for virtual public functions and public function
  234. overrides):
  235. @code
  236. #ifndef BOOST_CONTRACT_NO_OLDS
  237. boost::contract::old_ptr<old_type> old_var =
  238. BOOST_CONTRACT_OLDOF(v, old_expr)
  239. #endif
  240. @endcode
  241. Where:
  242. @arg <c><b>old_type</b></c> is the type of the pointed old value.
  243. This type must be copyable (i.e.,
  244. <c>boost::contract::is_old_value_copyable<old_type>::value</c> is
  245. @c true), otherwise this pointer will always be null and this
  246. library will generate a compile-time error when the pointer is
  247. dereferenced (see @RefMacro{BOOST_CONTRACT_OLD_PTR_IF_COPYABLE}).
  248. (This is a variadic macro parameter so it can contain commas not
  249. protected by round parenthesis.)
  250. @arg <c><b>v</b></c> is the extra parameter of type
  251. @RefClass{boost::contract::virtual_}<c>*</c> and default value @c 0
  252. from the enclosing virtual public function or public function
  253. override declaring the contract.
  254. (This is not a variadic macro parameter.)
  255. @arg <c><b>old_var</b></c> is the name of the old value pointer variable.
  256. (This is not a variadic macro parameter but it should never contain
  257. commas because it is an identifier.)
  258. @arg <c><b>old_expr</b></c> is the expression to be evaluated and copied
  259. in the old value pointer.
  260. (This is not a variadic macro parameter so any comma it might
  261. contain must be protected by round parenthesis,
  262. <c>BOOST_CONTRACT_OLD_PTR(old_type)(v, old_var, (old_expr))</c>
  263. will always work.)
  264. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  265. Disable Contract Compilation},
  266. @RefSect{tutorial.old_values, Old Values}
  267. */
  268. #define BOOST_CONTRACT_OLD_PTR(...)
  269. /**
  270. Program old values that can be completely disabled at compile-time and do
  271. not require the old value type to be copyable.
  272. This is used to program old value copies for types that might or might not
  273. be copyable:
  274. @code
  275. template<typename T> // Type `T` might or not be copyable.
  276. class u {
  277. public:
  278. void f(...) {
  279. BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type_a)(old_var_a);
  280. BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type_b)(old_var_b,
  281. old_expr_b);
  282. BOOST_CONTRACT_PUBLIC_FUNCTION(this)
  283. ...
  284. BOOST_CONTRACT_OLD([&] {
  285. old_var_a = BOOST_CONTRACT_OLDOF(old_expr_a);
  286. ...
  287. })
  288. ... // In postconditions or exception guarantees:
  289. if(old_var_a) ... // Always null for non-copyable types.
  290. if(old_var_b) ... // Always null for non-copyable types.
  291. ...
  292. ;
  293. ... // Function body.
  294. }
  295. virtual void g(..., boost::contract::virtual_* v = 0) {
  296. BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type_a)(old_var_a);
  297. BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type_b)(v, old_var_b,
  298. old_expr_b);
  299. BOOST_CONTRACT_PUBLIC_FUNCTION(v, this)
  300. ...
  301. BOOST_CONTRACT_OLD([&] {
  302. old_var_a = BOOST_CONTRACT_OLDOF(v, old_expr_a);
  303. ...
  304. })
  305. ... // In postconditions or exception guarantees:
  306. if(old_var_a) ... // Always null for non-copyable types.
  307. if(old_var_b) ... // Always null for non-copyable types.
  308. ...
  309. ;
  310. ... // Function body.
  311. }
  312. ...
  313. };
  314. @endcode
  315. This is an overloaded variadic macro and it can be used in the following
  316. different ways (note that no code is generated when
  317. @RefMacro{BOOST_CONTRACT_NO_OLDS} is defined).
  318. 1\. <c>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(old_var)</c> expands to
  319. code equivalent to the following (this leaves the old value pointer
  320. null):
  321. @code
  322. #ifndef BOOST_CONTRACT_NO_OLDS
  323. // This declaration does not need to use `v`.
  324. boost::contract::old_ptr_if_copyable<old_type> old_var
  325. #endif
  326. @endcode
  327. 2\. <c>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(old_var, old_expr)</c>
  328. expands to code equivalent to the following (this initializes the
  329. pointer to the old value copy, but not to be used for virtual public
  330. functions and public function overrides):
  331. @code
  332. #ifndef BOOST_CONTRACT_NO_OLDS
  333. boost::contract::old_ptr_if_copyable<old_type> old_var =
  334. BOOST_CONTRACT_OLDOF(old_expr)
  335. #endif
  336. @endcode
  337. 3\. <c>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(v, old_var,
  338. old_expr)</c> expands to code equivalent to the following (this
  339. initializes the pointer to the old value copy for virtual public
  340. functions and public function overrides):
  341. @code
  342. #ifndef BOOST_CONTRACT_NO_OLDS
  343. boost::contract::old_ptr_if_copyable<old_type> old_var =
  344. BOOST_CONTRACT_OLDOF(v, old_expr)
  345. #endif
  346. @endcode
  347. Where:
  348. @arg <c><b>old_type</b></c> is the type of the pointed old value.
  349. If this type is not copyable (i.e.,
  350. <c>boost::contract::is_old_value_copyable<old_type>::value</c> is
  351. @c false), this pointer will always be null, but this library will
  352. not generate a compile-time error when this pointer is dereferenced
  353. (see @RefMacro{BOOST_CONTRACT_OLD_PTR}).
  354. (This is a variadic macro parameter so it can contain commas not
  355. protected by round parenthesis.)
  356. @arg <c><b>v</b></c> is the extra parameter of type
  357. @RefClass{boost::contract::virtual_}<c>*</c> and default value @c 0
  358. from the enclosing virtual public function or public function
  359. override declaring the contract.
  360. (This is not a variadic macro parameter.)
  361. @arg <c><b>old_var</b></c> is the name of the old value pointer variable.
  362. (This is not a variadic macro parameter but it should never contain
  363. commas because it is an identifier.)
  364. @arg <c><b>old_expr</b></c> is the expression to be evaluated and copied
  365. in the old value pointer.
  366. (This is not a variadic macro parameter so any comma it might
  367. contain must be protected by round parenthesis,
  368. <c>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(v, old_var,
  369. (old_expr))</c> will always work.)
  370. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  371. Disable Contract Compilation},
  372. @RefSect{extras.old_value_requirements__templates_,
  373. Old Value Requirements}
  374. */
  375. #define BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(...)
  376. #elif !defined(BOOST_CONTRACT_NO_OLDS)
  377. #include <boost/contract/old.hpp>
  378. #include <boost/preprocessor/facilities/overload.hpp>
  379. #include <boost/preprocessor/facilities/empty.hpp>
  380. #include <boost/preprocessor/cat.hpp>
  381. /* PRIVATE */
  382. #define BOOST_CONTRACT_OLD_VAR_1(ptr) \
  383. ptr
  384. #define BOOST_CONTRACT_OLD_VAR_2(ptr, expr) \
  385. ptr = BOOST_CONTRACT_OLDOF(expr)
  386. #define BOOST_CONTRACT_OLD_VAR_3(v, ptr, expr) \
  387. ptr = BOOST_CONTRACT_OLDOF(v, expr)
  388. #define BOOST_CONTRACT_OLD_VAR_(...) \
  389. BOOST_PP_CAT(BOOST_PP_OVERLOAD(BOOST_CONTRACT_OLD_VAR_, __VA_ARGS__) \
  390. (__VA_ARGS__), BOOST_PP_EMPTY())
  391. /* PUBLIC */
  392. #define BOOST_CONTRACT_OLD(...) .old(__VA_ARGS__)
  393. #define BOOST_CONTRACT_OLD_PTR(...) \
  394. boost::contract::old_ptr< __VA_ARGS__ > \
  395. BOOST_CONTRACT_OLD_VAR_
  396. #define BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(...) \
  397. boost::contract::old_ptr_if_copyable< __VA_ARGS__ > \
  398. BOOST_CONTRACT_OLD_VAR_
  399. #else
  400. #include <boost/preprocessor/tuple/eat.hpp>
  401. #define BOOST_CONTRACT_OLD(...) /* nothing */
  402. #define BOOST_CONTRACT_OLD_PTR(...) BOOST_PP_TUPLE_EAT(0)
  403. #define BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(...) BOOST_PP_TUPLE_EAT(0)
  404. #endif
  405. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  406. /**
  407. Program (constant) class invariants that can be completely disabled at
  408. compile-time.
  409. @c BOOST_CONTRACT_INVARIANT({ ... }) expands to code equivalent to the
  410. following (note that no code is generated when
  411. @RefMacro{BOOST_CONTRACT_NO_INVARIANTS} is defined):
  412. @code
  413. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  414. void BOOST_CONTRACT_INVARIANT_FUNC() const {
  415. ...
  416. }
  417. #endif
  418. @endcode
  419. Where:
  420. @arg <b>{ ... }</b> is the definition of the function that checks class
  421. invariants for public functions that are not static and not volatile
  422. (see @RefMacro{BOOST_CONTRACT_STATIC_INVARIANT} and
  423. @RefMacro{BOOST_CONTRACT_INVARIANT_VOLATILE}).
  424. The curly parenthesis are mandatory.
  425. Assertions within this function are usually programmed using
  426. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  427. to this function indicates a contract assertion failure (and will
  428. result in this library calling either
  429. @RefFunc{boost::contract::entry_invariant_failure} or
  430. @RefFunc{boost::contract::exit_invariant_failure}).
  431. (This is a variadic macro parameter so it can contain commas not
  432. protected by round parenthesis.)
  433. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  434. Disable Contract Compilation},
  435. @RefSect{tutorial.class_invariants, Class Invariants}
  436. */
  437. #define BOOST_CONTRACT_INVARIANT(...)
  438. /**
  439. Program volatile class invariants that can be completely disabled at
  440. compile-time.
  441. @c BOOST_CONTRACT_INVARIANT_VOLATILE({ ... }) expands to code equivalent to
  442. the following (note that no code is generated when
  443. @RefMacro{BOOST_CONTRACT_NO_INVARIANTS} is defined):
  444. @code
  445. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  446. void BOOST_CONTRACT_INVARIANT_FUNC() const volatile {
  447. ...
  448. }
  449. #endif
  450. @endcode
  451. Where:
  452. @arg <b>{ ... }</b> is the definition of the function that checks class
  453. invariants for volatile public functions
  454. (see @RefMacro{BOOST_CONTRACT_INVARIANT} and
  455. @RefMacro{BOOST_CONTRACT_STATIC_INVARIANT}).
  456. The curly parenthesis are mandatory.
  457. Assertions within this function are usually programmed using
  458. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  459. to this function indicates a contract assertion failure (and will
  460. result in this library calling either
  461. @RefFunc{boost::contract::entry_invariant_failure} or
  462. @RefFunc{boost::contract::exit_invariant_failure}).
  463. (This is a variadic macro parameter so it can contain commas not
  464. protected by round parenthesis.)
  465. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  466. Disable Contract Compilation},
  467. @RefSect{extras.volatile_public_functions,
  468. Volatile Public Functions}
  469. */
  470. #define BOOST_CONTRACT_INVARIANT_VOLATILE(...)
  471. /**
  472. Program static class invariants that can be completely disabled at
  473. compile-time.
  474. @c BOOST_CONTRACT_STATIC_INVARIANT({ ... }) expands to code equivalent to
  475. the following (note that no code is generated when
  476. @RefMacro{BOOST_CONTRACT_NO_INVARIANTS} is defined):
  477. @code
  478. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  479. static void BOOST_CONTRACT_STATIC_INVARIANT_FUNC() {
  480. ...
  481. }
  482. #endif
  483. @endcode
  484. Where:
  485. @arg <b>{ ... }</b> is the definition of the function that checks class
  486. invariants for static public functions
  487. (see @RefMacro{BOOST_CONTRACT_INVARIANT} and
  488. @RefMacro{BOOST_CONTRACT_INVARIANT_VOLATILE}).
  489. The curly parenthesis are mandatory.
  490. Assertions within this function are usually programmed using
  491. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  492. to this function indicates a contract assertion failure (and will
  493. result in this library calling either
  494. @RefFunc{boost::contract::entry_invariant_failure} or
  495. @RefFunc{boost::contract::exit_invariant_failure}).
  496. (This is a variadic macro parameter so it can contain commas not
  497. protected by round parenthesis.)
  498. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  499. Disable Contract Compilation},
  500. @RefSect{tutorial.class_invariants, Class Invariants}
  501. */
  502. #define BOOST_CONTRACT_STATIC_INVARIANT(...)
  503. #elif !defined(BOOST_CONTRACT_NO_INVARIANTS)
  504. #include <boost/contract/core/config.hpp>
  505. #define BOOST_CONTRACT_INVARIANT(...) \
  506. void BOOST_CONTRACT_INVARIANT_FUNC() const __VA_ARGS__
  507. #define BOOST_CONTRACT_INVARIANT_VOLATILE(...) \
  508. void BOOST_CONTRACT_INVARIANT_FUNC() const volatile __VA_ARGS__
  509. #define BOOST_CONTRACT_STATIC_INVARIANT(...) \
  510. static void BOOST_CONTRACT_STATIC_INVARIANT_FUNC() __VA_ARGS__
  511. #else
  512. #define BOOST_CONTRACT_INVARIANT(...) /* nothing */
  513. #define BOOST_CONTRACT_INVARIANT_VOLATILE(...) /* nothing */
  514. #define BOOST_CONTRACT_STATIC_INVARIANT(...) /* nothing */
  515. #endif
  516. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  517. /**
  518. Program contracts that can be completely disabled at compile-time for
  519. constructors.
  520. This is used together with @RefMacro{BOOST_CONTRACT_POSTCONDITION},
  521. @RefMacro{BOOST_CONTRACT_EXCEPT}, and @RefMacro{BOOST_CONTRACT_OLD} to
  522. specify postconditions, exception guarantees, and old value copies at body
  523. that can be completely disabled at compile-time for constructors (see
  524. @RefMacro{BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION} to specify preconditions
  525. for constructors):
  526. @code
  527. class u {
  528. friend class boost::contract::access;
  529. BOOST_CONTRACT_INVARIANT({ // Optional (as for static and volatile).
  530. BOOST_CONTRACT_ASSERT(...);
  531. ...
  532. })
  533. public:
  534. u(...) {
  535. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  536. BOOST_CONTRACT_CONSTRUCTOR(this)
  537. // No `PRECONDITION` (use `CONSTRUCTOR_PRECONDITION` if needed).
  538. BOOST_CONTRACT_OLD([&] { // Optional.
  539. old_var = BOOST_CONTRACT_OLDOF(old_epxr);
  540. ...
  541. })
  542. BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
  543. BOOST_CONTRACT_ASSERT(...);
  544. ...
  545. })
  546. BOOST_CONTRACT_EXCEPT([&] { // Optional.
  547. BOOST_CONTRACT_ASSERT(...);
  548. ...
  549. })
  550. ;
  551. ... // Constructor body.
  552. }
  553. ...
  554. };
  555. @endcode
  556. For optimization, this can be omitted for constructors that do not have
  557. postconditions and exception guarantees, within classes that have no
  558. invariants.
  559. @c BOOST_CONTRACT_CONSTRUCTOR(obj) expands to code equivalent to the
  560. following (note that no code is generated when
  561. @RefMacro{BOOST_CONTRACT_NO_CONSTRUCTORS} is defined):
  562. @code
  563. #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
  564. boost::contract::check internal_var =
  565. boost::contract::constructor(obj)
  566. #endif
  567. @endcode
  568. Where:
  569. @arg <c><b>obj</b></c> is the object @c this from the scope of the
  570. enclosing constructor declaring the contract.
  571. Constructors check all class invariants, including static and
  572. volatile invariants (see @RefMacro{BOOST_CONTRACT_INVARIANT},
  573. @RefMacro{BOOST_CONTRACT_STATIC_INVARIANT}, and
  574. @RefMacro{BOOST_CONTRACT_INVARIANT_VOLATILE}).
  575. (This is a variadic macro parameter so it can contain commas not
  576. protected by round parenthesis.)
  577. @arg <c><b>internal_var</b></c> is a variable name internally generated
  578. by this library (this name is unique but only on different line
  579. numbers so this macro cannot be expanded multiple times on the same
  580. line).
  581. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  582. Disable Contract Compilation},
  583. @RefSect{tutorial.constructors, Constructors}
  584. */
  585. #define BOOST_CONTRACT_CONSTRUCTOR(...)
  586. #elif !defined(BOOST_CONTRACT_NO_CONSTRUCTORS)
  587. #include <boost/contract/constructor.hpp>
  588. #include <boost/contract/check.hpp>
  589. #include <boost/contract/detail/name.hpp>
  590. #define BOOST_CONTRACT_CONSTRUCTOR(...) \
  591. boost::contract::check BOOST_CONTRACT_DETAIL_NAME2(c, __LINE__) = \
  592. boost::contract::constructor(__VA_ARGS__)
  593. #else
  594. #define BOOST_CONTRACT_CONSTRUCTOR(...) /* nothing */
  595. #endif
  596. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  597. /**
  598. Program preconditions that can be disabled at compile-time for constructors.
  599. This is used together with @RefMacro{BOOST_CONTRACT_CONSTRUCTOR} to specify
  600. contracts for constructors.
  601. Constructors that do not have preconditions do not use this macro.
  602. When at least one of the class constructors uses this macro,
  603. @RefClass{boost::contract::constructor_precondition} must be the first and
  604. private base class of the class declaring the constructor for which
  605. preconditions are programmed:
  606. @code
  607. class u
  608. #define BASES private boost::contract::constructor_precondition<u>, \
  609. public b
  610. : BASES
  611. {
  612. ...
  613. #undef BASES
  614. public:
  615. explicit u(unsigned x) :
  616. BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(u)([&] {
  617. BOOST_CONTRACT_ASSERT(x != 0);
  618. }),
  619. b(1.0 / float(x))
  620. {
  621. ...
  622. }
  623. ...
  624. };
  625. @endcode
  626. <c>BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(class_type)(f)</c> expands
  627. to code equivalent to the following (note that when
  628. @RefMacro{BOOST_CONTRACT_NO_PRECONDITIONS} is defined, this macro trivially
  629. expands to a default constructor call that is internally implemented to do
  630. nothing so this should have minimal to no overhead):
  631. @code
  632. // Guarded only by NO_PRECONDITIONS (and not also by NO_CONSTRUCTORS)
  633. // because for constructor's preconditions (not for postconditions, etc.).
  634. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  635. boost::contract::constructor_precondition<class_type>(f)
  636. #else
  637. // No-op call (likely optimized away, minimal to no overhead).
  638. boost::contract::constructor_precondition<class_type>()
  639. #endif
  640. @endcode
  641. Where:
  642. @arg <c><b>class_type</b></c> is the type of the class containing the
  643. constructor for which preconditions are being programmed.
  644. (This is a variadic macro parameter so it can contain commas not
  645. protected by round parenthesis.)
  646. @arg <c><b>f</b></c> is the nullary functor called by this library to
  647. check constructor preconditions @c f().
  648. Assertions within this functor call are usually programmed using
  649. @RefMacro{BOOST_CONTRACT_ASSERT}, but any exception thrown by a call
  650. to this functor indicates a contract failure (and will result in
  651. this library calling
  652. @RefFunc{boost::contract::precondition_failure}).
  653. This functor should capture variables by (constant) value, or better
  654. by (constant) reference to avoid extra copies.
  655. (This is a variadic macro parameter so it can contain commas not
  656. protected by round parenthesis.)
  657. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  658. Disable Contract Compilation},
  659. @RefSect{tutorial.constructors, Constructors}
  660. */
  661. #define BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(...)
  662. #elif !defined(BOOST_CONTRACT_NO_PRECONDITIONS) // Not NO_CONSTRUCTORS here.
  663. // constructor_precondition.hpp already #included at top.
  664. #define BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(...) \
  665. boost::contract::constructor_precondition< __VA_ARGS__ >
  666. #else
  667. #include <boost/preprocessor/tuple/eat.hpp>
  668. // constructor_precondition.hpp always #included at top of this file.
  669. #define BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(...) \
  670. /* always use default ctor (i.e., do nothing) */ \
  671. boost::contract::constructor_precondition< __VA_ARGS__ >() \
  672. BOOST_PP_TUPLE_EAT(0)
  673. #endif
  674. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  675. /**
  676. Program contracts that can be completely disabled at compile-time for
  677. destructors.
  678. This is used together with @RefMacro{BOOST_CONTRACT_POSTCONDITION},
  679. @RefMacro{BOOST_CONTRACT_EXCEPT}, and @RefMacro{BOOST_CONTRACT_OLD} to
  680. specify postconditions, exception guarantees, and old value copies at body
  681. that can be completely disabled at compile-time for destructors (destructors
  682. cannot have preconditions, see
  683. @RefSect{contract_programming_overview.destructor_calls, Destructor Calls}):
  684. @code
  685. class u {
  686. friend class boost::contract::access;
  687. BOOST_CONTRACT_INVARIANT({ // Optional (as for static and volatile).
  688. BOOST_CONTRACT_ASSERT(...);
  689. ...
  690. })
  691. public:
  692. ~u() {
  693. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  694. BOOST_CONTRACT_DESTRUCTOR(this)
  695. // No `PRECONDITION` (destructors have no preconditions).
  696. BOOST_CONTRACT_OLD([&] { // Optional.
  697. old_var = BOOST_CONTRACT_OLDOF(old_expr);
  698. ...
  699. })
  700. BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
  701. BOOST_CONTRACT_ASSERT(...);
  702. ...
  703. })
  704. BOOST_CONTRACT_EXCEPT([&] { // Optional.
  705. BOOST_CONTRACT_ASSERT(...);
  706. ...
  707. })
  708. ;
  709. ... // Destructor body.
  710. }
  711. ...
  712. };
  713. @endcode
  714. For optimization, this can be omitted for destructors that do not have
  715. postconditions and exception guarantees, within classes that have no
  716. invariants.
  717. @c BOOST_CONTRACT_DESTRUCTOR(obj) expands to code equivalent to the
  718. following (note that no code is generated when
  719. @RefMacro{BOOST_CONTRACT_NO_DESTRUCTORS} is defined):
  720. @code
  721. #ifndef BOOST_CONTRACT_NO_DESTRUCTORS
  722. boost::contract::check internal_var =
  723. boost::contract::destructor(obj)
  724. #endif
  725. @endcode
  726. Where:
  727. @arg <c><b>obj</b></c> is the object @c this from the scope of the
  728. enclosing destructor declaring the contract.
  729. Destructors check all class invariants, including static and
  730. volatile invariants (see @RefSect{tutorial.class_invariants,
  731. Class Invariants} and
  732. @RefSect{extras.volatile_public_functions,
  733. Volatile Public Functions}).
  734. (This is a variadic macro parameter so it can contain commas not
  735. protected by round parenthesis.)
  736. @arg <c><b>internal_var</b></c> is a variable name internally generated
  737. by this library (this name is unique but only on different line
  738. numbers so this macro cannot be expanded multiple times on the same
  739. line).
  740. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  741. Disable Contract Compilation},
  742. @RefSect{tutorial.destructors, Destructors}
  743. */
  744. #define BOOST_CONTRACT_DESTRUCTOR(...)
  745. #elif !defined(BOOST_CONTRACT_NO_DESTRUCTORS)
  746. #include <boost/contract/destructor.hpp>
  747. #include <boost/contract/check.hpp>
  748. #include <boost/contract/detail/name.hpp>
  749. #define BOOST_CONTRACT_DESTRUCTOR(...) \
  750. boost::contract::check BOOST_CONTRACT_DETAIL_NAME2(c, __LINE__) = \
  751. boost::contract::destructor(__VA_ARGS__)
  752. #else
  753. #define BOOST_CONTRACT_DESTRUCTOR(...) /* nothing */
  754. #endif
  755. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  756. /**
  757. Program contracts that can be completely disabled at compile-time for
  758. (non-public) functions.
  759. This is used together with @RefMacro{BOOST_CONTRACT_PRECONDITION},
  760. @RefMacro{BOOST_CONTRACT_POSTCONDITION}, @RefMacro{BOOST_CONTRACT_EXCEPT},
  761. and @RefMacro{BOOST_CONTRACT_OLD} to specify preconditions, postconditions,
  762. exception guarantees, and old value copies at body that can be completely
  763. disabled at compile-time for (non-public) functions:
  764. @code
  765. void f(...) {
  766. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  767. BOOST_CONTRACT_FUNCTION()
  768. BOOST_CONTRACT_PRECONDITION([&] { // Optional.
  769. BOOST_CONTRACT_ASSERT(...);
  770. ...
  771. })
  772. BOOST_CONTRACT_OLD([&] { // Optional.
  773. old_var = BOOST_CONTRACT_OLDOF(old_expr);
  774. ...
  775. })
  776. BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
  777. BOOST_CONTRACT_ASSERT(...);
  778. ...
  779. })
  780. BOOST_CONTRACT_EXCEPT([&] { // Optional.
  781. BOOST_CONTRACT_ASSERT(...);
  782. ...
  783. })
  784. ;
  785. ... // Function body.
  786. }
  787. @endcode
  788. This can be used to program contracts for non-member functions but also for
  789. private and protected functions, lambda functions, loops, arbitrary blocks
  790. of code, etc.
  791. For optimization, this can be omitted for code that does not have
  792. preconditions, postconditions, and exception guarantees.
  793. @c BOOST_CONTRACT_FUNCTION() expands to code equivalent to the following
  794. (note that no code is generated when @RefMacro{BOOST_CONTRACT_NO_FUNCTIONS}
  795. is defined):
  796. @code
  797. #ifndef BOOST_CONTRACT_NO_FUNCTIONS
  798. boost::contract::check internal_var =
  799. boost::contract::function()
  800. #endif
  801. @endcode
  802. Where:
  803. @arg <c><b>internal_var</b></c> is a variable name internally generated
  804. by this library (this name is unique but only on different line
  805. numbers so this macro cannot be expanded multiple times on the same
  806. line).
  807. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  808. Disable Contract Compilation},
  809. @RefSect{tutorial.non_member_functions, Non-Member Functions},
  810. @RefSect{advanced.private_and_protected_functions,
  811. Private and Protected Functions},
  812. @RefSect{advanced.lambdas__loops__code_blocks__and__constexpr__,
  813. Lambdas\, Loops\, Code Blocks}
  814. */
  815. #define BOOST_CONTRACT_FUNCTION()
  816. #elif !defined(BOOST_CONTRACT_NO_FUNCTIONS)
  817. #include <boost/contract/function.hpp>
  818. #include <boost/contract/check.hpp>
  819. #include <boost/contract/detail/name.hpp>
  820. #define BOOST_CONTRACT_FUNCTION() \
  821. boost::contract::check BOOST_CONTRACT_DETAIL_NAME2(c, __LINE__) = \
  822. boost::contract::function()
  823. #else
  824. #include <boost/preprocessor/facilities/empty.hpp>
  825. #define BOOST_CONTRACT_FUNCTION() /* nothing */
  826. #endif
  827. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  828. /**
  829. Program contracts that can be completely disabled at compile-time for static
  830. public functions.
  831. This is used together with @RefMacro{BOOST_CONTRACT_PRECONDITION},
  832. @RefMacro{BOOST_CONTRACT_POSTCONDITION}, @RefMacro{BOOST_CONTRACT_EXCEPT},
  833. and @RefMacro{BOOST_CONTRACT_OLD} to specify preconditions, postconditions,
  834. exception guarantees, and old value copies at body that can be completely
  835. disabled at compile-time for static public functions:
  836. @code
  837. class u {
  838. friend class boost::contract::access;
  839. BOOST_CONTRACT_STATIC_INVARIANT({ // Optional (as for non-static).
  840. BOOST_CONTRACT_ASSERT(...);
  841. ...
  842. })
  843. public:
  844. static void f(...) {
  845. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  846. BOOST_CONTRACT_PUBLIC_FUNCTION(u)
  847. BOOST_CONTRACT_PRECONDITION([&] { // Optional.
  848. BOOST_CONTRACT_ASSERT(...);
  849. ...
  850. })
  851. BOOST_CONTRACT_OLD([&] { // Optional.
  852. old_var = BOOST_CONTRACT_OLDOF(old_expr);
  853. ...
  854. })
  855. BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
  856. BOOST_CONTRACT_ASSERT(...);
  857. ...
  858. })
  859. BOOST_CONTRACT_EXCEPT([&] { // Optional.
  860. BOOST_CONTRACT_ASSERT(...);
  861. ...
  862. })
  863. ;
  864. ... // Function body.
  865. }
  866. ...
  867. };
  868. @endcode
  869. For optimization, this can be omitted for static public functions that do
  870. not have preconditions, postconditions and exception guarantees, within
  871. classes that have no static invariants.
  872. @c BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(class_type) expands to code
  873. equivalent to the following (note that no code is generated when
  874. @RefMacro{BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS} is defined):
  875. @code
  876. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  877. boost::contract::check internal_var =
  878. boost::contract::public_function<class_type>()
  879. #endif
  880. @endcode
  881. Where:
  882. @arg <c><b>class_type</b></c> is the type of the class containing the
  883. static public function declaring the contract.
  884. (This is a variadic macro parameter so it can contain commas not
  885. protected by round parenthesis.)
  886. @arg <c><b>internal_var</b></c> is a variable name internally generated
  887. by this library (this name is unique but only on different line
  888. numbers so this macro cannot be expanded multiple times on the same
  889. line).
  890. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  891. Disable Contract Compilation},
  892. @RefSect{tutorial.static_public_functions, Static Public Functions}
  893. */
  894. #define BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(...)
  895. /**
  896. Program contracts that can be completely disabled at compile-time for
  897. non-static public functions that do not override.
  898. This is used together with @RefMacro{BOOST_CONTRACT_PRECONDITION},
  899. @RefMacro{BOOST_CONTRACT_POSTCONDITION}, @RefMacro{BOOST_CONTRACT_EXCEPT},
  900. and @RefMacro{BOOST_CONTRACT_OLD} to specify preconditions, postconditions,
  901. exception guarantees, and old value copies at body that can be completely
  902. disabled at compile-time for non-static public functions (virtual or not,
  903. void or not) that do not override:
  904. @code
  905. class u {
  906. friend class boost::contract::access;
  907. BOOST_CONTRACT_INVARIANT({ // Optional (as for static and volatile).
  908. BOOST_CONTRACT_ASSERT(...);
  909. ...
  910. })
  911. public:
  912. // Non-virtual (same if void).
  913. t f(...) {
  914. t result;
  915. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  916. BOOST_CONTRACT_PUBLIC_FUNCTION(this)
  917. BOOST_CONTRACT_PRECONDITION([&] { // Optional.
  918. BOOST_CONTRACT_ASSERT(...);
  919. ...
  920. })
  921. BOOST_CONTRACT_OLD([&] { // Optional.
  922. old_var = BOOST_CONTRACT_OLDOF(old_expr);
  923. ...
  924. })
  925. BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
  926. BOOST_CONTRACT_ASSERT(...);
  927. ...
  928. })
  929. BOOST_CONTRACT_EXCEPT([&] { // Optional.
  930. BOOST_CONTRACT_ASSERT(...);
  931. ...
  932. })
  933. ;
  934. ... // Function body (use `return result = return_expr`).
  935. }
  936. // Virtual and void.
  937. virtual void g(..., boost::contract::virtual_* v = 0) {
  938. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  939. BOOST_CONTRACT_PUBLIC_FUNCTION(v, this)
  940. ...
  941. BOOST_CONTRACT_OLD([&] { // Optional.
  942. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  943. ...
  944. })
  945. ...
  946. ;
  947. ... // Function body.
  948. }
  949. // Virtual and non-void.
  950. virtual t h(..., boost::contract::virtual_* v = 0) {
  951. t result;
  952. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  953. BOOST_CONTRACT_PUBLIC_FUNCTION(v, result, this)
  954. ...
  955. BOOST_CONTRACT_OLD([&] { // Optional.
  956. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  957. ...
  958. })
  959. BOOST_CONTRACT_POSTCONDITION([&] (t const& result) { // Optional
  960. BOOST_CONTRACT_ASSERT(...);
  961. ...
  962. })
  963. ...
  964. ;
  965. ... // Function body (use `return result = return_expr`).
  966. }
  967. ...
  968. };
  969. @endcode
  970. For optimization, this can be omitted for non-virtual public functions that
  971. do not have preconditions, postconditions and exception guarantees, within
  972. classes that have no invariants.
  973. Virtual public functions should always use
  974. @RefMacro{BOOST_CONTRACT_PUBLIC_FUNCTION} otherwise this library will not
  975. be able to correctly use them for subcontracting.
  976. This is an overloaded variadic macro and it can be used in the following
  977. different ways (note that no code is generated when
  978. @RefMacro{BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS} is defined).
  979. 1\. <c>BOOST_CONTRACT_PUBLIC_FUNCTION(obj)</c> expands to code
  980. equivalent to the following (for non-virtual public functions that are
  981. not static and do not override, returning void or not):
  982. @code
  983. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  984. boost::contract::check internal_var =
  985. boost::contract::public_function(obj)
  986. #endif
  987. @endcode
  988. 2\. <c>BOOST_CONTRACT_PUBLIC_FUNCTION(v, obj)</c> expands to code
  989. equivalent to the following (for virtual public functions that are
  990. not static and do not override, returning void):
  991. @code
  992. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  993. boost::contract::check internal_var =
  994. boost::contract::public_function(v, obj)
  995. #endif
  996. @endcode
  997. 3\. <c>BOOST_CONTRACT_PUBLIC_FUNCTION(v, r, obj)</c> expands to code
  998. equivalent to the following (for virtual public functions that are
  999. not static and do not override, not returning void):
  1000. @code
  1001. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  1002. boost::contract::check internal_var =
  1003. boost::contract::public_function(v, r, obj)
  1004. #endif
  1005. @endcode
  1006. Where (these are all variadic macro parameters so they can contain commas
  1007. not protected by round parenthesis):
  1008. @arg <c><b>v</b></c> is the extra parameter of type
  1009. @RefClass{boost::contract::virtual_}<c>*</c> and default value @c 0
  1010. from the enclosing virtual public function declaring the contract.
  1011. @arg <c><b>r</b></c> is a reference to the return value of the enclosing
  1012. virtual public function declaring the contract.
  1013. This is usually a local variable declared by the enclosing virtual
  1014. public function just before the contract, but programmers must set
  1015. it to the actual value being returned by the function at each
  1016. @c return statement.
  1017. @arg <c><b>obj</b></c> is the object @c this from the scope of the
  1018. enclosing public function declaring the contract.
  1019. This object might be mutable, @c const, @c volatile, or
  1020. <c>const volatile</c> depending on the cv-qualifier of the enclosing
  1021. function (volatile public functions will check volatile class
  1022. invariants, see @RefSect{extras.volatile_public_functions,
  1023. Volatile Public Functions}).
  1024. @arg <c><b>internal_var</b></c> is a variable name internally generated
  1025. by this library (this name is unique but only on different line
  1026. numbers so this macro cannot be expanded multiple times on the same
  1027. line).
  1028. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  1029. Disable Contract Compilation},
  1030. @RefSect{tutorial.public_functions, Public Functions},
  1031. @RefSect{tutorial.virtual_public_functions,
  1032. Virtual Public Functions}
  1033. */
  1034. #define BOOST_CONTRACT_PUBLIC_FUNCTION(...)
  1035. /**
  1036. Program contracts that can be completely disabled at compile-time for
  1037. public function overrides.
  1038. This is used together with @RefMacro{BOOST_CONTRACT_PRECONDITION},
  1039. @RefMacro{BOOST_CONTRACT_POSTCONDITION}, @RefMacro{BOOST_CONTRACT_EXCEPT},
  1040. and @RefMacro{BOOST_CONTRACT_OLD} to specify preconditions, postconditions,
  1041. exception guarantees, and old value copies at body that can be completely
  1042. disabled at compile-time for public function overrides (virtual or not):
  1043. @code
  1044. class u
  1045. #define BASES private boost::contract::constructor_precondition<u>, \
  1046. public b, private w
  1047. : BASES
  1048. {
  1049. friend class boost::contract::access;
  1050. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  1051. #undef BASES
  1052. BOOST_CONTRACT_INVARIANT({ // Optional (as for static and volatile).
  1053. BOOST_CONTRACT_ASSERT(...);
  1054. ...
  1055. })
  1056. BOOST_CONTRACT_OVERRIDES(f, g)
  1057. public:
  1058. // Override from `b::f`, and void.
  1059. void f(t_1 a_1, ..., t_n a_n, boost::contract::virtual_* v = 0) {
  1060. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  1061. BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_f)(
  1062. v, &u::f, this, a_1, ..., a_n)
  1063. BOOST_CONTRACT_PRECONDITION([&] { // Optional.
  1064. BOOST_CONTRACT_ASSERT(...);
  1065. ...
  1066. })
  1067. BOOST_CONTRACT_OLD([&] { // Optional.
  1068. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  1069. ...
  1070. })
  1071. BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
  1072. BOOST_CONTRACT_ASSERT(...);
  1073. ...
  1074. })
  1075. BOOST_CONTRACT_EXCEPT([&] { // Optional.
  1076. BOOST_CONTRACT_ASSERT(...);
  1077. ...
  1078. })
  1079. ;
  1080. ... // Function body.
  1081. }
  1082. // Override from `b::g`, and void.
  1083. t g(t_1 a_1, ..., t_n a_n, boost::contract::virtual_* v = 0) {
  1084. t result;
  1085. BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
  1086. BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_g)(
  1087. v, result, &u::g, this, a_1, ..., a_n)
  1088. ...
  1089. BOOST_CONTRACT_OLD([&] { // Optional.
  1090. old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
  1091. ...
  1092. })
  1093. BOOST_CONTRACT_POSTCONDITION([&] (t const& result) { // Optional
  1094. BOOST_CONTRACT_ASSERT(...);
  1095. ...
  1096. })
  1097. ...
  1098. ;
  1099. ... // Function body (use `return result = return_expr`).
  1100. }
  1101. ...
  1102. };
  1103. @endcode
  1104. Public function overrides should always use
  1105. @RefMacro{BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE} otherwise this library
  1106. will not be able to correctly use it for subcontracting.
  1107. This is an overloaded variadic macro and it can be used in the following
  1108. different ways (note that no code is generated when
  1109. @RefMacro{BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS} is defined).
  1110. 1\. <c>BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_type)(v, f, obj,
  1111. ...)</c> expands to code equivalent to the following (for public
  1112. function overrides that return void):
  1113. @code
  1114. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  1115. boost::contract::check internal_var = boost::contract::
  1116. public_function<override_type>(v, f, obj, ...)
  1117. #endif
  1118. @endcode
  1119. 2\. <c>BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_type)(v, r, f, obj,
  1120. ...)</c> expands to code equivalent to the following (for public
  1121. function overrides that do not return void):
  1122. @code
  1123. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  1124. boost::contract::check internal_var = boost::contract::
  1125. public_function<override_type>(v, r, f, obj, ...)
  1126. #endif
  1127. @endcode
  1128. Where (these are all variadic macro parameters so they can contain commas
  1129. not protected by round parenthesis):
  1130. @arg <c><b>override_type</b></c> is the type
  1131. <c>override_<i>function-name</i></c> declared using the
  1132. @RefMacro{BOOST_CONTRACT_OVERRIDE} or related macros.
  1133. @arg <c><b>v</b></c> is the extra parameter of type
  1134. @RefClass{boost::contract::virtual_}<c>*</c> and default value @c 0
  1135. from the enclosing virtual public function declaring the contract.
  1136. @arg <c><b>r</b></c> is a reference to the return value of the enclosing
  1137. virtual public function declaring the contract.
  1138. This is usually a local variable declared by the enclosing virtual
  1139. public function just before the contract, but programmers must set
  1140. it to the actual value being returned by the function at each
  1141. @c return statement.
  1142. @arg <c><b>f</b></c> is a pointer to the enclosing public function
  1143. override declaring the contract.
  1144. @arg <c><b>obj</b></c> is the object @c this from the scope of the
  1145. enclosing public function declaring the contract.
  1146. This object might be mutable, @c const, @c volatile, or
  1147. <c>const volatile</c> depending on the cv-qualifier of the enclosing
  1148. function (volatile public functions will check volatile class
  1149. invariants, see @RefSect{extras.volatile_public_functions,
  1150. Volatile Public Functions}).
  1151. @arg <c><b>...</b></c> is a variadic macro parameter listing all the
  1152. arguments passed to the enclosing public function override declaring
  1153. the contract (by reference and in order they appear in the enclosing
  1154. function declaration), but excluding the trailing argument @c v.
  1155. @arg <c><b>internal_var</b></c> is a variable name internally generated
  1156. by this library (this name is unique but only on different line
  1157. numbers so this macro cannot be expanded multiple times on the same
  1158. line).
  1159. @see @RefSect{extras.disable_contract_compilation__macro_interface_,
  1160. Disable Contract Compilation},
  1161. @RefSect{tutorial.public_function_overrides__subcontracting_,
  1162. Public Function Overrides}
  1163. */
  1164. #define BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(...)
  1165. #elif !defined(BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS)
  1166. #include <boost/contract/public_function.hpp>
  1167. #include <boost/contract/check.hpp>
  1168. #include <boost/contract/detail/name.hpp>
  1169. #define BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(...) \
  1170. boost::contract::check BOOST_CONTRACT_DETAIL_NAME2(c, __LINE__) = \
  1171. boost::contract::public_function< __VA_ARGS__ >()
  1172. #define BOOST_CONTRACT_PUBLIC_FUNCTION(...) \
  1173. boost::contract::check BOOST_CONTRACT_DETAIL_NAME2(c, __LINE__) = \
  1174. boost::contract::public_function(__VA_ARGS__)
  1175. #define BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(...) \
  1176. boost::contract::check BOOST_CONTRACT_DETAIL_NAME2(c, __LINE__) = \
  1177. boost::contract::public_function<__VA_ARGS__>
  1178. #else
  1179. #include <boost/preprocessor/tuple/eat.hpp>
  1180. #define BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(...) /* nothing */
  1181. #define BOOST_CONTRACT_PUBLIC_FUNCTION(...) /* nothing */
  1182. #define BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(...) BOOST_PP_TUPLE_EAT(0)
  1183. #endif
  1184. #endif // #include guard