Giter Site home page Giter Site logo

libindi-scope's Introduction

libindi-scope

C++ version Licence

An implementation of the proposed (P0052) C++ <scope> header

Latest release Latest release Latest release date
CI Status Travis CI (main branch) Travis CI (develop branch)
Issues Open issues Open pull requests
P0052 revision P0052 revision

Overview

C++ standards proposal P0052 describes a new standard library header <scope> that includes scope guards. Scope guards provide ad-hoc RAII functionality, which is particularly useful when using C library facilities in modern C++ code.

This project is all about the single header scope.hpp, which aims to be a drop-in replacement for <scope> (except that everything is in the indi namespace, rather than std). The goal is that you can take the scope.hpp header and drop it into any project to be able to use P0052โ€™s scope guards. (And if and when <scope> is standardized, you can simply replace the include directive and namespace, and everything should work exactly the same.)

Library

The following is only a brief overview of the provided library facilities. For details, see the revision of P0052 that is currently tracked by this project.

Scope guards

The header scope.hpp provides 3 scope guards:

  • scope_exit
  • scope_success
  • scope_fail

All three scope guards can be initialized with a function or reference to a function, which is then called when the scope guard is destroyed. The only difference between the three scope guards are the conditions when the function is called.

Scope guard When function is called
scope_exit Whenever the scope guard is destroyed.
scope_success Only if the scope guard is destroyed normally (not via stack unwinding).
scope_fail Only if the scope guard is destroyed via stack unwinding.

Example:

auto f()
{
    auto const s1 = scope_exit   {[] { std::cout << "exit!"; }};
    auto const s2 = scope_success{[] { std::cout << "good!"; }};
    auto const s3 = scope_fail   {[] { std::cout << "fail!"; }};

    // [...]

    // "fail!" will be printed ONLY if an exception was thrown above.
    // "good!" will be printed ONLY if an exception was *NOT* thrown.
    // "exit!" will be printed, no matter what happened above.
}

All scope guards also have a release() member function, that prevents the wrapped function from being called under any circumstances.

libindi-scope's People

Contributors

darkerstar avatar

Stargazers

 avatar

Watchers

 avatar  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.