2015-10-05 00:33:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace ext
|
|
|
|
{
|
|
|
|
|
|
|
|
template <class F> class scope_guard {
|
2017-06-06 17:10:04 +00:00
|
|
|
const F function;
|
2015-10-05 00:33:43 +00:00
|
|
|
|
|
|
|
public:
|
2017-06-06 17:10:04 +00:00
|
|
|
constexpr scope_guard(const F & function) : function{function} {}
|
|
|
|
constexpr scope_guard(F && function) : function{std::move(function)} {}
|
|
|
|
~scope_guard() { function(); }
|
2015-10-05 00:33:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
inline scope_guard<F> make_scope_guard(F && function) { return std::forward<F>(function); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SCOPE_EXIT_CONCAT(n, ...) \
|
|
|
|
const auto scope_exit##n = ext::make_scope_guard([&] { __VA_ARGS__; })
|
|
|
|
#define SCOPE_EXIT_FWD(n, ...) SCOPE_EXIT_CONCAT(n, __VA_ARGS__)
|
|
|
|
#define SCOPE_EXIT(...) SCOPE_EXIT_FWD(__LINE__, __VA_ARGS__)
|
|
|
|
|