-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLifetimeChecker.mpp
More file actions
84 lines (74 loc) · 2.55 KB
/
LifetimeChecker.mpp
File metadata and controls
84 lines (74 loc) · 2.55 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
export module CppUtils.UnitTest.LifetimeChecker;
import std;
export namespace CppUtils::UnitTest
{
class LifetimeChecker
{
public:
inline LifetimeChecker(std::function<void(std::string_view)> callback, std::string name, std::size_t indentationLevel = 0):
m_callback{std::move(callback)},
m_name{std::move(name)},
m_indentationLevel{indentationLevel}
{
m_callback(std::format("{}{}()", std::string(m_indentationLevel, '\t'), m_name));
}
[[nodiscard]] inline auto getCopyCount() const -> std::size_t
{
return m_copyCount;
}
[[nodiscard]] inline auto getMoveCount() const -> std::size_t
{
return m_moveCount;
}
virtual ~LifetimeChecker()
{
m_callback(std::format("{}~{}()", std::string(m_indentationLevel, '\t'), m_name));
}
inline LifetimeChecker(const LifetimeChecker& other):
m_callback{other.m_callback},
m_name{other.m_name},
m_indentationLevel{other.m_indentationLevel},
m_copyCount{other.m_copyCount.load() + 1},
m_moveCount{other.m_moveCount.load()}
{
m_callback(std::format("{}{}(const {}&)", std::string(m_indentationLevel, '\t'), m_name, m_name));
}
inline LifetimeChecker(LifetimeChecker&& other) noexcept:
m_callback{other.m_callback},
m_name{std::move(other.m_name)},
m_indentationLevel{other.m_indentationLevel},
m_copyCount{other.m_copyCount.load()},
m_moveCount{other.m_moveCount.load() + 1}
{
other.m_name = "[invalid object]";
m_callback(std::format("{}{}({}&&)", std::string(m_indentationLevel, '\t'), m_name, m_name));
}
inline auto operator=(const LifetimeChecker& rhs) -> LifetimeChecker&
{
m_callback = rhs.m_callback;
m_name = rhs.m_name;
m_indentationLevel = rhs.m_indentationLevel;
m_copyCount.store(rhs.m_copyCount.load() + 1);
m_moveCount.store(rhs.m_moveCount.load());
m_callback(std::format("{}{}::operator=(const {}&)", std::string(m_indentationLevel, '\t'), m_name, m_name));
return *this;
}
inline auto operator=(LifetimeChecker&& rhs) noexcept -> LifetimeChecker&
{
m_callback = rhs.m_callback;
m_name = std::move(rhs.m_name);
m_indentationLevel = rhs.m_indentationLevel;
m_copyCount.store(rhs.m_copyCount.load());
m_moveCount.store(rhs.m_moveCount.load() + 1);
rhs.m_name = "[invalid object]";
m_callback(std::format("{}{}::operator=({}&&)", std::string(m_indentationLevel, '\t'), m_name, m_name));
return *this;
}
private:
std::function<void(std::string_view)> m_callback;
std::string m_name;
std::size_t m_indentationLevel;
std::atomic_size_t m_copyCount = 0;
std::atomic_size_t m_moveCount = 0;
};
}