All Projects → Neargye → scope_guard

Neargye / scope_guard

Licence: MIT license
Scope Guard & Defer C++

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to scope guard

Magic enum
Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
Stars: ✭ 2,340 (+2086.92%)
Mutual labels:  no-dependencies
Variant Lite
variant lite - A C++17-like variant, a type-safe union for C++98, C++11 and later in a single-file header-only library
Stars: ✭ 187 (+74.77%)
Mutual labels:  no-dependencies
Tcptracer Bpf
eBPF program using kprobes to trace TCP events without run-time compilation dependencies
Stars: ✭ 240 (+124.3%)
Mutual labels:  no-dependencies
Expected Lite
expected lite - Expected objects in C++11 and later in a single-file header-only library
Stars: ✭ 158 (+47.66%)
Mutual labels:  no-dependencies
Catch2
A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
Stars: ✭ 14,330 (+13292.52%)
Mutual labels:  no-dependencies
Displaceable
A tiny, performant and configurable JavaScript library that smoothly displaces elements on mouse move.
Stars: ✭ 219 (+104.67%)
Mutual labels:  no-dependencies
Eneida
Experimental demoscene project using C99 and Direct3D 12. Fully standalone with custom windows, and d3d12 headers. No Windows SDK, libc or Visual Studio dependency. C and HLSL compilers included.
Stars: ✭ 125 (+16.82%)
Mutual labels:  no-dependencies
ng-payment-card
💳 Responsive credit card component for Angular.
Stars: ✭ 27 (-74.77%)
Mutual labels:  no-dependencies
Long Press Event
Adds `long-press` event to the DOM in 1k of pure JavaScript
Stars: ✭ 187 (+74.77%)
Mutual labels:  no-dependencies
Zingchart
A declarative, efficient, and simple JavaScript library for building responsive charts
Stars: ✭ 235 (+119.63%)
Mutual labels:  no-dependencies
Mailtoui
A simple way to enhance your mailto links with a convenient user interface.
Stars: ✭ 162 (+51.4%)
Mutual labels:  no-dependencies
Zingtouch
A JavaScript touch gesture detection library for the modern web
Stars: ✭ 2,019 (+1786.92%)
Mutual labels:  no-dependencies
Validate
A lightweight form validation script.
Stars: ✭ 227 (+112.15%)
Mutual labels:  no-dependencies
Houdini
A simple, accessible show-and-hide/accordion script.
Stars: ✭ 148 (+38.32%)
Mutual labels:  no-dependencies
String View Lite
string_view lite - A C++17-like string_view for C++98, C++11 and later in a single-file header-only library
Stars: ✭ 243 (+127.1%)
Mutual labels:  no-dependencies
Climake
The simplistic, dependency-free cli library ✨
Stars: ✭ 139 (+29.91%)
Mutual labels:  no-dependencies
Leaf
Lightweight Error Augmentation Framework
Stars: ✭ 201 (+87.85%)
Mutual labels:  no-dependencies
Entia
Entia is a free, open-source, data-oriented, highly performant, parallelizable and extensible Entity-Component-System (ECS) framework written in C# especially for game development.
Stars: ✭ 28 (-73.83%)
Mutual labels:  no-dependencies
Swiped Events
Adds `swiped` events to the DOM in 0.7k of pure JavaScript
Stars: ✭ 249 (+132.71%)
Mutual labels:  no-dependencies
Medium Zoom
🔎🖼 A JavaScript library for zooming images like Medium
Stars: ✭ 2,799 (+2515.89%)
Mutual labels:  no-dependencies
  _____                         _____                     _    _____
 / ____|                       / ____|                   | |  / ____|_     _
| (___   ___ ___  _ __   ___  | |  __ _   _  __ _ _ __ __| | | |   _| |_ _| |_
 \___ \ / __/ _ \| '_ \ / _ \ | | |_ | | | |/ _` | '__/ _` | | |  |_   _|_   _|
 ____) | (_| (_) | |_) |  __/ | |__| | |_| | (_| | | | (_| | | |____|_|   |_|
|_____/ \___\___/| .__/ \___|  \_____|\__,_|\__,_|_|  \__,_|  \_____|
                 | |
                 |_|

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.

References

Licensed under the MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].