1
0
Fork 0
mirror of https://github.com/eliboa/TegraRcmGUI.git synced 2024-11-10 04:31:43 +00:00
TegraRcmGUI/ScopeGuard.h
2018-05-11 00:39:24 +02:00

22 lines
No EOL
555 B
C++

#pragma once
template<typename Func>
class ScopeGuard
{
public:
ScopeGuard(Func&& runFunc) : exitFunc(std::forward<Func>(runFunc)), shouldRun(true) {}
~ScopeGuard()
{
if (shouldRun)
exitFunc();
}
bool reset() { auto prevRun = shouldRun; shouldRun = false; return prevRun; }
bool run() { auto prevRun = shouldRun; if (shouldRun) { exitFunc(); shouldRun = false; } return prevRun; }
private:
Func exitFunc;
bool shouldRun;
};
template<typename Func>
ScopeGuard<Func> MakeScopeGuard(Func&& theFunc) { return { std::forward<Func>(theFunc) }; }