/// @author Yury Melnichek mailto:melnichek@gmail.com /// @date 07.01.2005 /// @brief See http://gzip.rsdn.ru/forum/Message.aspx?mid=389127&only=1. #pragma once #include "base/base.hpp" namespace my { namespace impl { /// Base class for all ScopeGuards. class GuardBase { public: /// Disable ScopeGuard functionality on it's destruction void release() const { m_bDoRollback = false; } protected: GuardBase() : m_bDoRollback(true) { } // Do something in the destructor mutable bool m_bDoRollback; }; /// ScopeGuard for specific functor template class GuardImpl : public GuardBase { public: explicit GuardImpl(TFunctor const & F) : m_Functor(F) { } ~GuardImpl() { if (m_bDoRollback) m_Functor(); } private: TFunctor m_Functor; }; } // namespace impl typedef impl::GuardBase const & scope_guard; /// Create scope_guard template impl::GuardImpl make_scope_guard(TFunctor const & F) { return impl::GuardImpl(F); } } // namespace my #define MY_SCOPE_GUARD(name, func) \ ::my::scope_guard name = my::make_scope_guard(func); static_cast(name);