forked from Soreepeong/XivAlexander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils_CallOnDestruction.cpp
44 lines (37 loc) · 1.12 KB
/
Utils_CallOnDestruction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "pch.h"
#include "Utils_CallOnDestruction.h"
/// Constructors that will not call anything on destruction
Utils::CallOnDestruction::CallOnDestruction() noexcept = default;
Utils::CallOnDestruction::CallOnDestruction(std::nullptr_t) noexcept {}
/// Constructor that will call something on destruction
Utils::CallOnDestruction::CallOnDestruction(std::function<void()> fn) :
m_fn(std::move(fn)) {
}
/// Constructor that will move the destruction callback from r to this
Utils::CallOnDestruction::CallOnDestruction(CallOnDestruction && r) noexcept {
m_fn = std::move(r.m_fn);
r.m_fn = nullptr;
}
/// Movement operator
Utils::CallOnDestruction& Utils::CallOnDestruction::operator=(CallOnDestruction && r) noexcept {
if (m_fn)
m_fn();
m_fn = std::move(r.m_fn);
r.m_fn = nullptr;
return *this;
}
/// Null assignment operator
Utils::CallOnDestruction& Utils::CallOnDestruction::operator=(std::nullptr_t) noexcept {
if (!m_fn)
return *this;
m_fn();
m_fn = nullptr;
return *this;
}
Utils::CallOnDestruction::~CallOnDestruction() {
if (m_fn)
m_fn();
}
Utils::CallOnDestruction::operator bool() const {
return !!m_fn;
}