| 1234567891011121314151617181920212223242526272829303132333435 |
- //
- // scope_guard.hpp
- // memory
- //
- // Created by Sam Jaffe on 9/24/15.
- //
- //
- #pragma once
- #include <functional>
- struct scope_exit {
- template <typename F> scope_exit(F f) : _f(f) {}
- ~scope_exit() noexcept { _f(); }
- std::function<void()> _f;
- };
- #if __cplusplus > 201402L
- template <bool B>
- struct scope_exception {
- template <typename F> scope_exception(F f) : _f(f), _except(std::uncaught_exceptions()) {}
- ~scope_exception() noexcept(B) { if (B == (std::uncaught_exceptions() > _except)) _f(); }
- std::function<void()> _f;
- int const _except;
- };
- using scope_failure = scope_exception<true>;
- using scope_success = scope_exception<false>;
- #endif
- #define CONCAT2(A, B) A##B
- #define CONCAT(A, B) CONCAT2(A, B)
- #define scope(t) scope_##t CONCAT(inline_scope_guard_##t##_,__LINE__) = [&]()
- #define scope_guard(t, f) scope_##t CONCAT(scope_guard_##t##_,__LINE__){f};
|