00001 #ifndef toast_scope_guard_hpp_INCLUDED 00002 #define toast_scope_guard_hpp_INCLUDED 00003 00004 #include <boost/function.hpp> 00005 #include <boost/utility.hpp> 00006 00013 namespace toast { 00014 00028 class scope_guard : boost::noncopyable 00029 { 00030 public: 00031 explicit scope_guard(boost::function<void ()> const &f) 00032 : f_(f) {} 00033 void dismiss() { f_ = 0; } 00034 ~scope_guard() try 00035 { 00036 if(f_) { 00037 f_(); 00038 } 00039 } catch (...) { std::terminate(); } 00040 private: 00041 boost::function<void ()> f_; 00042 }; 00043 00046 } 00047 00048 #endif // toast_scope_guard_hpp_INCLUDED