1
0
Fork 0
mirror of https://github.com/eliboa/TegraRcmGUI.git synced 2024-11-28 13:12:05 +00:00
TegraRcmGUI/TegraRcmGUI/ScopeGuard.h
2018-06-10 16:21:59 +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) }; }