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/config.hpp>
10#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
11# error "variadic macros required"
12#else
13
14#include <boost/scope_exit.hpp>
15#include <boost/preprocessor/cat.hpp>
16#include <boost/detail/lightweight_test.hpp>
17
18//[same_line
19#define SCOPE_EXIT_INC_DEC(variable, offset) \
20 BOOST_SCOPE_EXIT_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
21 &variable, offset) { \
22 variable += offset; \
23 } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(inc, __LINE__)) \
24 \
25 BOOST_SCOPE_EXIT_ID(BOOST_PP_CAT(dec, __LINE__), \
26 &variable, offset) { \
27 variable -= offset; \
28 } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(dec, __LINE__))
29
30#define SCOPE_EXIT_INC_DEC_TPL(variable, offset) \
31 BOOST_SCOPE_EXIT_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
32 &variable, offset) { \
33 variable += offset; \
34 } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(inc, __LINE__)) \
35 \
36 BOOST_SCOPE_EXIT_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
37 &variable, offset) { \
38 variable -= offset; \
39 } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(dec, __LINE__))
40
41#define SCOPE_EXIT_ALL_INC_DEC(variable, offset) \
42 BOOST_SCOPE_EXIT_ALL_ID(BOOST_PP_CAT(inc, __LINE__), \
43 =, &variable) { \
44 variable += offset; \
45 }; \
46 BOOST_SCOPE_EXIT_ALL_ID(BOOST_PP_CAT(dec, __LINE__), \
47 =, &variable) { \
48 variable -= offset; \
49 };
50
51template<typename T>
52void f(T& x, T& delta) {
53 SCOPE_EXIT_INC_DEC_TPL(x, delta) // Multiple scope exits on same line.
54 BOOST_TEST(x == 0);
55}
56
57int main(void) {
58 int x = 0, delta = 10;
59
60 {
61 SCOPE_EXIT_INC_DEC(x, delta) // Multiple scope exits on same line.
62 }
63 BOOST_TEST(x == 0);
64
65 f(x, delta);
66
67#ifndef BOOST_NO_CXX11_LAMBDAS
68 {
69 SCOPE_EXIT_ALL_INC_DEC(x, delta) // Multiple scope exits on same line.
70 }
71 BOOST_TEST(x == 0);
72#endif // LAMBDAS
73
74 return boost::report_errors();
75}
76//]
77
78#endif // variadic macros
79
80

source code of boost/libs/scope_exit/test/same_line.cpp