npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

scope_guard.cxx

v0.9.1

Published

Scope Guard & Defer C++; Daniil Goncharov (2018).

Readme

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

Installation

Run:

$ npm i scope_guard.cxx

And then include scope_guard.hpp as follows:

// main.cxx
#include <scope_guard.hpp>

int main() { /* ... */ }

Finally, compile while adding the path node_modules/scope_guard.cxx to your compiler's include paths.

$ clang++ -I./node_modules/scope_guard.cxx main.cxx  # or, use g++
$ g++     -I./node_modules/scope_guard.cxx main.cxx

You may also use a simpler approach with the cpoach tool, which automatically adds the necessary include paths of all the installed dependencies for your project.

$ cpoach clang++ main.cxx  # or, use g++
$ cpoach g++     main.cxx

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

SRC ORG