1
0
Fork 0
mirror of https://github.com/eliboa/TegraRcmGUI.git synced 2024-12-01 06:32:05 +00:00
TegraRcmGUI/ScopeGuard.h

22 lines
555 B
C
Raw Normal View History

2018-05-10 23:39:24 +01:00
#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) }; }