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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#pragma once
#include "base/assert.hpp"
#include "base/mutex.hpp"
#include "std/map.hpp"
#include "std/mutex.hpp"
#include "std/string.hpp"
#include "std/type_traits.hpp"
#include "std/typeinfo.hpp"
#include "std/utility.hpp"
#include "std/unique_ptr.hpp"
//#define TRACK_POINTERS
/// This class tracks usage of drape_ptr's and ref_ptr's
class DpPointerTracker
{
public:
typedef map<void *, pair<int, string> > TAlivePointers;
static DpPointerTracker & Instance();
template <typename T>
void RefPtr(T * refPtr)
{
RefPtrNamed(static_cast<void*>(refPtr), typeid(refPtr).name());
}
void DerefPtr(void * p);
void DestroyPtr(void * p);
TAlivePointers const & GetAlivePointers() const;
private:
DpPointerTracker() = default;
~DpPointerTracker();
void RefPtrNamed(void * refPtr, string const & name);
TAlivePointers m_alivePointers;
mutex m_mutex;
};
// Custom deleter for unique_ptr
class DpPointerDeleter
{
public:
template <typename T>
void operator()(T * p)
{
DpPointerTracker::Instance().DestroyPtr(p);
delete p;
}
};
#if defined(TRACK_POINTERS)
template<typename T> using drape_ptr = unique_ptr<T, DpPointerDeleter>;
#else
template<typename T> using drape_ptr = unique_ptr<T>;
#endif
template <typename T, typename... Args>
drape_ptr<T> make_unique_dp(Args &&... args)
{
return drape_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T>
class ref_ptr
{
public:
ref_ptr()
: m_ptr(nullptr), m_isOwnerUnique(false)
{}
ref_ptr(T * ptr, bool isOwnerUnique = false)
: m_ptr(ptr), m_isOwnerUnique(isOwnerUnique)
{
#if defined(TRACK_POINTERS)
if (m_isOwnerUnique)
DpPointerTracker::Instance().RefPtr(m_ptr);
#endif
}
ref_ptr(ref_ptr const & rhs)
: m_ptr(rhs.m_ptr), m_isOwnerUnique(rhs.m_isOwnerUnique)
{
#if defined(TRACK_POINTERS)
if (m_isOwnerUnique)
DpPointerTracker::Instance().RefPtr(m_ptr);
#endif
}
ref_ptr(ref_ptr && rhs)
{
m_ptr = rhs.m_ptr;
rhs.m_ptr = nullptr;
m_isOwnerUnique = rhs.m_isOwnerUnique;
rhs.m_isOwnerUnique = false;
}
~ref_ptr()
{
#if defined(TRACK_POINTERS)
if (m_isOwnerUnique)
DpPointerTracker::Instance().DerefPtr(m_ptr);
#endif
m_ptr = nullptr;
}
T * operator->() const { return m_ptr; }
template<typename TResult>
operator ref_ptr<TResult>() const
{
static_assert(is_base_of<TResult, T>::value || is_base_of<T, TResult>::value ||
is_void<T>::value || is_void<TResult>::value, "");
return ref_ptr<TResult>(static_cast<TResult *>(m_ptr), m_isOwnerUnique);
}
template<typename TResult>
ref_ptr<TResult> downcast() const
{
ASSERT(dynamic_cast<TResult *>(m_ptr) != nullptr, ());
return ref_ptr<TResult>(static_cast<TResult *>(m_ptr), m_isOwnerUnique);
}
operator bool() const { return m_ptr != nullptr; }
bool operator==(ref_ptr const & rhs) const { return m_ptr == rhs.m_ptr; }
bool operator==(T * rhs) const { return m_ptr == rhs; }
bool operator!=(ref_ptr const & rhs) const { return !operator==(rhs); }
bool operator!=(T * rhs) const { return !operator==(rhs); }
bool operator<(ref_ptr const & rhs) const { return m_ptr < rhs.m_ptr; }
template<typename TResult, class = typename enable_if<!is_void<TResult>::value>::type>
TResult & operator*() const
{
return *m_ptr;
}
ref_ptr & operator=(ref_ptr const & rhs)
{
if (this == &rhs)
return *this;
#if defined(TRACK_POINTERS)
if (m_isOwnerUnique)
DpPointerTracker::Instance().DerefPtr(m_ptr);
#endif
m_ptr = rhs.m_ptr;
m_isOwnerUnique = rhs.m_isOwnerUnique;
#if defined(TRACK_POINTERS)
if (m_isOwnerUnique)
DpPointerTracker::Instance().RefPtr(m_ptr);
#endif
return *this;
}
ref_ptr & operator=(ref_ptr && rhs)
{
if (this == &rhs)
return *this;
#if defined(TRACK_POINTERS)
if (m_isOwnerUnique)
DpPointerTracker::Instance().DerefPtr(m_ptr);
#endif
m_ptr = rhs.m_ptr;
rhs.m_ptr = nullptr;
m_isOwnerUnique = rhs.m_isOwnerUnique;
rhs.m_isOwnerUnique = false;
return *this;
}
T * get() const { return m_ptr; }
private:
T* m_ptr;
bool m_isOwnerUnique;
template <typename TResult>
friend inline string DebugPrint(ref_ptr<TResult> const & v);
};
template <typename T>
inline string DebugPrint(ref_ptr<T> const & v)
{
return DebugPrint(v.m_ptr);
}
template <typename T>
ref_ptr<T> make_ref(drape_ptr<T> const & drapePtr)
{
return ref_ptr<T>(drapePtr.get(), true);
}
template <typename T>
ref_ptr<T> make_ref(T* ptr)
{
return ref_ptr<T>(ptr, false);
}
|