scope_exit.hpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. // Copyright (C) 2006-2009, 2012 Alexander Nasonov
  2. // Copyright (C) 2012 Lorenzo Caminiti
  3. // Distributed under the Boost Software License, Version 1.0
  4. // (see accompanying file LICENSE_1_0.txt or a copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Home at http://www.boost.org/libs/scope_exit
  7. #ifndef BOOST_SCOPE_EXIT_HPP
  8. #define BOOST_SCOPE_EXIT_HPP
  9. #ifndef DOXYGEN
  10. #include <boost/config/workaround.hpp>
  11. #include <boost/type_traits/integral_constant.hpp>
  12. #include <boost/type_traits/enable_if.hpp>
  13. #include <boost/function.hpp>
  14. #include <boost/typeof/typeof.hpp>
  15. #include <boost/config.hpp>
  16. #include <boost/preprocessor/cat.hpp>
  17. #include <boost/preprocessor/control/iif.hpp>
  18. #include <boost/preprocessor/control/expr_iif.hpp>
  19. #include <boost/preprocessor/comparison/equal.hpp>
  20. #include <boost/preprocessor/logical/bitor.hpp>
  21. #include <boost/preprocessor/logical/bitand.hpp>
  22. #include <boost/preprocessor/facilities/empty.hpp>
  23. #include <boost/preprocessor/facilities/is_empty.hpp>
  24. #include <boost/preprocessor/facilities/identity.hpp>
  25. #include <boost/preprocessor/punctuation/comma_if.hpp>
  26. #include <boost/preprocessor/punctuation/paren_if.hpp>
  27. #include <boost/preprocessor/seq/cat.hpp>
  28. #include <boost/preprocessor/seq/size.hpp>
  29. #include <boost/preprocessor/seq/to_tuple.hpp>
  30. #include <boost/preprocessor/tuple/elem.hpp>
  31. #include <boost/preprocessor/tuple/eat.hpp>
  32. #include <boost/preprocessor/tuple/to_list.hpp>
  33. #include <boost/preprocessor/list/append.hpp>
  34. #include <boost/preprocessor/list/fold_left.hpp>
  35. #include <boost/preprocessor/list/enum.hpp>
  36. #include <boost/preprocessor/list/adt.hpp>
  37. #include <boost/preprocessor/list/for_each_i.hpp>
  38. #include <boost/preprocessor/detail/is_unary.hpp>
  39. // PRIVATE/PROTECTED //
  40. // NOTE: AUX prefix and aux namespace mark "private" symbols that shall be used
  41. // only within this library; DETAIL prefix and detail namespace mark "protected"
  42. // symbols that can be used by other Boost libraries but not outside Boost.
  43. // WARNING: BOOST_SCOPE_EXIT_AUX_GCC also used by some regression test.
  44. #if defined(__GNUC__) && !defined(BOOST_INTEL)
  45. # define BOOST_SCOPE_EXIT_AUX_GCC (__GNUC__ * 100 + __GNUC_MINOR__)
  46. #else
  47. # define BOOST_SCOPE_EXIT_AUX_GCC 0
  48. #endif
  49. #if BOOST_WORKAROUND(BOOST_SCOPE_EXIT_AUX_GCC, BOOST_TESTED_AT(413))
  50. # define BOOST_SCOPE_EXIT_AUX_TPL_GCC_WORKAROUND_01 1
  51. #else
  52. # define BOOST_SCOPE_EXIT_AUX_TPL_GCC_WORKAROUND_01 0
  53. #endif
  54. #if BOOST_MSVC && (BOOST_MSVC <= 1900)
  55. # define BOOST_SCOPE_EXIT_AUX_TYPEOF_THIS_MSVC_WORKAROUND_01 1
  56. #else
  57. # define BOOST_SCOPE_EXIT_AUX_TYPEOF_THIS_MSVC_WORKAROUND_01 0
  58. #endif
  59. // MSVC has problems expanding __LINE__ so use (the non standard) __COUNTER__.
  60. #ifdef BOOST_MSVC
  61. # define BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER __COUNTER__
  62. #else
  63. # define BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER __LINE__
  64. #endif
  65. // Preprocessor "keyword" detection.
  66. // These are not a local macros, do not #undefine them (these are used by the
  67. // ..._BACK macros below).
  68. #define this_BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_THISUNDERSCORE_IS (1) /* unary */
  69. #define void_BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_VOID_IS (1) /* unary */
  70. #define BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_BACK_(token, checking_postfix) \
  71. BOOST_PP_IS_UNARY(BOOST_PP_CAT(token, checking_postfix))
  72. #define BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_THISUNDERSCORE_BACK(token) \
  73. BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_BACK_(token, \
  74. BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_THISUNDERSCORE_IS)
  75. #define BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_VOID_BACK(token) \
  76. BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_BACK_(token, \
  77. _BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_VOID_IS)
  78. // Preprocessor "void-list".
  79. // NOTE: Empty list must always be represented as void (which is also a way to
  80. // specify no function parameter) and it can never be empty because (1)
  81. // IS_EMPTY(&var) fails (because of the leading non alphanumeric symbol) and
  82. // (2) some compilers (MSVC) fail to correctly pass empty macro parameters
  83. // even if they support variadic macros. Therefore, always using void to
  84. // represent is more portable.
  85. // Argument: (token1)...
  86. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_FROM_SEQ_(unused, seq) \
  87. BOOST_PP_TUPLE_TO_LIST(BOOST_PP_SEQ_SIZE(seq), BOOST_PP_SEQ_TO_TUPLE(seq))
  88. // Token: void | token1
  89. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_HANDLE_VOID_( \
  90. is_void_macro, token) \
  91. BOOST_PP_IIF(is_void_macro(token), \
  92. BOOST_PP_NIL \
  93. , \
  94. (token, BOOST_PP_NIL) \
  95. )
  96. // Token: (a)(b)... | empty | void | token
  97. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_HANDLE_SEQ_( \
  98. is_void_macro, token) \
  99. BOOST_PP_IIF(BOOST_PP_IS_UNARY(token), /* unary paren (a)... */ \
  100. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_FROM_SEQ_ \
  101. , \
  102. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_HANDLE_VOID_ \
  103. )(is_void_macro, token)
  104. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_NEVER_(tokens) \
  105. 0 /* void check always returns false */
  106. #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
  107. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_(is_void_macro, seq) \
  108. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_HANDLE_SEQ_(is_void_macro, seq)
  109. // Expand `void | (a)(b)...` to pp-list `NIL | (a, (b, NIL))`.
  110. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST(sign) \
  111. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_( \
  112. BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_VOID_BACK, sign)
  113. // Expand `(a)(b)...` to pp-list `(a, (b, NIL))`.
  114. #define BOOST_SCOPE_EXIT_AUX_PP_NON_VOID_LIST(seq) \
  115. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_( \
  116. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_NEVER_, seq)
  117. #else // VARIADICS
  118. // FUTURE: Replace this with BOOST_PP_VARIADIC_SIZE when and if
  119. // BOOST_PP_VARIAIDCS detection will match !BOOST_NO_CXX11_VARIADIC_MACROS (for
  120. // now Boost.Preprocessor and Boost.Config disagree on detecting compiler
  121. // variadic support while this VARIADIC_SIZE works on compilers not detected by
  122. // PP).
  123. #if BOOST_MSVC
  124. # define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_(...) \
  125. BOOST_PP_CAT(BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_I_(__VA_ARGS__, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,),)
  126. #else // MSVC
  127. # define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_(...) \
  128. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_I_(__VA_ARGS__, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,)
  129. #endif // MSVC
  130. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_I_(e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, e62, e63, size, ...) size
  131. // Argument: token1, ...
  132. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_FROM_VARIADIC_(unused, ...) \
  133. BOOST_PP_TUPLE_TO_LIST( \
  134. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_( \
  135. __VA_ARGS__), (__VA_ARGS__))
  136. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_(is_void_macro, ...) \
  137. BOOST_PP_IIF(BOOST_PP_EQUAL( \
  138. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_VARIADIC_SIZE_( \
  139. __VA_ARGS__), 1), \
  140. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_HANDLE_SEQ_ \
  141. , \
  142. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_FROM_VARIADIC_ \
  143. )(is_void_macro, __VA_ARGS__)
  144. // Expand `void | (a)(b)... | a, b, ...` to pp-list `NIL | (a, (b, NIL))`.
  145. #define BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST(...) \
  146. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_( \
  147. BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_VOID_BACK, __VA_ARGS__)
  148. // Expand `(a)(b)... | a, b, ...` to pp-list `(a, (b, NIL))`.
  149. #define BOOST_SCOPE_EXIT_AUX_PP_NON_VOID_LIST(...) \
  150. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_( \
  151. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST_NEVER_, __VA_ARGS__)
  152. #endif // VARIADICS
  153. // Steven Watanabe's trick with a modification suggested by Kim Barrett
  154. namespace boost { namespace scope_exit { namespace detail {
  155. // Type of a local BOOST_SCOPE_EXIT_AUX_ARGS variable.
  156. // First use in a local scope will declare the BOOST_SCOPE_EXIT_AUX_ARGS
  157. // variable, subsequent uses will be resolved as two comparisons
  158. // (cmp1 with 0 and cmp2 with BOOST_SCOPE_EXIT_AUX_ARGS).
  159. template<int Dummy = 0>
  160. struct declared
  161. {
  162. void* value;
  163. static int const cmp2 = 0;
  164. friend void operator>(int, declared const&) {}
  165. };
  166. struct undeclared { declared<> dummy[2]; };
  167. template<int> struct resolve;
  168. template<>
  169. struct resolve<sizeof(declared<>)>
  170. {
  171. static const int cmp1 = 0;
  172. };
  173. template<>
  174. struct resolve<sizeof(undeclared)>
  175. {
  176. template<int>
  177. struct cmp1
  178. {
  179. static int const cmp2 = 0;
  180. };
  181. };
  182. typedef void (*ref_tag)(int&);
  183. typedef void (*val_tag)(int );
  184. template<class T, class Tag> struct member;
  185. template<class T>
  186. struct member<T,ref_tag>
  187. {
  188. T& value;
  189. #if !BOOST_SCOPE_EXIT_AUX_TPL_GCC_WORKAROUND_01
  190. member(T& ref) : value(ref) {}
  191. #endif
  192. };
  193. template<class T>
  194. struct member<T,val_tag>
  195. {
  196. T value;
  197. #if !BOOST_SCOPE_EXIT_AUX_TPL_GCC_WORKAROUND_01
  198. member(T& val) : value(val) {}
  199. #endif
  200. };
  201. template<class T> inline T& deref(T* p, ref_tag) { return *p; }
  202. template<class T> inline T& deref(T& r, val_tag) { return r; }
  203. template<class T>
  204. struct wrapper
  205. {
  206. typedef T type;
  207. };
  208. template<class T> wrapper<T> wrap(T&);
  209. } } } // namespace
  210. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  211. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::scope_exit::detail::wrapper, 1)
  212. #define BOOST_SCOPE_EXIT_AUX_ARGS boost_scope_exit_aux_args
  213. extern boost::scope_exit::detail::undeclared BOOST_SCOPE_EXIT_AUX_ARGS;
  214. #define BOOST_SCOPE_EXIT_AUX_GUARD(id) \
  215. BOOST_PP_CAT(boost_se_guard_, id)
  216. #define BOOST_SCOPE_EXIT_AUX_GUARD_T(id) \
  217. BOOST_PP_CAT(boost_se_guard_t_, id)
  218. #define BOOST_SCOPE_EXIT_AUX_PARAMS(id) \
  219. BOOST_PP_CAT(boost_se_params_, id)
  220. #define BOOST_SCOPE_EXIT_AUX_THIS_T(id) \
  221. BOOST_PP_CAT(boost_se_this_t_, id)
  222. #define BOOST_SCOPE_EXIT_AUX_THIS_CAPTURE_T(id) \
  223. BOOST_PP_CAT(boost_se_this_capture_t_, id)
  224. #define BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id) \
  225. BOOST_PP_CAT(boost_se_params_t_, id)
  226. #define BOOST_SCOPE_EXIT_DETAIL_TAG(id, i) \
  227. BOOST_PP_SEQ_CAT( (boost_se_tag_)(i)(_)(id) )
  228. #define BOOST_SCOPE_EXIT_DETAIL_PARAM_THIS(id) \
  229. BOOST_PP_SEQ_CAT( (boost_se_param_this_)(id) )
  230. #define BOOST_SCOPE_EXIT_DETAIL_PARAM(id, i, var) \
  231. BOOST_PP_SEQ_CAT( (boost_se_param_)(i)(_)(id) )
  232. #define BOOST_SCOPE_EXIT_DETAIL_PARAM_T(id, i, var) \
  233. BOOST_PP_SEQ_CAT( (boost_se_param_t_)(i)(_)(id) )
  234. #define BOOST_SCOPE_EXIT_DETAIL_CAPTURE_T(id, i, var) \
  235. BOOST_PP_SEQ_CAT( (boost_se_capture_t_)(i)(_)(id) )
  236. #define BOOST_SCOPE_EXIT_AUX_WRAPPED(id, i) \
  237. BOOST_PP_SEQ_CAT( (boost_se_wrapped_t_)(i)(_)(id) )
  238. #define BOOST_SCOPE_EXIT_AUX_DEREF(id, i, var) \
  239. ::boost::scope_exit::detail::deref(var, \
  240. static_cast<BOOST_SCOPE_EXIT_DETAIL_TAG(id, i)>(0))
  241. #define BOOST_SCOPE_EXIT_AUX_MEMBER(r, id, i, var) \
  242. ::boost::scope_exit::detail::member< \
  243. BOOST_SCOPE_EXIT_DETAIL_PARAM_T(id, i, var), \
  244. BOOST_SCOPE_EXIT_DETAIL_TAG(id, i) \
  245. > BOOST_SCOPE_EXIT_DETAIL_PARAM(id, i, var);
  246. #define BOOST_SCOPE_EXIT_AUX_ARG_DECL(r, id_ty, i, var) \
  247. BOOST_PP_COMMA_IF(i) \
  248. BOOST_PP_TUPLE_ELEM(2, 1, id_ty) \
  249. BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty)):: \
  250. BOOST_SCOPE_EXIT_DETAIL_PARAM_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  251. i, var) \
  252. var
  253. #define BOOST_SCOPE_EXIT_AUX_ARG(r, id, i, var) \
  254. BOOST_PP_COMMA_IF(i) \
  255. boost_se_params_->BOOST_SCOPE_EXIT_DETAIL_PARAM(id, i, var).value
  256. #define BOOST_SCOPE_EXIT_DETAIL_TAG_DECL(r, id, i, var) \
  257. typedef void (*BOOST_SCOPE_EXIT_DETAIL_TAG(id, i))(int var);
  258. // Adam Butcher's workaround to deduce `this` type on MSVC revision < 10.
  259. // Boost.Typeof for VC71's typeid-based workaround does not work to determine
  260. // `this` type due to error C2355 being incorrectly reported. The typical
  261. // avoidance strategy implemented below is to make an indirect compile-time
  262. // constant by assigning an enum and use that as type-index-- this only works
  263. // with the sizeof() approach and not with the typeid() approach. Lorenzo
  264. // Caminiti extended this approach to work in type-of emulation mode. This code
  265. // is very similar (and somewhat of a duplication) of the code in
  266. // boost/typeof/msvc/typeof_impl.hpp). However, this code cannot be integrated
  267. // into Boost.Typeof because its final API has to be a `typedef ...` and it
  268. // cannot be a `typeof(...)`.
  269. #if BOOST_SCOPE_EXIT_AUX_TYPEOF_THIS_MSVC_WORKAROUND_01
  270. #include <boost/config.hpp>
  271. #include <boost/detail/workaround.hpp>
  272. #include <boost/type_traits/integral_constant.hpp>
  273. #include <boost/type_traits/is_function.hpp>
  274. #include <boost/type_traits/enable_if.hpp>
  275. #if defined(BOOST_MSVC)
  276. # include <typeinfo>
  277. #endif
  278. namespace boost { namespace scope_exit { namespace aux {
  279. namespace msvc_typeof_this {
  280. // compile-time constant code
  281. #if defined(BOOST_MSVC) && defined(_MSC_EXTENSIONS)
  282. template<int N> struct the_counter;
  283. template<typename T,int N = 5 /* for similarity */>
  284. struct encode_counter {
  285. __if_exists(the_counter<N + 256>) {
  286. BOOST_STATIC_CONSTANT(unsigned,
  287. count=(encode_counter<T,N + 257>::count));
  288. }
  289. __if_not_exists(the_counter<N + 256>) {
  290. __if_exists(the_counter<N + 64>) {
  291. BOOST_STATIC_CONSTANT(unsigned,
  292. count=(encode_counter<T,N + 65>::count));
  293. }
  294. __if_not_exists(the_counter<N + 64>) {
  295. __if_exists(the_counter<N + 16>) {
  296. BOOST_STATIC_CONSTANT(unsigned,
  297. count=(encode_counter<T,N + 17>::count));
  298. }
  299. __if_not_exists(the_counter<N + 16>) {
  300. __if_exists(the_counter<N + 4>) {
  301. BOOST_STATIC_CONSTANT(unsigned,
  302. count=(encode_counter<T,N + 5>::count));
  303. }
  304. __if_not_exists(the_counter<N + 4>) {
  305. __if_exists(the_counter<N>) {
  306. BOOST_STATIC_CONSTANT(unsigned,
  307. count=(encode_counter<T,N + 1>::count));
  308. }
  309. __if_not_exists(the_counter<N>) {
  310. BOOST_STATIC_CONSTANT(unsigned,count=N);
  311. typedef the_counter<N> type;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. };
  318. #else // compile-time constant code
  319. template<int N> struct encode_counter : encode_counter<N - 1> {};
  320. template<> struct encode_counter<0> {};
  321. #endif // compile-time constant code
  322. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) // type-of code
  323. struct msvc_extract_type_default_param {};
  324. template<typename ID, typename T = msvc_extract_type_default_param>
  325. struct msvc_extract_type;
  326. template<typename ID>
  327. struct msvc_extract_type<ID, msvc_extract_type_default_param> {
  328. template<bool>
  329. struct id2type_impl;
  330. typedef id2type_impl<true> id2type;
  331. };
  332. template<typename ID, typename T>
  333. struct msvc_extract_type
  334. : msvc_extract_type<ID, msvc_extract_type_default_param> {
  335. template<>
  336. struct id2type_impl<true> { // VC8.0 specific bug-feature.
  337. typedef T type;
  338. };
  339. template<bool>
  340. struct id2type_impl;
  341. typedef id2type_impl<true> id2type;
  342. };
  343. template<typename T, typename ID>
  344. struct msvc_register_type : msvc_extract_type<ID, T> {};
  345. #else // type-of code
  346. template<typename ID>
  347. struct msvc_extract_type {
  348. struct id2type;
  349. };
  350. template<typename T, typename ID>
  351. struct msvc_register_type : msvc_extract_type<ID> {
  352. typedef msvc_extract_type<ID> base_type;
  353. struct base_type::id2type { // This uses nice VC6.5 and VC7.1 bug-features.
  354. typedef T type;
  355. };
  356. };
  357. #endif // typeof code
  358. template<int Id>
  359. struct msvc_typeid_wrapper {
  360. typedef typename msvc_extract_type<boost::integral_constant<int, Id>
  361. >::id2type id2type;
  362. typedef typename id2type::type type;
  363. };
  364. template<>
  365. struct msvc_typeid_wrapper<4> {
  366. typedef msvc_typeid_wrapper<4> type;
  367. };
  368. template<typename T>
  369. struct encode_type {
  370. BOOST_STATIC_CONSTANT(unsigned, value = encode_counter<T>::count);
  371. typedef typename msvc_register_type<T,
  372. boost::integral_constant<int, value> >::id2type type;
  373. BOOST_STATIC_CONSTANT(unsigned, next = value + 1);
  374. };
  375. template<class T>
  376. struct sizer {
  377. typedef char(*type)[encode_type<T>::value];
  378. };
  379. template<typename T>
  380. typename boost::enable_if_<
  381. !boost::is_function<T>::value
  382. , typename sizer<T>::type
  383. >::type encode_start(T const&);
  384. template<typename T>
  385. typename boost::enable_if_<
  386. boost::is_function<T>::value
  387. , typename sizer<T>::type
  388. >::type encode_start(T&);
  389. template<typename Organizer, typename T>
  390. msvc_register_type<T, Organizer> typeof_register_type(const T&,
  391. Organizer* = 0);
  392. } } } } // namespace
  393. #define BOOST_SCOPE_EXIT_AUX_TYPEDEF_TYPEOF_THIS_INDEX_(id) \
  394. BOOST_PP_CAT(boost_se_thistype_index_, id)
  395. #define BOOST_SCOPE_EXIT_DETAIL_TYPEDEF_TYPEOF_THIS(id, ty, new_type) \
  396. /* unfortunately, we need to go via this enum which causes this to be */ \
  397. /* a typedef construct and not a typeof (so this code cannot be */ \
  398. /* integrated into Boost.Typeof) */ \
  399. enum { \
  400. BOOST_SCOPE_EXIT_AUX_TYPEDEF_TYPEOF_THIS_INDEX_(id) = sizeof( \
  401. *::boost::scope_exit::aux::msvc_typeof_this::encode_start(this)) \
  402. }; \
  403. typedef \
  404. ty ::boost::scope_exit::aux::msvc_typeof_this::msvc_typeid_wrapper< \
  405. BOOST_SCOPE_EXIT_AUX_TYPEDEF_TYPEOF_THIS_INDEX_(id) \
  406. >::type \
  407. new_type \
  408. ;
  409. #else // TYPEOF_THIS_MSVC_WORKAROUND
  410. #define BOOST_SCOPE_EXIT_DETAIL_TYPEDEF_TYPEOF_THIS(id, ty, new_type) \
  411. typedef /* trailing `EMPTY()` handles empty `ty` */ \
  412. BOOST_PP_IIF(BOOST_PP_IS_EMPTY(ty BOOST_PP_EMPTY()), \
  413. BOOST_TYPEOF \
  414. , \
  415. BOOST_TYPEOF_TPL \
  416. )(this) \
  417. new_type \
  418. ;
  419. #endif // TYPEOF_THIS_MSVC_WORKAROUND
  420. #if BOOST_SCOPE_EXIT_AUX_TPL_GCC_WORKAROUND_01
  421. #define BOOST_SCOPE_EXIT_AUX_PARAMS_T_CTOR(id, ty, captures, has_this) \
  422. /* expand to nothing */
  423. #define BOOST_SCOPE_EXIT_DETAIL_PARAM_INIT(r, id, i, var) \
  424. BOOST_PP_COMMA_IF(i) { BOOST_SCOPE_EXIT_AUX_DEREF(id, i, var) }
  425. #define BOOST_SCOPE_EXIT_AUX_PARAMS_INIT(id, captures, has_this) \
  426. BOOST_PP_EXPR_IIF(BOOST_PP_BITOR(has_this, \
  427. BOOST_PP_LIST_IS_CONS(captures)), \
  428. = { \
  429. ) \
  430. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_DETAIL_PARAM_INIT, id, captures) \
  431. BOOST_PP_COMMA_IF(BOOST_PP_BITAND(BOOST_PP_LIST_IS_CONS(captures), \
  432. has_this)) \
  433. BOOST_PP_EXPR_IIF(has_this, this) /* no extra {...} needed here */ \
  434. BOOST_PP_EXPR_IIF(BOOST_PP_BITOR(has_this, \
  435. BOOST_PP_LIST_IS_CONS(captures)), \
  436. } /* trailing `;` will be added by the caller */ \
  437. )
  438. #else // TPL_GCC_WORKAROUND
  439. #define BOOST_SCOPE_EXIT_AUX_CTOR_ARG(r, id, i, var) \
  440. BOOST_PP_COMMA_IF(i) \
  441. BOOST_SCOPE_EXIT_DETAIL_PARAM_T(id, i, var) & BOOST_PP_CAT(a, i)
  442. #define BOOST_SCOPE_EXIT_AUX_MEMBER_INIT(r, id, i, var) \
  443. BOOST_PP_COMMA_IF(i) \
  444. BOOST_SCOPE_EXIT_DETAIL_PARAM(id, i, var) ( BOOST_PP_CAT(a, i) )
  445. #define BOOST_SCOPE_EXIT_AUX_CTOR_ARG_THIS_NAME(id) \
  446. BOOST_PP_CAT(boost_se_this_arg_, id)
  447. #define BOOST_SCOPE_EXIT_AUX_CTOR_ARG_THIS(id, ty, comma01) \
  448. BOOST_PP_COMMA_IF(comma01) \
  449. ty BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id)::BOOST_SCOPE_EXIT_AUX_THIS_T(id) \
  450. BOOST_SCOPE_EXIT_AUX_CTOR_ARG_THIS_NAME(id) /* ptr so no & */
  451. #define BOOST_SCOPE_EXIT_AUX_MEMBER_THIS_INIT(id, comma01) \
  452. BOOST_PP_COMMA_IF(comma01) \
  453. BOOST_SCOPE_EXIT_DETAIL_PARAM_THIS(id)( \
  454. BOOST_SCOPE_EXIT_AUX_CTOR_ARG_THIS_NAME(id))
  455. #define BOOST_SCOPE_EXIT_AUX_PARAMS_T_CTOR(id, ty, captures, has_this) \
  456. BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id)( \
  457. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_AUX_CTOR_ARG, id, captures) \
  458. BOOST_PP_IIF(has_this, \
  459. BOOST_SCOPE_EXIT_AUX_CTOR_ARG_THIS \
  460. , \
  461. BOOST_PP_TUPLE_EAT(3) \
  462. )(id, ty, BOOST_PP_LIST_IS_CONS(captures)) \
  463. ) \
  464. BOOST_PP_EXPR_IIF(BOOST_PP_BITOR(BOOST_PP_LIST_IS_CONS(captures), \
  465. has_this), \
  466. : \
  467. ) \
  468. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_AUX_MEMBER_INIT, id, \
  469. captures) \
  470. BOOST_PP_IIF(has_this, \
  471. BOOST_SCOPE_EXIT_AUX_MEMBER_THIS_INIT \
  472. , \
  473. BOOST_PP_TUPLE_EAT(2) \
  474. )(id, BOOST_PP_LIST_IS_CONS(captures)) \
  475. {}
  476. #define BOOST_SCOPE_EXIT_DETAIL_PARAM_INIT(r, id, i, var) \
  477. BOOST_PP_COMMA_IF(i) BOOST_SCOPE_EXIT_AUX_DEREF(id,i,var)
  478. #define BOOST_SCOPE_EXIT_AUX_PARAMS_INIT(id, captures, has_this) \
  479. BOOST_PP_LPAREN_IF(BOOST_PP_BITOR(has_this, \
  480. BOOST_PP_LIST_IS_CONS(captures))) \
  481. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_DETAIL_PARAM_INIT, id, captures) \
  482. BOOST_PP_COMMA_IF(BOOST_PP_BITAND(BOOST_PP_LIST_IS_CONS(captures), \
  483. has_this)) \
  484. BOOST_PP_EXPR_IIF(has_this, this) \
  485. BOOST_PP_RPAREN_IF(BOOST_PP_BITOR(has_this, \
  486. BOOST_PP_LIST_IS_CONS(captures)))
  487. #endif // TPL_GCC_WORKAROUND
  488. #if defined(BOOST_TYPEOF_EMULATION)
  489. #define BOOST_SCOPE_EXIT_DETAIL_CAPTURE_DECL(r, id_ty, i, var) \
  490. struct BOOST_SCOPE_EXIT_AUX_WRAPPED(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), i) \
  491. /* no need to use TYPEOF_TPL here because it's within inheritance */ \
  492. : BOOST_TYPEOF(::boost::scope_exit::detail::wrap( \
  493. BOOST_SCOPE_EXIT_AUX_DEREF(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  494. i, var))) \
  495. {}; \
  496. typedef BOOST_PP_TUPLE_ELEM(2, 1, id_ty) \
  497. BOOST_SCOPE_EXIT_AUX_WRAPPED(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), i)::type\
  498. BOOST_SCOPE_EXIT_DETAIL_CAPTURE_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  499. i, var) \
  500. ;
  501. #elif defined(BOOST_INTEL)
  502. #define BOOST_SCOPE_EXIT_DETAIL_CAPTURE_DECL(r, id_ty, i, var) \
  503. typedef \
  504. /* no TYPEOF_TPL here because uses TYPEOF_KEYWORD directly */ \
  505. BOOST_TYPEOF_KEYWORD(BOOST_SCOPE_EXIT_AUX_DEREF( \
  506. BOOST_PP_TUPLE_ELEM(2, 0, id_ty), i, var)) \
  507. BOOST_SCOPE_EXIT_DETAIL_CAPTURE_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  508. i, var) \
  509. ;
  510. #else
  511. #define BOOST_SCOPE_EXIT_DETAIL_CAPTURE_DECL(r, id_ty, i, var) \
  512. typedef \
  513. /* no need to use TYPEOF_TPL here because it's a typedef */ \
  514. BOOST_TYPEOF(::boost::scope_exit::detail::wrap( \
  515. BOOST_SCOPE_EXIT_AUX_DEREF(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  516. i, var))) \
  517. BOOST_SCOPE_EXIT_AUX_WRAPPED(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), i) \
  518. ; \
  519. typedef BOOST_PP_TUPLE_ELEM(2, 1, id_ty) \
  520. BOOST_SCOPE_EXIT_AUX_WRAPPED(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), i)::type\
  521. BOOST_SCOPE_EXIT_DETAIL_CAPTURE_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  522. i, var) \
  523. ;
  524. #endif
  525. #define BOOST_SCOPE_EXIT_DETAIL_PARAM_DECL(r, id_ty, i, var) \
  526. typedef \
  527. BOOST_SCOPE_EXIT_DETAIL_CAPTURE_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  528. i, var) \
  529. BOOST_SCOPE_EXIT_DETAIL_PARAM_T(BOOST_PP_TUPLE_ELEM(2, 0, id_ty), \
  530. i, var) \
  531. ;
  532. // Traits.
  533. #define BOOST_SCOPE_EXIT_AUX_TRAITS_OP_CAPTURE(d, captures, this01, capture) \
  534. (BOOST_PP_LIST_APPEND(captures, (capture, BOOST_PP_NIL)), this01)
  535. #define BOOST_SCOPE_EXIT_AUX_TRAITS_OP_THIS(d, captures, this01, this_) \
  536. (captures, 1 /* has this (note, no error if multiple this_) */)
  537. #define BOOST_SCOPE_EXIT_AUX_TRAITS_OP(d, captures_this, capture) \
  538. BOOST_PP_IIF(BOOST_SCOPE_EXIT_AUX_PP_KEYWORD_IS_THISUNDERSCORE_BACK(\
  539. capture), \
  540. BOOST_SCOPE_EXIT_AUX_TRAITS_OP_THIS \
  541. , \
  542. BOOST_SCOPE_EXIT_AUX_TRAITS_OP_CAPTURE \
  543. )(d, BOOST_PP_TUPLE_ELEM(2, 0, captures_this), \
  544. BOOST_PP_TUPLE_ELEM(2, 1, captures_this), capture)
  545. // ref_val: & | =
  546. #define BOOST_SCOPE_EXIT_AUX_TRAITS_ALL_OP(ref_val, traits) \
  547. ( \
  548. BOOST_PP_LIST_APPEND((ref_val, BOOST_PP_NIL), \
  549. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  550. , \
  551. BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits) \
  552. )
  553. #define BOOST_SCOPE_EXIT_AUX_TRAITS(captures) \
  554. BOOST_PP_LIST_FOLD_LEFT(BOOST_SCOPE_EXIT_AUX_TRAITS_OP, \
  555. (BOOST_PP_NIL, 0), captures)
  556. #define BOOST_SCOPE_EXIT_AUX_TRAITS_ALL(captures) \
  557. BOOST_SCOPE_EXIT_AUX_TRAITS_ALL_OP(BOOST_PP_LIST_FIRST(captures), \
  558. BOOST_SCOPE_EXIT_AUX_TRAITS(BOOST_PP_LIST_REST(captures)))
  559. #define BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits) \
  560. BOOST_PP_TUPLE_ELEM(2, 0, traits)
  561. #define BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits) \
  562. BOOST_PP_TUPLE_ELEM(2, 1, traits)
  563. #ifndef BOOST_NO_CXX11_LAMBDAS
  564. namespace boost { namespace scope_exit { namespace aux {
  565. template<typename This = void>
  566. struct guard { // With object `this_` (for backward compatibility).
  567. explicit guard(This _this) : this_(_this) {}
  568. ~guard() { if(f_) f_(this_); }
  569. template<typename Lambda>
  570. void operator=(Lambda f) { f_ = f; }
  571. private:
  572. This this_;
  573. boost::function<void (This)> f_;
  574. };
  575. template<>
  576. struct guard<void> { // Without object `this_` (could capture `this` directly).
  577. ~guard() { if(f_) f_(); }
  578. template<typename Lambda>
  579. void operator=(Lambda f) { f_ = f; }
  580. private:
  581. boost::function<void (void)> f_;
  582. };
  583. } } } // namespace
  584. #define BOOST_SCOPE_EXIT_AUX_LAMBDA_PARAMS(id) \
  585. BOOST_PP_CAT(boost_se_lambda_params_, id)
  586. #define BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_CAPTURE_TYPE(id) \
  587. BOOST_PP_CAT(boost_se_lambda_this_t_, id)
  588. #define BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_PARAM_TYPE(id) \
  589. BOOST_PP_CAT(boost_se_lambda_this_capture_t_, id)
  590. #define BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_TYPE(id, ty) \
  591. ty BOOST_SCOPE_EXIT_AUX_LAMBDA_PARAMS(id):: \
  592. BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_PARAM_TYPE(id)
  593. // Precondition: HAS_THIS(traits).
  594. #define BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_TYPEDEFS(id, ty, traits) \
  595. BOOST_SCOPE_EXIT_DETAIL_TYPEDEF_TYPEOF_THIS(id, ty, \
  596. BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_CAPTURE_TYPE(id)) \
  597. /* capture type for workaround GCC internal error (even on later C++11) */ \
  598. struct BOOST_SCOPE_EXIT_AUX_LAMBDA_PARAMS(id) { \
  599. typedef BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_CAPTURE_TYPE(id) \
  600. BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_PARAM_TYPE(id); \
  601. };
  602. #define BOOST_SCOPE_EXIT_AUX_IMPL_LAMBDA(id, ty, traits) \
  603. BOOST_PP_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  604. /* no need for TYPEDEF THIS MSVC workaround on C++11 */ \
  605. BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_TYPEDEFS \
  606. , \
  607. BOOST_PP_TUPLE_EAT(3) \
  608. )(id, ty, traits) \
  609. ::boost::scope_exit::aux::guard< \
  610. BOOST_PP_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  611. BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_TYPE \
  612. , \
  613. BOOST_PP_TUPLE_EAT(2) \
  614. )(id, ty) \
  615. > BOOST_SCOPE_EXIT_AUX_GUARD(id) \
  616. BOOST_PP_EXPR_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  617. (this) \
  618. ) \
  619. ; \
  620. BOOST_SCOPE_EXIT_AUX_GUARD(id) = [ \
  621. BOOST_PP_LIST_ENUM(BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  622. ]( \
  623. BOOST_PP_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  624. BOOST_SCOPE_EXIT_AUX_LAMBDA_THIS_TYPE \
  625. , \
  626. BOOST_PP_TUPLE_EAT(2) \
  627. )(id, ty) \
  628. BOOST_PP_EXPR_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), this_) \
  629. ) mutable /* can change value captures (as with SCOPE_EXIT) */ -> void
  630. #endif // Lambdas.
  631. #if defined(BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS) && \
  632. !defined(BOOST_NO_CXX11_LAMBDAS) // Use lambda for SCOPE_EXIT (not just _ALL).
  633. #define BOOST_SCOPE_EXIT_AUX_IMPL(id, ty, traits) \
  634. BOOST_SCOPE_EXIT_AUX_IMPL_LAMBDA(id, ty, traits)
  635. #else // Not using lambdas.
  636. // ty: EMPTY() | typename
  637. #define BOOST_SCOPE_EXIT_AUX_IMPL(id, ty, traits) \
  638. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_DETAIL_TAG_DECL, id, \
  639. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  640. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_DETAIL_CAPTURE_DECL, (id, ty), \
  641. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  642. BOOST_PP_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  643. BOOST_SCOPE_EXIT_DETAIL_TYPEDEF_TYPEOF_THIS \
  644. , \
  645. BOOST_PP_TUPLE_EAT(3) \
  646. )(id, ty, BOOST_SCOPE_EXIT_AUX_THIS_CAPTURE_T(id)) \
  647. struct BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id) { \
  648. /* interim capture types to workaround internal errors on old GCC */ \
  649. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_DETAIL_PARAM_DECL, (id, ty), \
  650. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  651. BOOST_PP_EXPR_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  652. typedef BOOST_SCOPE_EXIT_AUX_THIS_CAPTURE_T(id) \
  653. BOOST_SCOPE_EXIT_AUX_THIS_T(id) ; \
  654. ) \
  655. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_AUX_MEMBER, id, \
  656. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  657. BOOST_PP_EXPR_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  658. BOOST_SCOPE_EXIT_AUX_THIS_T(id) \
  659. BOOST_SCOPE_EXIT_DETAIL_PARAM_THIS(id) ; \
  660. ) \
  661. BOOST_SCOPE_EXIT_AUX_PARAMS_T_CTOR(id, ty, \
  662. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits), \
  663. BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits)) \
  664. } BOOST_SCOPE_EXIT_AUX_PARAMS(id) \
  665. BOOST_SCOPE_EXIT_AUX_PARAMS_INIT(id, \
  666. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits), \
  667. BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits)) \
  668. ; \
  669. ::boost::scope_exit::detail::declared< \
  670. ::boost::scope_exit::detail::resolve< \
  671. sizeof(BOOST_SCOPE_EXIT_AUX_ARGS) \
  672. >::cmp1<0>::cmp2 \
  673. > BOOST_SCOPE_EXIT_AUX_ARGS; \
  674. BOOST_SCOPE_EXIT_AUX_ARGS.value = &BOOST_SCOPE_EXIT_AUX_PARAMS(id); \
  675. struct BOOST_SCOPE_EXIT_AUX_GUARD_T(id) { \
  676. BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id)* boost_se_params_; \
  677. BOOST_SCOPE_EXIT_AUX_GUARD_T(id) (void* boost_se_params) \
  678. : boost_se_params_( \
  679. (BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id)*)boost_se_params) \
  680. {} \
  681. ~BOOST_SCOPE_EXIT_AUX_GUARD_T(id)() { \
  682. boost_se_body( \
  683. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_AUX_ARG, id, \
  684. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  685. BOOST_PP_COMMA_IF(BOOST_PP_BITAND(BOOST_PP_LIST_IS_CONS( \
  686. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)), \
  687. BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits))) \
  688. BOOST_PP_EXPR_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS( \
  689. traits), \
  690. boost_se_params_->BOOST_SCOPE_EXIT_DETAIL_PARAM_THIS(id) \
  691. ) \
  692. ); \
  693. } \
  694. static void boost_se_body( \
  695. BOOST_PP_LIST_FOR_EACH_I(BOOST_SCOPE_EXIT_AUX_ARG_DECL, (id, ty), \
  696. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)) \
  697. BOOST_PP_COMMA_IF(BOOST_PP_BITAND(BOOST_PP_LIST_IS_CONS( \
  698. BOOST_SCOPE_EXIT_AUX_TRAITS_CAPTURES(traits)), \
  699. BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits))) \
  700. BOOST_PP_EXPR_IIF(BOOST_SCOPE_EXIT_AUX_TRAITS_HAS_THIS(traits), \
  701. ty BOOST_SCOPE_EXIT_DETAIL_PARAMS_T(id):: \
  702. BOOST_SCOPE_EXIT_AUX_THIS_T(id) this_ \
  703. ) \
  704. )
  705. #endif // Using lambdas.
  706. // PUBLIC //
  707. #if defined(BOOST_NO_CXX11_VARIADIC_MACROS) // No variadic macros (sequences only).
  708. # define BOOST_SCOPE_EXIT_ID(id, void_or_seq) \
  709. BOOST_SCOPE_EXIT_AUX_IMPL(id, BOOST_PP_EMPTY(), \
  710. BOOST_SCOPE_EXIT_AUX_TRAITS( \
  711. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST(void_or_seq)))
  712. # define BOOST_SCOPE_EXIT_ID_TPL(id, void_or_seq) \
  713. BOOST_SCOPE_EXIT_AUX_IMPL(id, typename, \
  714. BOOST_SCOPE_EXIT_AUX_TRAITS( \
  715. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST(void_or_seq)))
  716. # define BOOST_SCOPE_EXIT(void_or_seq) \
  717. BOOST_SCOPE_EXIT_ID(BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER, \
  718. void_or_seq)
  719. # define BOOST_SCOPE_EXIT_TPL(void_or_seq) \
  720. BOOST_SCOPE_EXIT_ID_TPL(BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER, \
  721. void_or_seq)
  722. # if !defined(BOOST_NO_CXX11_LAMBDAS)
  723. # define BOOST_SCOPE_EXIT_ALL_ID(id, seq) \
  724. BOOST_SCOPE_EXIT_AUX_IMPL_LAMBDA(id, \
  725. /* C++11 allows to use typename outside templates so */ \
  726. /* always typename here and no need for ..._ALL_TPL */ \
  727. /* (if a C++11 compiler does not implement this use of */ \
  728. /* typename, always use `this` instead of `this_`) */ \
  729. typename, \
  730. BOOST_SCOPE_EXIT_AUX_TRAITS_ALL( \
  731. BOOST_SCOPE_EXIT_AUX_PP_NON_VOID_LIST(seq)))
  732. # define BOOST_SCOPE_EXIT_ALL(seq) \
  733. BOOST_SCOPE_EXIT_ALL_ID( \
  734. BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER, seq)
  735. # endif
  736. #else // Variadic macros (both sequences and variadic tuples).
  737. # define BOOST_SCOPE_EXIT_ID(id, ...) \
  738. BOOST_SCOPE_EXIT_AUX_IMPL(id, BOOST_PP_EMPTY(), \
  739. BOOST_SCOPE_EXIT_AUX_TRAITS( \
  740. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST(__VA_ARGS__)))
  741. # define BOOST_SCOPE_EXIT_ID_TPL(id, ...) \
  742. BOOST_SCOPE_EXIT_AUX_IMPL(id, typename, \
  743. BOOST_SCOPE_EXIT_AUX_TRAITS( \
  744. BOOST_SCOPE_EXIT_AUX_PP_VOID_LIST(__VA_ARGS__)))
  745. # define BOOST_SCOPE_EXIT(...) \
  746. BOOST_SCOPE_EXIT_ID(BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER, \
  747. __VA_ARGS__)
  748. # define BOOST_SCOPE_EXIT_TPL(...) \
  749. BOOST_SCOPE_EXIT_ID_TPL(BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER, \
  750. __VA_ARGS__)
  751. # if !defined(BOOST_NO_CXX11_LAMBDAS)
  752. # define BOOST_SCOPE_EXIT_ALL_ID(id, ...) \
  753. BOOST_SCOPE_EXIT_AUX_IMPL_LAMBDA(id, \
  754. /* C++11 allows to use typename outside templates so */ \
  755. /* always typename here and no need for ..._ALL_TPL */ \
  756. /* (if a C++11 compiler does not implement this use of */ \
  757. /* typename, always use `this` instead of `this_`) */ \
  758. typename, \
  759. BOOST_SCOPE_EXIT_AUX_TRAITS_ALL( \
  760. BOOST_SCOPE_EXIT_AUX_PP_NON_VOID_LIST( \
  761. __VA_ARGS__)))
  762. # define BOOST_SCOPE_EXIT_ALL(...) \
  763. BOOST_SCOPE_EXIT_ALL_ID( \
  764. BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER, __VA_ARGS__)
  765. # endif
  766. #endif // Variadics.
  767. #if defined(BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS) && \
  768. !defined(BOOST_NO_CXX11_LAMBDAS) // Use lambdas for SCOPE_EXIT (not just ALL).
  769. # define BOOST_SCOPE_EXIT_END_ID(id) \
  770. ; /* lambdas ended with just `;` */
  771. #else // Not using lambdas.
  772. # define BOOST_SCOPE_EXIT_END_ID(id) \
  773. } BOOST_SCOPE_EXIT_AUX_GUARD(id)(BOOST_SCOPE_EXIT_AUX_ARGS.value);
  774. #endif // Using lambdas.
  775. #define BOOST_SCOPE_EXIT_END \
  776. BOOST_SCOPE_EXIT_END_ID(BOOST_SCOPE_EXIT_AUX_PP_LINE_COUNTER)
  777. // DOCUMENTATION //
  778. #else // DOXYGEN
  779. /** @file
  780. @brief Scope exits allow to execute arbitrary code when the enclosing scope
  781. exits.
  782. */
  783. /**
  784. @brief This macro declares a scope exit.
  785. The scope exit declaration schedules the execution of the scope exit body at
  786. the exit of the enclosing scope:
  787. @code
  788. { // Some local scope.
  789. ...
  790. BOOST_SCOPE_EXIT(capture_list) {
  791. ... // Body code.
  792. } BOOST_SCOPE_EXIT_END
  793. ...
  794. }
  795. @endcode
  796. The enclosing scope must be local.
  797. If multiple scope exits are declared within the same enclosing scope, the scope
  798. exit bodies are executed in the reversed order of their declarations.
  799. Note how the end of the scope exit body must be marked by
  800. @RefMacro{BOOST_SCOPE_EXIT_END}.
  801. @Params
  802. @Param{capture_list,
  803. On compilers that support variadic macros (see also Boost.Config
  804. <c>BOOST_NO_CXX11_VARIADIC_MACROS</c>)\, the capture list syntax is defined by the
  805. following grammar:
  806. @code
  807. capture_list:
  808. void | capture_tuple | capture_sequence
  809. capture_tuple:
  810. capture\, capture\, ...
  811. capture_sequence:
  812. (capture) (capture) ...
  813. capture:
  814. [&]variable | this_
  815. @endcode
  816. On compilers that do not support variadic macros\, <c>capture_tuple</c> cannot
  817. be used:
  818. @code
  819. capture_list:
  820. void | capture_sequence
  821. @endcode
  822. Furthermore\, if @RefMacro{BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS} is defined on
  823. C++11 compilers that support lambda functions (i.e.\, Boost.Config's <c>BOOST_NO_CXX11_LAMBDAS</c> is not defined) then a semicolon <c>;</c> can be used instead of
  824. @RefMacro{BOOST_SCOPE_EXIT_END} and <c>this</c> can be used instead of
  825. <c>this_</c>:
  826. @code
  827. capture:
  828. [&]variable | this_ | this
  829. @endcode
  830. (Lexical conventions: <c>token1 | token2</c> means either <c>token1</c> or
  831. <c>token2</c>; <c>[token]</c> means either <c>token</c> or nothing;
  832. <c>{expression}</c> means the tokens resulting from the expression.)
  833. }
  834. @EndParams
  835. Note that on compilers that support variadic macros (most of moder compliers
  836. and all C++11 compilers), the capture list can be specified as a
  837. comma-separated list of tokens (this is the preferred syntax).
  838. However, on all compilers the same macro @RefMacro{BOOST_SCOPE_EXIT} also
  839. allows to specify the capture list as a Boost.Preprocessor sequence of tokens
  840. (for supporting compilers without variadic macros and for backward compatibility with older versions of this library).
  841. The name <c>variable</c> of each captured variable must be a valid name in the
  842. enclosing scope and it must appear exactly once in the capture list.
  843. If a capture starts with the ampersand sign <c>&</c>, the corresponding
  844. variable will be available by reference within the scope exit body; otherwise,
  845. a copy of the variable will be made at the point of the scope exit declaration
  846. and that copy will be available inside the scope exit body (in this case, the
  847. variable's type must be <c>CopyConstructible</c>).
  848. From within a member function, the object <c>this</c> can be captured using the
  849. special name <c>this_</c> in both the capture list and the scope exit body
  850. (using <c>this</c> instead of <c>this_</c> in the scope exit body leads to
  851. undefined behaviour).
  852. It is possible to capture no variable by specifying the capture list as
  853. <c>void</c> (regardless of variadic macro support).
  854. Only variables listed in the capture list, static variables, <c>extern</c>
  855. variables, global variables, functions, and enumerations from the enclosing
  856. scope can be used inside the scope exit body.
  857. On various GCC versions the special macro @RefMacro{BOOST_SCOPE_EXIT_TPL} must
  858. be used instead of @RefMacro{BOOST_SCOPE_EXIT} within templates (to maximize
  859. portability, it is recommended to always use @RefMacro{BOOST_SCOPE_EXIT_TPL}
  860. within templates).
  861. On C++11, it is possible capture all variables in scope without listing their
  862. names one-by-one using the macro @RefMacro{BOOST_SCOPE_EXIT_ALL}.
  863. In general, the special macro @RefMacro{BOOST_SCOPE_EXIT_ID} must be used
  864. instead of @RefMacro{BOOST_SCOPE_EXIT} when it is necessary to expand multiple
  865. scope exit declarations on the same line.
  866. @Warning The implementation executes the scope exit body within a destructor
  867. thus the scope exit body must never throw in order to comply with STL exception
  868. safety requirements.
  869. @Note The implementation uses Boost.Typeof to automatically deduce the types of
  870. the captured variables.
  871. In order to compile code in type-of emulation mode, all types must be properly
  872. registered with Boost.Typeof (see the
  873. @RefSect{getting_started, Getting Started} section).
  874. @See @RefSect{tutorial, Tutorial} section,
  875. @RefSect{getting_started, Getting Started} section,
  876. @RefSect{no_variadic_macros, No Variadic Macros} section,
  877. @RefMacro{BOOST_SCOPE_EXIT_TPL}, @RefMacro{BOOST_SCOPE_EXIT_ALL},
  878. @RefMacro{BOOST_SCOPE_EXIT_END}, @RefMacro{BOOST_SCOPE_EXIT_ID}.
  879. */
  880. #define BOOST_SCOPE_EXIT(capture_list)
  881. /**
  882. @brief This macro is a workaround for various versions of GCC to declare scope
  883. exits within templates.
  884. Various versions of the GCC compiler do not compile @RefMacro{BOOST_SCOPE_EXIT}
  885. inside function templates.
  886. As a workaround, @RefMacro{BOOST_SCOPE_EXIT_TPL} should be used instead of
  887. @RefMacro{BOOST_SCOPE_EXIT} in these cases:
  888. @code
  889. { // Some local scope.
  890. ...
  891. BOOST_SCOPE_EXIT_TPL(capture_list) {
  892. ... // Body code.
  893. } BOOST_SCOPE_EXIT_END
  894. ...
  895. }
  896. @endcode
  897. The syntax of @RefMacro{BOOST_SCOPE_EXIT_TPL} is the exact same as the one of
  898. @RefMacro{BOOST_SCOPE_EXIT} (see @RefMacro{BOOST_SCOPE_EXIT} for more
  899. information).
  900. On C++11 compilers, @RefMacro{BOOST_SCOPE_EXIT_TPL} is not needed because
  901. @RefMacro{BOOST_SCOPE_EXIT} always compiles on GCC versions that support C++11.
  902. However, @RefMacro{BOOST_SCOPE_EXIT_TPL} is still provided on C++11 so to write code that is portable between C++03 and C++11 compilers.
  903. It is recommended to always use @RefMacro{BOOST_SCOPE_EXIT_TPL} within
  904. templates so to maximize portability.
  905. In general, the special macro @RefMacro{BOOST_SCOPE_EXIT_ID_TPL} must be used
  906. instead of @RefMacro{BOOST_SCOPE_EXIT_TPL} when it is necessary to expand
  907. multiple scope exit declarations on the same line within templates.
  908. @Note The issue in compiling scope exit declarations that some GCC versions
  909. have is illustrated by the following code (see also
  910. <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37920">GCC bug 37920</a>):
  911. @code
  912. template<class T>
  913. void f(T const& x) {
  914. int i = 0;
  915. struct local {
  916. typedef __typeof__(i) typeof_i;
  917. typedef __typeof__(x) typeof_x;
  918. };
  919. typedef local::typeof_i i_type;
  920. typedef local::typeof_x x_type;
  921. }
  922. int main(void) { f(0); }
  923. @endcode
  924. This can be fixed by adding <c>typename</c> in front of <c>local::typeof_i</c>
  925. and <c>local::typeof_x</c> (which is the approach followed by the
  926. implementation of the @RefMacro{BOOST_SCOPE_EXIT_TPL} macro).
  927. @Note Although @RefMacro{BOOST_SCOPE_EXIT_TPL} has the same suffix as
  928. <c>BOOST_TYPEOF_TPL</c>, it does not follow the Boost.Typeof convention.
  929. @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_SCOPE_EXIT},
  930. @RefMacro{BOOST_SCOPE_EXIT_END}, @RefMacro{BOOST_SCOPE_EXIT_ID_TPL}.
  931. */
  932. #define BOOST_SCOPE_EXIT_TPL(capture_list)
  933. /**
  934. @brief This macro allows to expand multiple scope exit declarations on the same
  935. line.
  936. This macro is equivalent to @RefMacro{BOOST_SCOPE_EXIT} but it can be expanded
  937. multiple times on the same line if different identifiers <c>id</c> are provided
  938. for each expansion (see @RefMacro{BOOST_SCOPE_EXIT} for more information).
  939. @Params
  940. @Param{id,
  941. A unique identifier token which can be concatenated by the preprocessor
  942. (<c>__LINE__</c>\, <c>scope_exit_number_1_on_line_123</c>\, a combination of
  943. alphanumeric tokens\, etc).
  944. }
  945. @Param{capture_list,
  946. Same as the <c>capture_list</c> parameter of the @RefMacro{BOOST_SCOPE_EXIT}
  947. macro.
  948. }
  949. @EndParams
  950. @Note This macro can be useful when the scope exit macros are expanded
  951. within user-defined macros (because nested macros expand on the same line).
  952. On some compilers (e.g., MSVC which supports the non standard
  953. <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
  954. the use of this macro is always necessary to ensure portability when expanding
  955. multiple scope exit declarations on the same line.
  956. @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_SCOPE_EXIT},
  957. @RefMacro{BOOST_SCOPE_EXIT_END_ID}, @RefMacro{BOOST_SCOPE_EXIT_ALL_ID},
  958. @RefMacro{BOOST_SCOPE_EXIT_ID_TPL}.
  959. */
  960. #define BOOST_SCOPE_EXIT_ID(id, capture_list)
  961. /**
  962. @brief This macro is required to expand multiple scope exit declarations on the
  963. same line within templates on various versions of GCC.
  964. This macro is equivalent to @RefMacro{BOOST_SCOPE_EXIT_TPL} but it can be
  965. expanded multiple times on the same line if different identifiers <c>id</c> are
  966. provided for each expansion (see @RefMacro{BOOST_SCOPE_EXIT_TPL} for more
  967. information).
  968. As with @RefMacro{BOOST_SCOPE_EXIT_TPL}, it is recommended to always use this
  969. macro when expanding scope exits multiple times on the same line within
  970. templates.
  971. @Params
  972. @Param{id,
  973. A unique identifier token which can be concatenated by the preprocessor
  974. (<c>__LINE__</c>\, <c>scope_exit_number_1_on_line_123</c>\, a combination of
  975. alphanumeric tokens\, etc).
  976. }
  977. @Param{capture_list,
  978. Same as the <c>capture_list</c> parameter of the
  979. @RefMacro{BOOST_SCOPE_EXIT_TPL} macro.
  980. }
  981. @EndParams
  982. @Note This macro can be useful when the scope exit macros are expanded
  983. within user-defined macros (because nested macros expand on the same line).
  984. On some compilers (e.g., MSVC which supports the non standard
  985. <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
  986. the use of this macro is always necessary to ensure portability when expanding
  987. multiple scope exit declarations on the same line.
  988. @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_SCOPE_EXIT_TPL},
  989. @RefMacro{BOOST_SCOPE_EXIT_END_ID}, @RefMacro{BOOST_SCOPE_EXIT_ID},
  990. @RefMacro{BOOST_SCOPE_EXIT_ALL_ID}.
  991. */
  992. #define BOOST_SCOPE_EXIT_ID_TPL(id, capture_list)
  993. /**
  994. @brief This macro declares a scope exit that captures all variables in scope
  995. (C++11 only).
  996. This macro accepts a capture list starting with either <c>&</c> or <c>=</c> to capture all variables in scope by reference or value respectively (following the same syntax of C++11 lambdas).
  997. A part from that, this macro works like @RefMacro{BOOST_SCOPE_EXIT} (see @RefMacro{BOOST_SCOPE_EXIT} for more information):
  998. @code
  999. { // Some local scope.
  1000. ...
  1001. BOOST_SCOPE_EXIT_ALL(capture_list) { // C++11 only.
  1002. ... // Body code.
  1003. }; // Use `;` instead of `BOOST_SCOPE_EXIT_END` (C++11 only).
  1004. ...
  1005. }
  1006. @endcode
  1007. Note how the end of the scope exit body declared by this macro must be marked
  1008. by a semi-column <c>;</c> (and not by @RefMacro{BOOST_SCOPE_EXIT_END}).
  1009. @Warning This macro is only available on C++11 compilers (specifically, on
  1010. C++11 compilers that do not define the Boost.Config <c>BOOST_NO_CXX11_LAMBDAS</c>
  1011. macro).
  1012. It is not defined on non-C++11 compilers so its use on non-C++11 compilers will generate a compiler error.
  1013. @Params
  1014. @Param{capture_list,
  1015. On compilers that support variadic macros (see also Boost.Config
  1016. <c>BOOST_NO_CXX11_VARIADIC_MACROS</c>)\, the capture list syntax is defined by the
  1017. following grammar:
  1018. @code
  1019. capture_list:
  1020. capture_tuple | capture_sequence
  1021. capture_tuple:
  1022. {& | =} [\, capture\, capture\, ...]
  1023. capture_sequence:
  1024. {(&) | (=)} [(capture) (capture) ...]
  1025. capture:
  1026. [&]variable | this_
  1027. @endcode
  1028. On compilers that do not support variadic macros\, <c>capture_tuple</c> cannot
  1029. be used:
  1030. @code
  1031. capture_list:
  1032. void | capture_sequence
  1033. @endcode
  1034. Furthermore\, on C++11 compilers that support the use of <c>typename</c>
  1035. outside templates\, also <c>this</c> can be used to capture the object at member
  1036. function scope:
  1037. @code
  1038. capture:
  1039. [&]variable | this_ | this
  1040. @endcode
  1041. (Lexical conventions: <c>token1 | token2</c> means either <c>token1</c> or
  1042. <c>token2</c>; <c>[token]</c> means either <c>token</c> or nothing;
  1043. <c>{expression}</c> means the token resulting from the expression.)
  1044. }
  1045. @EndParams
  1046. Note that on compilers with variadic macro support (which should be all C++11
  1047. compilers), the capture list can be specified as a comma-separated list.
  1048. On all compilers, the same macro @RefMacro{BOOST_SCOPE_EXIT_ALL} also allows to
  1049. specify the capture list as a Boost.Preprocessor sequence.
  1050. The capture list must always contain at least the leading <c>&</c> or <c>=</c>
  1051. so it can never be <c>void</c> (<c>BOOST_SCOPE_EXIT(void)</c> should be used
  1052. to program scope exits with an empty capture list).
  1053. In general, the special macro @RefMacro{BOOST_SCOPE_EXIT_ALL_ID} must be used
  1054. instead of @RefMacro{BOOST_SCOPE_EXIT_ALL} when it is necessary to expand
  1055. multiple scope exit declarations on the same line.
  1056. @Warning This macro capture list follows the exact same syntax of C++11 lambda
  1057. captures which is unfortunately different from the syntax of
  1058. @RefMacro{BOOST_SCOPE_EXIT} captures (unless programmers define the
  1059. @RefMacro{BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS} macro).
  1060. For example, like C++11 lambda functions, @RefMacro{BOOST_SCOPE_EXIT_ALL}
  1061. requires to capture data members by capturing the object <c>this</c> while
  1062. @RefMacro{BOOST_SCOPE_EXIT} allows to capture data members directly and without
  1063. capturing the object.
  1064. @Warning The implementation executes the scope exit body within a destructor
  1065. thus the scope exit body must never throw in order to comply with STL exception
  1066. safety requirements.
  1067. @Note This macro can always be used also within templates (so there is no need
  1068. for a <c>BOOST_SCOPE_EXIT_ALL_TPL</c> macro).
  1069. @See @RefSect{tutorial, Tutorial} section,
  1070. @RefSect{no_variadic_macros, No Variadic Macros} section,
  1071. @RefMacro{BOOST_SCOPE_EXIT}, @RefMacro{BOOST_SCOPE_EXIT_ALL_ID}.
  1072. */
  1073. #define BOOST_SCOPE_EXIT_ALL(capture_list)
  1074. /**
  1075. @brief This macro allows to expand on the same line multiple scope exits that
  1076. capture all variables in scope (C++11 only).
  1077. This macro is equivalent to @RefMacro{BOOST_SCOPE_EXIT_ALL} but it can be
  1078. expanded multiple times on the same line if different identifiers <c>id</c> are
  1079. provided for each expansion (see @RefMacro{BOOST_SCOPE_EXIT_ALL} for more
  1080. information).
  1081. As with @RefMacro{BOOST_SCOPE_EXIT_ALL}, this macro is only available on C++11
  1082. compilers (specifically, on C++11 compilers that do not define the
  1083. Boost.Config <c>BOOST_NO_CXX11_LAMBDAS</c> macro).
  1084. @Params
  1085. @Param{id,
  1086. A unique identifier token which can be concatenated by the preprocessor
  1087. (<c>__LINE__</c>\, <c>scope_exit_number_1_on_line_123</c>\, a combination of
  1088. alphanumeric tokens\, etc).
  1089. }
  1090. @Param{capture_list,
  1091. Same as the <c>capture_list</c> parameter of the
  1092. @RefMacro{BOOST_SCOPE_EXIT_ALL} macro.
  1093. }
  1094. @EndParams
  1095. @Note This macro can be useful when the scope exit macros are expanded
  1096. within user-defined macros (because nested macros expand on the same line).
  1097. On some compilers (e.g., MSVC which supports the non standard
  1098. <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
  1099. the use of this macro is always necessary to ensure portability when expanding
  1100. multiple scope exit declarations on the same line.
  1101. @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_SCOPE_EXIT_ALL},
  1102. @RefMacro{BOOST_SCOPE_EXIT_ID}.
  1103. */
  1104. #define BOOST_SCOPE_EXIT_ALL_ID(id, capture_list)
  1105. /**
  1106. @brief This macro marks the end of a scope exit body.
  1107. This macro must follow the closing curly bracket <c>}</c> that ends the body of
  1108. either @RefMacro{BOOST_SCOPE_EXIT} or @RefMacro{BOOST_SCOPE_EXIT_TPL}:
  1109. @code
  1110. { // Some local scope.
  1111. ...
  1112. BOOST_SCOPE_EXIT(capture_list) {
  1113. ... // Body code.
  1114. } BOOST_SCOPE_EXIT_END
  1115. ...
  1116. }
  1117. @endcode
  1118. In general, the special macro @RefMacro{BOOST_SCOPE_EXIT_END_ID} must be used
  1119. instead of @RefMacro{BOOST_SCOPE_EXIT_END} when it is necessary to expand
  1120. multiple scope exit bodies on the same line.
  1121. @Note If programmers define the @RefMacro{BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS}
  1122. macro on C++11 compilers, a semicolon <c>;</c> can be used instead of this
  1123. macro.
  1124. However, to maximize portability, it is recommended to always use
  1125. @RefMacro{BOOST_SCOPE_EXIT_END}.
  1126. @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_SCOPE_EXIT},
  1127. @RefMacro{BOOST_SCOPE_EXIT_TPL}, @RefMacro{BOOST_SCOPE_EXIT_END_ID}.
  1128. */
  1129. #define BOOST_SCOPE_EXIT_END
  1130. /**
  1131. @brief This macro allows to terminate multiple scope exit bodies on the same
  1132. line.
  1133. This macro is equivalent to @RefMacro{BOOST_SCOPE_EXIT_END} but it can be
  1134. expanded multiple times on the same line if different identifiers <c>id</c> are
  1135. provided for each expansion (see @RefMacro{BOOST_SCOPE_EXIT_END} for more
  1136. information).
  1137. @Params
  1138. @Param{id,
  1139. A unique identifier token which can be concatenated by the preprocessor
  1140. (<c>__LINE__</c>\, <c>scope_exit_number_1_on_line_123</c>\, a combination of
  1141. alphanumeric tokens\, etc).
  1142. }
  1143. @EndParams
  1144. @Note This macro can be useful when the scope exit macros are expanded
  1145. within user-defined macros (because macros all expand on the same line).
  1146. On some compilers (e.g., MSVC which supports the non standard
  1147. <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
  1148. the use of this macro is always necessary to ensure portability when expanding
  1149. multiple scope exit macros on the same line (because this library can only
  1150. portably use <c>__LINE__</c> to internally generate unique identifiers).
  1151. @See @RefMacro{BOOST_SCOPE_EXIT_ID}, @RefMacro{BOOST_SCOPE_EXIT_ID_TPL},
  1152. @RefMacro{BOOST_SCOPE_EXIT_END}.
  1153. */
  1154. #define BOOST_SCOPE_EXIT_END_ID(id)
  1155. /**
  1156. @brief Force to use C++11 lambda functions to implement scope exits.
  1157. If programmers define this configuration macro on a C++11 compiler for which
  1158. the Boost.Config macro <c>BOOST_NO_CXX11_LAMBDAS</c> is not defined, the
  1159. @RefMacro{BOOST_SCOPE_EXIT} and @RefMacro{BOOST_SCOPE_EXIT_TPL} macros will use
  1160. C++11 lambda functions to declare scope exits.
  1161. By default this macro is not defined.
  1162. @Warning When scope exits are implemented using lambda functions, the syntax of
  1163. the capture list follows the exact same syntax of C++11 lambda captures
  1164. which is in general different from the legacy capture syntax of this library.
  1165. For example, C++11 lambdas require to capture data members by capturing the
  1166. object <c>this</c> while this library always allowed to capture data members
  1167. directly.
  1168. Therefore, when this configuration macro is defined,
  1169. @RefMacro{BOOST_SCOPE_EXIT} and @RefMacro{BOOST_SCOPE_EXIT_TPL} are no longer
  1170. backward compatible (and this is why this macro is not defined by default).
  1171. A semicolon <c>;</c> can be used instead of @RefMacro{BOOST_SCOPE_EXIT_END}
  1172. when this configuration macro is defined (but it is recommended to always use
  1173. @RefMacro{BOOST_SCOPE_EXIT_END} so to maximize portability).
  1174. @Note This configuration macro does not control the definition of
  1175. @RefMacro{BOOST_SCOPE_EXIT_ALL} which is always and automatically defined on
  1176. compilers that support C++11 lambda functions.
  1177. @See @RefMacro{BOOST_SCOPE_EXIT}, @RefMacro{BOOST_SCOPE_EXIT_TPL},
  1178. @RefMacro{BOOST_SCOPE_EXIT_END}.
  1179. */
  1180. #define BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS
  1181. #endif // DOXYGEN
  1182. #endif // BOOST_SCOPE_EXIT_HPP