Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Class template scope_fail

boost::scope::scope_fail — Scope exit guard that invokes a function upon leaving the scope, if a failure condition is satisfied.

Synopsis

// 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;
};

Description

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:

scope_exit

See Also:

scope_success

Template Parameters

  1. typename Func

    Scope guard action function object type.

  2. typename Cond = exception_checker

    Scope guard failure condition function object type.

scope_fail public construct/copy/destruct

  1. template<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:

    active

    Indicates whether the scope guard should be active upon construction.

    func

    The callable action function object to invoke on destruction.

    Postconditions:

    this->active() == active

  2. 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:

    active

    Indicates whether the scope guard should be active upon construction.

    cond

    The callable failure condition function object.

    func

    The callable action function object to invoke on destruction.

    Postconditions:

    this->active() == active

  3. 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:

    that

    Move source.

    Postconditions:

    that.active() == false

  4. scope_fail(scope_fail const &) = delete;
  5. scope_fail & operator=(scope_fail &&) = delete;
  6. scope_fail & operator=(scope_fail const &) = delete;

PrevUpHomeNext