// // scope_guard.hpp // memory // // Created by Sam Jaffe on 9/24/15. // // #pragma once #include struct scope_exit { template scope_exit(F f) : _f(f) {} ~scope_exit() noexcept { _f(); } std::function _f; }; #if __cplusplus > 201402L template struct scope_exception { template scope_exception(F f) : _f(f), _except(std::uncaught_exceptions()) {} ~scope_exception() noexcept(B) { if (B == (std::uncaught_exceptions() > _except)) _f(); } std::function _f; int const _except; }; using scope_failure = scope_exception; using scope_success = scope_exception; #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};