same_line_seq.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <boost/scope_exit.hpp>
  8. #include <boost/preprocessor/cat.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #define SCOPE_EXIT_INC_DEC(variable, offset) \
  12. BOOST_SCOPE_EXIT_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
  13. (&variable) (offset) ) { \
  14. variable += offset; \
  15. } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(inc, __LINE__)) \
  16. \
  17. BOOST_SCOPE_EXIT_ID(BOOST_PP_CAT(dec, __LINE__), \
  18. (&variable) (offset) ) { \
  19. variable -= offset; \
  20. } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(dec, __LINE__))
  21. #define SCOPE_EXIT_INC_DEC_TPL(variable, offset) \
  22. BOOST_SCOPE_EXIT_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
  23. (&variable) (offset) ) { \
  24. variable += offset; \
  25. } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(inc, __LINE__)) \
  26. \
  27. BOOST_SCOPE_EXIT_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
  28. (&variable) (offset) ) { \
  29. variable -= offset; \
  30. } BOOST_SCOPE_EXIT_END_ID(BOOST_PP_CAT(dec, __LINE__))
  31. #define SCOPE_EXIT_ALL_INC_DEC(variable, offset) \
  32. BOOST_SCOPE_EXIT_ALL_ID(BOOST_PP_CAT(inc, __LINE__), \
  33. (=) (&variable) ) { \
  34. variable += offset; \
  35. }; \
  36. BOOST_SCOPE_EXIT_ALL_ID(BOOST_PP_CAT(dec, __LINE__), \
  37. (=) (&variable) ) { \
  38. variable -= offset; \
  39. };
  40. template<typename T>
  41. void f(T& x, T& delta) {
  42. SCOPE_EXIT_INC_DEC_TPL(x, delta)
  43. BOOST_TEST(x == 0);
  44. }
  45. int main(void) {
  46. int x = 0, delta = 10;
  47. {
  48. SCOPE_EXIT_INC_DEC(x, delta)
  49. }
  50. BOOST_TEST(x == 0);
  51. f(x, delta);
  52. #ifndef BOOST_NO_CXX11_LAMBDAS
  53. {
  54. SCOPE_EXIT_ALL_INC_DEC(x, delta)
  55. }
  56. BOOST_TEST(x == 0);
  57. #endif // LAMBDAS
  58. return boost::report_errors();
  59. }