Drake
ScopeExit Class Referencefinal

Detailed Description

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:

void some_function() {
void* foo = ::malloc(10);
ScopeExit guard([foo]() {
::free(foo);
});
// ...
if (condition) { throw std::runtime_error("..."); }
// ...
}

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
 
ScopeExitoperator= (const ScopeExit &)=delete
 
 ScopeExit (ScopeExit &&)=delete
 
ScopeExitoperator= (ScopeExit &&)=delete
 

Constructor & Destructor Documentation

◆ ScopeExit() [1/3]

ScopeExit ( const ScopeExit )
delete

◆ ScopeExit() [2/3]

ScopeExit ( ScopeExit &&  )
delete

◆ ScopeExit() [3/3]

ScopeExit ( std::function< void()>  func)
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()

~ScopeExit ( )

Invokes the func that was passed into the constructor, unless this has been disarmed.

Member Function Documentation

◆ Disarm()

void Disarm ( )

Disarms this guard, so that the destructor has no effect.

◆ operator=() [1/2]

ScopeExit& operator= ( const ScopeExit )
delete

◆ operator=() [2/2]

ScopeExit& operator= ( ScopeExit &&  )
delete

The documentation for this class was generated from the following file: