ScopeGuard improvements (#651)

`std::function` introduced a layer of indirection that can be removed through templating the class.
This commit is contained in:
Jack 2024-01-30 22:06:40 +00:00 committed by GitHub
parent 350e6b1463
commit 6ad955ae0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 5 deletions

View File

@ -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;
};