scope_guard.hpp 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*! \file
  2. * \brief TODO.
  3. *
  4. * TODO.
  5. *
  6. * Copyright (c) 2005-2007 Philipp Henkel
  7. *
  8. * Use, modification, and distribution are subject to the
  9. * Boost Software License, Version 1.0. (See accompanying file
  10. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  11. *
  12. * http://threadpool.sourceforge.net
  13. *
  14. */
  15. #ifndef THREADPOOL_DETAIL_SCOPE_GUARD_HPP_INCLUDED
  16. #define THREADPOOL_DETAIL_SCOPE_GUARD_HPP_INCLUDED
  17. #include <boost/function.hpp>
  18. namespace boost { namespace threadpool { namespace detail
  19. {
  20. // TODO documentation
  21. class scope_guard
  22. : private boost::noncopyable
  23. {
  24. function0<void> const m_function;
  25. bool m_is_active;
  26. public:
  27. scope_guard(function0<void> const & call_on_exit)
  28. : m_function(call_on_exit)
  29. , m_is_active(true)
  30. {
  31. }
  32. ~scope_guard()
  33. {
  34. if(m_is_active && m_function)
  35. {
  36. m_function();
  37. }
  38. }
  39. void disable()
  40. {
  41. m_is_active = false;
  42. }
  43. };
  44. } } } // namespace boost::threadpool::detail
  45. #endif // THREADPOOL_DETAIL_SCOPE_GUARD_HPP_INCLUDED