diff --git a/include/fc/scoped_exit.hpp b/include/fc/scoped_exit.hpp new file mode 100644 index 0000000..98ec16d --- /dev/null +++ b/include/fc/scoped_exit.hpp @@ -0,0 +1,32 @@ +#pragma once + +namespace fc { + + template + class scoped_exit { + public: + template + scoped_exit( C&& c ):callback( std::forward(c) ){} + scoped_exit( scoped_exit&& mv ):callback( std::move( mv.callback ) ){} + + ~scoped_exit() { + try { callback(); } catch( ... ) {} + } + + scoped_exit& operator = ( scoped_exit&& mv ) { + callback = std::move(mv); + return *this; + } + private: + scoped_exit( const scoped_exit& ); + scoped_exit& operator=( const scoped_exit& ); + + Callback callback; + }; + + template + scoped_exit make_scoped_exit( Callback&& c ) { + return scoped_exit( std::forward(c) ); + } + +}