Code Monkey home page Code Monkey logo

scopeexit's Introduction

ScopeExit - C++11 scope guard library

Build Status Build status

Introduction

ScopeExit library provides an efficient and convenient way to execute statements when execution flow leaves current scope. It implements a so-called scope guard idiom and defines 3 type of guards:

  • SCOPE_EXIT - statements are always executed on scope exit
  • SCOPE_SUCCESS - statements are executed on scope exit when no exceptions have been thrown
  • SCOPE_FAILURE - statements are executed when scope is leaving due to an exception

Using scope guards makes code much cleaner and allows to place resource allocation and clean up code next to each other. They also improve safety because cleanup code is always called independent of which paths are actually taken at runtime.

Scope guards are called in the reverse order they are defined.

Features

  • Easy to use
  • Headers only
  • Requires C++11 or higher
  • No 3rd-party dependencies
  • Cross-platform
  • Minimal overhead (no std::function or virtual table)
  • Clean code
  • CMake integration

Sample

Here is a simple sample that prints htlm code. Scope exit blocks make sure that html tags are properly closed:

#include <iostream>
#include <ScopeExit/ScopeExit.h>

using namespace std;

int main()
{
    cout << "<html>" << endl;
    SCOPE_EXIT{ cout << "</html>" << endl; };

    {
        cout << "<head>" << endl;
        SCOPE_EXIT{ cout << "</head>" << endl; };

        cout << "<title>Hello</title>" << endl;
    }

    cout << "<body>" << endl;
    SCOPE_EXIT{ cout << "</body>" << endl; };

    cout << "<h1>Hello World!</h1>" << endl;

    return 0;
}

More samples can be found in the samples folder.

References

Competing C++ scope exit/guard libraries

Other resources

License

ScopeExit is licensed under the MIT license. You can freely use it in your commercial or opensource software.

Version history

Version 1.0.0 (08 Oct 2019)

  • Initial public release

scopeexit's People

Contributors

sergiusthebest avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.