#pragma once #if !defined(MIJIN_UTIL_SCOPE_GUARD_HPP_INCLUDED) #define MIJIN_UTIL_SCOPE_GUARD_HPP_INCLUDED 1 #include #include "./common_macros.hpp" #define MIJIN_SCOPE_EXIT_NAMED(name) \ const mijin::ScopeExitGuard name = [&]() #define MIJIN_SCOPE_EXIT MIJIN_SCOPE_EXIT_NAMED(MIJIN_CONCAT(MIJIN_CONCAT(scope_guard_, __LINE__), __)) namespace mijin { class ScopeExitGuard { private: mutable std::function func; // variable is declared const to make clang-tidy happy, but we still want to be able to reset() public: template inline ScopeExitGuard(TFunc&& func_) noexcept : func(std::forward(func_)) {} ScopeExitGuard(const ScopeExitGuard&) noexcept = delete; ScopeExitGuard(ScopeExitGuard&&) noexcept = delete; inline ~ScopeExitGuard() noexcept { if (func) { func(); } } ScopeExitGuard& operator=(const ScopeExitGuard&) noexcept = delete; ScopeExitGuard& operator=(ScopeExitGuard&&) noexcept = delete; inline void reset() const noexcept { func = {}; } }; } #endif // defined(MIJIN_UTIL_SCOPE_GUARD_HPP_INCLUDED)