ScopeGuard improvements (#651)
`std::function` introduced a layer of indirection that can be removed through templating the class.
This commit is contained in:
parent
350e6b1463
commit
6ad955ae0a
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue