• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2006-2009, 2012 Alexander Nasonov
3 // Copyright (C) 2012 Lorenzo Caminiti
4 // Distributed under the Boost Software License, Version 1.0
5 // (see accompanying file LICENSE_1_0.txt or a copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 // Home at http://www.boost.org/libs/scope_exit
8 
9 #include <boost/scope_exit.hpp>
10 #include <boost/preprocessor/cat.hpp>
11 #include <boost/config.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 
14 #define SCOPE_EXIT_INC_DEC(variable, offset) \
15     BOOST_SCOPE_EXIT_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
16             (&variable) (offset) ) { \
17         variable += offset; \
18     } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(inc, __LINE__)) \
19     \
20     BOOST_SCOPE_EXIT_ID(BOOST_PP_CAT(dec, __LINE__), \
21             (&variable) (offset) ) { \
22         variable -= offset; \
23     } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(dec, __LINE__))
24 
25 #define SCOPE_EXIT_INC_DEC_TPL(variable, offset) \
26     BOOST_SCOPE_EXIT_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
27             (&variable) (offset) ) { \
28         variable += offset; \
29     } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(inc, __LINE__)) \
30     \
31     BOOST_SCOPE_EXIT_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
32             (&variable) (offset) ) { \
33         variable -= offset; \
34     } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(dec, __LINE__))
35 
36 #define SCOPE_EXIT_ALL_INC_DEC(variable, offset) \
37     BOOST_SCOPE_EXIT_ALL_ID(BOOST_PP_CAT(inc, __LINE__), \
38             (=) (&variable) ) { \
39         variable += offset; \
40     }; \
41     BOOST_SCOPE_EXIT_ALL_ID(BOOST_PP_CAT(dec, __LINE__), \
42             (=) (&variable) ) { \
43         variable -= offset; \
44     };
45 
46 template<typename T>
f(T & x,T & delta)47 void f(T& x, T& delta) {
48     SCOPE_EXIT_INC_DEC_TPL(x, delta)
49     BOOST_TEST(x == 0);
50 }
51 
main(void)52 int main(void) {
53     int x = 0, delta = 10;
54 
55     {
56         SCOPE_EXIT_INC_DEC(x, delta)
57     }
58     BOOST_TEST(x == 0);
59 
60     f(x, delta);
61 
62 #ifndef BOOST_NO_CXX11_LAMBDAS
63     {
64         SCOPE_EXIT_ALL_INC_DEC(x, delta)
65     }
66     BOOST_TEST(x == 0);
67 #endif // LAMBDAS
68 
69     return boost::report_errors();
70 }
71 
72