same_line.cpp 2.2 KB

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