mirror of
https://github.com/eliboa/TegraRcmGUI.git
synced 2024-11-08 11:51:45 +00:00
22 lines
No EOL
555 B
C++
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) }; } |