...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_fail — Scope exit guard that invokes a function upon leaving the scope, if a failure condition is satisfied.
// In header: <boost/scope/scope_fail.hpp> template<typename Func, typename Cond = exception_checker> class scope_fail : public boost::scope::scope_exit< Func, exception_checker > { public: // construct/copy/destruct template<typename F> explicit scope_fail(F &&, bool = true) noexcept(...); template<typename F, typename C> explicit scope_fail(F &&, C &&, bool = true) noexcept(...); scope_fail(scope_fail &&) noexcept(...); scope_fail(scope_fail const &) = delete; scope_fail & operator=(scope_fail &&) = delete; scope_fail & operator=(scope_fail const &) = delete; };
The scope guard wraps two function objects: the scope guard action and a failure 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 failure is detected and the action function object is allowed to be executed, and false
otherwise. Additionally, the failure condition function object operator()
must not throw, as otherwise the action function object may not be called. If not specified, the default failure condition checks whether the scope is left due to an exception - the action function object will not be called if the scope is left normally.
See Also:
See Also:
typename Func
Scope guard action function object type.
typename Cond = exception_checker
Scope guard failure condition function object type.
scope_fail
public
construct/copy/destructtemplate<typename F> explicit scope_fail(F && func, bool active = true) noexcept(...);Constructs a scope guard with a given callable function object.
Requires: Func
is constructible from func. Cond
is nothrow default-constructible.
Effects: Constructs the scope guard as if by calling
.scope_fail
(std::forward< F >(func), Cond(), active)
Throws: Nothing, unless construction of the function objects throw.
Parameters: |
|
||||
Postconditions: |
|
template<typename F, typename C> explicit scope_fail(F && func, C && cond, bool active = true) noexcept(...);Constructs a scope guard with a given callable action and failure 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_fail(scope_fail && 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_fail(scope_fail const &) = delete;
scope_fail & operator=(scope_fail &&) = delete;
scope_fail & operator=(scope_fail const &) = delete;