Giter Site home page Giter Site logo

state_saver's Introduction

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

Github Releases License

State Saver C++

Sometimes a certain value has to change only for a limited scope. This class wrapper saves a copy of the current state of some object, and resets the object’s state at destruction time, undoing any change the object may have gone through.

  • saver_exit - saves the original variable value and restores on scope exit.

  • saver_fail - saves the original variable value and restores on scope exit when an exception has been thrown.

  • saver_success - saves the original variable value and restores on scope exit when no exceptions have been thrown.

Features

  • C++11
  • Header-only
  • Dependency-free
  • State Saver on exit

    void Foo(A& a)
      SAVER_EXIT(a);
      ... // Alter state from non-const functions.
      ... // Other code.
      // Original state automatically restored on scope exit.
    }
  • State Saver on fail

    void Foo(A& a)
      SAVER_FAIL(a);
      ... // Alter state from non-const functions.
      ... // Other code.
      // Original state automatically restored when an exception has been thrown.
    }
  • State Saver on success

    void Foo(A& a)
      SAVER_SUCCESS(a);
      ... // Alter state from non-const functions.
      ... // Other code.
      // Original state automatically restored when no exceptions have been thrown.
    }

Synopsis

Reference

saver_exit

  • saver_exit<decltype(object)> state_saver{object}; - creation saver_exit for the object.
  • SAVER_EXIT{object}; - macro for creating saver_exit for the object.
  • MAKE_SAVER_EXIT(name) {object}; - macro for creating named saver_exit for the object.
  • WITH_SAVER_EXIT(object) {/*...*/}; - macro for creating scope with saver_exit for the object.

saver_fail

  • saver_fail<decltype(object)> state_saver{object}; - creation saver_fail for the object.
  • SAVER_FAIL{object}; - macro for creating saver_fail for the object.
  • MAKE_SAVER_FAIL(name) {object}; - macro for creating scope with saver_fail for the object.
  • WITH_SAVER_FAIL(object) {/*...*/}; - macro for creating scope with saver_fail for the object.

saver_success

  • saver_success<decltype(object)> state_saver{object}; - creation saver_success for the object.
  • SAVER_SUCCESS{object}; - macro for creating saver_success for the object.
  • MAKE_SAVER_SUCCESS(name) {object}; - macro for creating scope with saver_success for the object.
  • WITH_SAVER_SUCCESS(object) {/*...*/}; - macro for creating scope with saver_success for the object.

Interface of state_saver

saver_exit, saver_fail, saver_success implement state_saver interface.

  • constructor state_saver(T& object) - construct state_saver with saved object.

  • dismiss() - dismiss restore on scope exit.

  • restore() - resets the object’s state. Requirements copy operator =.

Requirements to saved object

  • POD or object semantic (cannot be reference, function, ...).
  • Copy constructor.
  • operator= (no-throw one preferred).

Configuration macros

Throwable settings

  • STATE_SAVER_NO_THROW_CONSTRUCTIBLE define this to require nothrow constructible.

  • STATE_SAVER_MAY_THROW_RESTORE define this to restore may throw exceptions.

  • STATE_SAVER_NO_THROW_RESTORE define this to require noexcept restore.

  • STATE_SAVER_SUPPRESS_THROW_RESTORE define this to exceptions during restore will be suppressed.

  • By default using STATE_SAVER_MAY_THROW_RESTORE.

  • STATE_SAVER_CATCH_HANDLER define this to add exceptions handler. If STATE_SAVER_SUPPRESS_THROW_RESTORE is not defined, it will do nothing.

Assignable settings

  • STATE_SAVER_FORCE_MOVE_ASSIGNABLE define this to restore on scope exit will be move assigned.

  • STATE_SAVER_FORCE_COPY_ASSIGNABLE define this to restore on scope exit will be copy assigned.

  • By default using move assignable if noexcept.

Integration

You should add required file state_saver.hpp.

Compiler compatibility

  • Clang/LLVM >= 5
  • MSVC++ >= 14.0 / Visual Studio >= 2015
  • Xcode >= 9
  • GCC >= 5

Licensed under the MIT License

state_saver's People

Contributors

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