提交 ae4e4c7f 编写于 作者: N Nathan Hourt

[FC] Add cancel() to scoped_exit

Allow scoped_exit objects to be canceled so they do not call their
callbacks when destroyed.
上级 f7c60216
...@@ -9,12 +9,15 @@ namespace fc { ...@@ -9,12 +9,15 @@ namespace fc {
scoped_exit( C&& c ):callback( std::forward<C>(c) ){} scoped_exit( C&& c ):callback( std::forward<C>(c) ){}
scoped_exit( scoped_exit&& mv ):callback( std::move( mv.callback ) ){} scoped_exit( scoped_exit&& mv ):callback( std::move( mv.callback ) ){}
void cancel() { canceled = true; }
~scoped_exit() { ~scoped_exit() {
if (!canceled)
try { callback(); } catch( ... ) {} try { callback(); } catch( ... ) {}
} }
scoped_exit& operator = ( scoped_exit&& mv ) { scoped_exit& operator = ( scoped_exit&& mv ) {
callback = std::move(mv); callback = std::move(mv.callback);
return *this; return *this;
} }
private: private:
...@@ -22,6 +25,7 @@ namespace fc { ...@@ -22,6 +25,7 @@ namespace fc {
scoped_exit& operator=( const scoped_exit& ); scoped_exit& operator=( const scoped_exit& );
Callback callback; Callback callback;
bool canceled = false;
}; };
template<typename Callback> template<typename Callback>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册