justanotheranonymoususer / scope_guard

Scope Guard & Defer C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Github Releases License Build Status Build status Codacy Badge

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_fail - 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 occure.

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 occure.
  • Scope Guard on fail

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

    person = new Person{/*...*/};
    // ...
    SCOPE_SUCCESS{ persons.push_back(person); }; // If no errors occure, 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 occure.
      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 occure.
      // ...
    };

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_succes

  • scope_succes<F> make_scope_succes(F&& action); - return scope_succes with the action.
  • SCOPE_SUCCESS{action}; - macro for creating scope_succes with the action.
  • MAKE_SCOPE_SUCCESS(name) {action}; - macro for creating named scope_succes with the action.
  • WITH_SCOPE_SUCCESS({action}) {/*...*/}; - macro for creating scope with scope_succes 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_succes 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

About

Scope Guard & Defer C++

License:MIT License


Languages

Language:C++ 84.9%Language:CMake 15.1%