scope_exit.hpp 49 KB

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