Scope guard - Scope Guard & Defer C++

Overview
  _____                         _____                     _    _____
 / ____|                       / ____|                   | |  / ____|_     _
| (___   ___ ___  _ __   ___  | |  __ _   _  __ _ _ __ __| | | |   _| |_ _| |_
 \___ \ / __/ _ \| '_ \ / _ \ | | |_ | | | |/ _` | '__/ _` | | |  |_   _|_   _|
 ____) | (_| (_) | |_) |  __/ | |__| | |_| | (_| | | | (_| | | |____|_|   |_|
|_____/ \___\___/| .__/ \___|  \_____|\__,_|\__,_|_|  \__,_|  \_____|
                 | |
                 |_|

Github Releases License

Scope Guard & Defer C++

Scope Guard statement invokes a function with deferred execution until surrounding function returns in cases:

  • scope_exit - executing action on scope exit.

  • scope_fail - executing action on scope exit when an exception has been thrown.

  • scope_success - executing action on scope exit when no exceptions have been thrown.

Program control transferring does not influence Scope Guard statement execution. Hence, Scope Guard statement can be used to perform manual resource management, such as file descriptors closing, and to perform actions even if an error occurs.

Features

  • C++11
  • Header-only
  • Dependency-free
  • Thin callback wrapping, no added std::function or virtual table penalties
  • No implicitly ignored return, check callback return void
  • Defer or Scope Guard syntax and "With" syntax

Examples

  • Scope Guard on exit

    std::fstream file("test.txt");
    SCOPE_EXIT{ file.close(); }; // File closes when exit the enclosing scope or errors occur.
  • Scope Guard on fail

    persons.push_back(person); // Add the person to db.
    SCOPE_FAIL{ persons.pop_back(); }; // If errors occur, we should roll back.
  • Scope Guard on success

    person = new Person{/*...*/};
    // ...
    SCOPE_SUCCESS{ persons.push_back(person); }; // If no errors occur, we should add the person to db.
  • Custom Scope Guard

    persons.push_back(person); // Add the person to db.
    
    MAKE_SCOPE_EXIT(scope_exit) { // Following block is executed when exit the enclosing scope or errors occur.
      persons.pop_back(); // If the db insertion fails, we should roll back.
    };
    // MAKE_SCOPE_EXIT(name) {action} - macro is used to create a new scope_exit object.
    scope_exit.dismiss(); // An exception was not thrown, so don't execute the scope_exit.
    persons.push_back(person); // Add the person to db.
    
    auto scope_exit = make_scope_exit([]() { persons.pop_back(); });
    // make_scope_exit(A&& action) - function is used to create a new scope_exit object. It can be instantiated with a lambda function, a std::function<void()>, a functor, or a void(*)() function pointer.
    // ...
    scope_exit.dismiss(); // An exception was not thrown, so don't execute the scope_exit.
  • With Scope Guard

    std::fstream file("test.txt");
    WITH_SCOPE_EXIT({ file.close(); }) { // File closes when exit the enclosing with scope or errors occur.
      // ...
    };

Synopsis

Reference

scope_exit

  • scope_exit<F> make_scope_exit(F&& action); - return scope_exit with the action.
  • SCOPE_EXIT{action}; - macro for creating scope_exit with the action.
  • MAKE_SCOPE_EXIT(name) {action}; - macro for creating named scope_exit with the action.
  • WITH_SCOPE_EXIT({action}) {/*...*/}; - macro for creating scope with scope_exit with the action.

scope_fail

  • scope_fail<F> make_scope_fail(F&& action); - return scope_fail with the action.
  • SCOPE_FAIL{action}; - macro for creating scope_fail with the action.
  • MAKE_SCOPE_FAIL(name) {action}; - macro for creating named scope_fail with the action.
  • WITH_SCOPE_FAIL({action}) {/*...*/}; - macro for creating scope with scope_fail with the action.

scope_success

  • scope_success<F> make_scope_success(F&& action); - return scope_success with the action.
  • SCOPE_SUCCESS{action}; - macro for creating scope_success with the action.
  • MAKE_SCOPE_SUCCESS(name) {action}; - macro for creating named scope_success with the action.
  • WITH_SCOPE_SUCCESS({action}) {/*...*/}; - macro for creating scope with scope_success with the action.

defer

  • DEFER{action}; - macro for creating defer with the action.
  • MAKE_DEFER(name) {action}; - macro for creating named defer with the action.
  • WITH_DEFER({action}) {/*...*/}; - macro for creating scope with defer with the action.

Interface of scope_guard

scope_exit, scope_fail, scope_success implement scope_guard interface.

  • dismiss() - dismiss executing action on scope exit.

Throwable settings

  • SCOPE_GUARD_NOTHROW_CONSTRUCTIBLE define this to require nothrow constructible action.

  • SCOPE_GUARD_MAY_THROW_ACTION define this to action may throw exceptions.

  • SCOPE_GUARD_NO_THROW_ACTION define this to require noexcept action.

  • SCOPE_GUARD_SUPPRESS_THROW_ACTIONS define this to exceptions during action will be suppressed.

  • By default using SCOPE_GUARD_MAY_THROW_ACTION.

  • SCOPE_GUARD_CATCH_HANDLER define this to add exceptions handler. If SCOPE_GUARD_SUPPRESS_THROW_ACTIONS is not defined, it will do nothing.

Remarks

  • If multiple Scope Guard statements appear in the same scope, the order they appear is the reverse of the order they are executed.

    void f() {
      SCOPE_EXIT{ std::cout << "First" << std::endl; };
      SCOPE_EXIT{ std::cout << "Second" << std::endl; };
      SCOPE_EXIT{ std::cout << "Third" << std::endl; };
      ... // Other code.
      // Prints "Third".
      // Prints "Second".
      // Prints "First".
    }

Integration

You should add required file scope_guard.hpp.

Compiler compatibility

  • Clang/LLVM >= 5
  • MSVC++ >= 14.0 / Visual Studio >= 2015
  • Xcode >= 9
  • GCC >= 5

References

Licensed under the MIT License

You might also like...
Releases(v0.9.1)
Owner
Daniil Goncharov
Daniil Goncharov
This is a list of hardware which is supports Intel SGX - Software Guard Extensions.

SGX-hardware list This is a list of hardware which supports Intel SGX - Software Guard Extensions. Desktop The CPU and the motherboard BIOS must suppo

Lars Lühr 507 Oct 1, 2022