Skip to content

Commit

Permalink
ScopeGuard improvements (#651)
Browse files Browse the repository at this point in the history
`std::function` introduced a layer of indirection that can be removed through templating the class.
  • Loading branch information
ASpoonPlaysGames committed Jan 30, 2024
1 parent 350e6b1 commit 6ad955a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions primedev/util/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

void RemoveAsciiControlSequences(char* str, bool allow_color_codes);

class ScopeGuard
template <typename T> class ScopeGuard
{
public:
auto operator=(ScopeGuard&) = delete;
ScopeGuard(ScopeGuard&) = delete;

ScopeGuard(std::function<void()> callback) : m_callback(callback) {}
ScopeGuard(T callback) : m_callback(callback) {}
~ScopeGuard()
{
m_callback();
if (!m_dismissed)
m_callback();
}
void Dismiss()
{
m_callback = [] {};
m_dismissed = true;
}

private:
std::function<void()> m_callback;
bool m_dismissed = false;
T m_callback;
};

0 comments on commit 6ad955a

Please sign in to comment.