scope_exit.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #ifndef SCOPE_EXIT_HPP_
  7. #define SCOPE_EXIT_HPP_
  8. #include <boost/local_function.hpp>
  9. #include <boost/local_function/detail/preprocessor/line_counter.hpp>
  10. #include <boost/function.hpp>
  11. #include <boost/preprocessor/cat.hpp>
  12. #include <boost/config.hpp>
  13. //[scope_exit_class
  14. struct scope_exit {
  15. scope_exit(boost::function<void (void)> f): f_(f) {}
  16. ~scope_exit(void) { f_(); }
  17. private:
  18. boost::function<void (void)> f_;
  19. };
  20. //]
  21. // PRIVATE //
  22. //[scope_exit_end_macro
  23. #define SCOPE_EXIT_END_(id) \
  24. BOOST_LOCAL_FUNCTION_NAME(BOOST_PP_CAT(scope_exit_func_, id)) \
  25. scope_exit BOOST_PP_CAT(scope_exit_, id)( \
  26. BOOST_PP_CAT(scope_exit_func_, id));
  27. //]
  28. // PUBLIC //
  29. #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
  30. # define SCOPE_EXIT(void_or_seq) \
  31. void BOOST_LOCAL_FUNCTION(void_or_seq)
  32. #else
  33. //[scope_exit_macro
  34. # define SCOPE_EXIT(...) \
  35. void BOOST_LOCAL_FUNCTION(__VA_ARGS__)
  36. //]
  37. #endif
  38. #define SCOPE_EXIT_END \
  39. SCOPE_EXIT_END_(BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER)
  40. #endif // #include guard