#pragma once #include #include namespace sead { template class ScopeGuard final { public: ScopeGuard(Function&& function) : mFunction(std::move(function)) {} ScopeGuard(ScopeGuard&& other) noexcept { mFunction = std::move(other.mFunction); other.dismiss(); } ~ScopeGuard() { exit(); } void dismiss() { mFunction.reset(); } void exit() { if (!mFunction) return; (*mFunction)(); dismiss(); } private: std::optional mFunction; }; /// To work around the lack of CTAD in compilers with incomplete C++17 support. template ScopeGuard makeScopeGuard(Function&& function) { return ScopeGuard(std::forward(function)); } } // namespace sead