...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::scope::scope_exit — Scope exit guard that conditionally invokes a function upon leaving the scope.
// In header: <boost/scope/scope_exit.hpp> template<typename Func, typename Cond = always_true> class scope_exit { public: // construct/copy/destruct template<typename F> explicit scope_exit(F &&, bool = true) noexcept(...); template<typename F, typename C> explicit scope_exit(F &&, C &&, bool = true) noexcept(...); scope_exit(scope_exit &&) noexcept(...); scope_exit(scope_exit const &) = delete; scope_exit & operator=(scope_exit &&) = delete; scope_exit & operator=(scope_exit const &) = delete; ~scope_exit() noexcept(...); // public member functions bool active() const noexcept; void set_active(bool) noexcept; };
The scope guard wraps two function objects: the scope guard action and a condition for invoking the action. Both function objects must be callable with no arguments and can be one of:
A user-defined class with a public operator()
.
An lvalue reference to such class.
An lvalue reference or pointer to function taking no arguments.
The condition function object operator()
must return a value contextually convertible to true
, if the action function object is allowed to be executed, and false
otherwise. Additionally, the condition function object operator()
must not throw, as otherwise the action function object may not be called.
The condition function object is optional, and if not specified in template parameters, the scope guard will operate as if the condition always returns true
.
The scope guard can be in either active or inactive state. By default, the constructed scope guard is active. When active, and condition function object returns true
, the scope guard invokes the wrapped action function object on destruction. Otherwise, the scope guard does not call the wrapped action function object.
The scope guard can be made inactive by moving-from the scope guard or calling set_active(false)
. An inactive scope guard can be made active by calling set_active(true)
. If a moved-from scope guard is active on destruction, the behavior is undefined.
typename Func
Scope guard action function object type.
typename Cond = always_true
Scope guard condition function object type.
scope_exit
public
construct/copy/destructtemplate<typename F> explicit scope_exit(F && func, bool active = true) noexcept(...);Constructs a scope guard with a given callable action function object.
Requires: Func
is constructible from func. Cond
is nothrow default-constructible and is not a pointer to function.
Note | |
---|---|
The requirement for |
Effects: Constructs the scope guard as if by calling
.scope_exit
(std::forward< F >(func), Cond(), active)
Throws: Nothing, unless construction of the function objects throw.
Parameters: |
|
||||
Postconditions: |
|
template<typename F, typename C> explicit scope_exit(F && func, C && cond, bool active = true) noexcept(...);Constructs a scope guard with a given callable action and condition function objects.
Requires: Func
is constructible from func. Cond
is constructible from cond.
Effects: If Func
is nothrow constructible from F&&
then constructs Func
from std::forward< F >(func)
, otherwise constructs from func
. If Cond
is nothrow constructible from C&&
then constructs Cond
from std::forward< C >(cond)
, otherwise constructs from cond
.
If Func
or Cond
construction throws and active is true
, invokes cond and, if it returns true
, func before returning with the exception.
Throws: Nothing, unless construction of the function objects throw.
Parameters: |
|
||||||
Postconditions: |
|
scope_exit(scope_exit && that) noexcept(...);Move-constructs a scope guard.
Requires: Func
and Cond
are nothrow move-constructible or copy-constructible.
Effects: If Func
is nothrow move-constructible then move-constructs Func
from a member of that, otherwise copy-constructs Func
. If Cond
is nothrow move-constructible then move-constructs Cond
from a member of that, otherwise copy-constructs Cond
.
If Func
or Cond
construction throws and that.active() == true
, invokes Cond
object stored in that and, if it returns true
, Func object (either the newly constructed one, if its construction succeeded, or the original one stored in that) before returning with the exception.
If the construction succeeds, marks that as inactive.
Throws: Nothing, unless move-construction of the function objects throw.
Parameters: |
|
||
Postconditions: |
|
scope_exit(scope_exit const &) = delete;
scope_exit & operator=(scope_exit &&) = delete;
scope_exit & operator=(scope_exit const &) = delete;
~scope_exit() noexcept(...);If
active() == true
, and invoking the condition function object returns true
, invokes the wrapped callable action function object. Destroys the function objects. Throws: Nothing, unless invoking a function object throws.
scope_exit
public member functionsbool active() const noexcept;Returns
true
if the scope guard is active, otherwise false
. Note | |
---|---|
This method does not call the condition function object specified on construction. |
Throws: Nothing.
void set_active(bool active) noexcept;Activates or deactivates the scope guard.
Throws: Nothing.
Parameters: |
|
||
Postconditions: |
|