Helper class to create a scope exit guard – an object that when destroyed runs func
.
This is useful to apply RAII to third-party code that only supports manual acquire and release operations.
Example:
Here, the allocation of foo
will always be free'd no matter whether some_function
returns normally or via an exception.
#include <drake/common/scope_exit.h>
Public Member Functions | |
ScopeExit (std::function< void()> func) | |
Creates a resource that will call func when destroyed. More... | |
~ScopeExit () | |
Invokes the func that was passed into the constructor, unless this has been disarmed. More... | |
void | Disarm () |
Disarms this guard, so that the destructor has no effect. More... | |
Does not allow copy, move, or assignment | |
ScopeExit (const ScopeExit &)=delete | |
ScopeExit & | operator= (const ScopeExit &)=delete |
ScopeExit (ScopeExit &&)=delete | |
ScopeExit & | operator= (ScopeExit &&)=delete |
|
explicit |
Creates a resource that will call func
when destroyed.
Note that func()
should not throw an exception, since it will typically be invoked during stack unwinding.
~ScopeExit | ( | ) |
Invokes the func
that was passed into the constructor, unless this has been disarmed.
void Disarm | ( | ) |
Disarms this guard, so that the destructor has no effect.