Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pointers.hpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22ef786f9cb50af9680220a2cb39a26ca0136d80 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#pragma once

#include "drape/drape_diagnostics.hpp"

#include "base/assert.hpp"
#include "base/mutex.hpp"

#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <type_traits>
#include <typeinfo>
#include <utility>

// This class tracks usage of drape_ptr's and ref_ptr's
class DpPointerTracker
{
public:
  typedef std::map<void *, std::pair<int, std::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, std::string const & name);

  TAlivePointers m_alivePointers;
  std::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 = std::unique_ptr<T, DpPointerDeleter>;
#else
template <typename T>
using drape_ptr = std::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() noexcept
    : m_ptr(nullptr)
  {
#if defined(TRACK_POINTERS)
    m_isOwnerUnique = false;
#endif
  }

#if defined(TRACK_POINTERS)
  ref_ptr(T * ptr, bool isOwnerUnique = false) noexcept
#else
  ref_ptr(T * ptr) noexcept
#endif
    : m_ptr(ptr)
  {
#if defined(TRACK_POINTERS)
    m_isOwnerUnique = isOwnerUnique;
    if (m_isOwnerUnique)
      DpPointerTracker::Instance().RefPtr(m_ptr);
#endif
  }

  ref_ptr(ref_ptr const & rhs) noexcept
    : m_ptr(rhs.m_ptr)
  {
#if defined(TRACK_POINTERS)
    m_isOwnerUnique = rhs.m_isOwnerUnique;
    if (m_isOwnerUnique)
      DpPointerTracker::Instance().RefPtr(m_ptr);
#endif
  }

  ref_ptr(ref_ptr && rhs) noexcept
  {
    m_ptr = rhs.m_ptr;
    rhs.m_ptr = nullptr;

#if defined(TRACK_POINTERS)
    m_isOwnerUnique = rhs.m_isOwnerUnique;
    rhs.m_isOwnerUnique = false;
#endif
  }

  ~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(std::is_base_of<TResult, T>::value || std::is_base_of<T, TResult>::value ||
                  std::is_void<T>::value || std::is_void<TResult>::value, "");

#if defined(TRACK_POINTERS)
    return ref_ptr<TResult>(static_cast<TResult *>(m_ptr), m_isOwnerUnique);
#else
    return ref_ptr<TResult>(static_cast<TResult *>(m_ptr));
#endif
  }

  template<typename TResult>
  ref_ptr<TResult> downcast() const
  {
    ASSERT(dynamic_cast<TResult *>(m_ptr) != nullptr, ());

#if defined(TRACK_POINTERS)
    return ref_ptr<TResult>(static_cast<TResult *>(m_ptr), m_isOwnerUnique);
#else
    return ref_ptr<TResult>(static_cast<TResult *>(m_ptr));
#endif
  }

  explicit 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, typename = std::enable_if_t<!std::is_void<TResult>::value>>
  TResult & operator*() const
  {
    return *m_ptr;
  }

  ref_ptr & operator=(ref_ptr const & rhs) noexcept
  {
    if (this == &rhs)
      return *this;

#if defined(TRACK_POINTERS)
    if (m_isOwnerUnique)
      DpPointerTracker::Instance().DerefPtr(m_ptr);
#endif

    m_ptr = rhs.m_ptr;

#if defined(TRACK_POINTERS)
    m_isOwnerUnique = rhs.m_isOwnerUnique;
    if (m_isOwnerUnique)
      DpPointerTracker::Instance().RefPtr(m_ptr);
#endif

    return *this;
  }

  ref_ptr & operator=(ref_ptr && rhs) noexcept
  {
    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;

#if defined(TRACK_POINTERS)
    m_isOwnerUnique = rhs.m_isOwnerUnique;
    rhs.m_isOwnerUnique = false;
#endif

    return *this;
  }

  T * get() const { return m_ptr; }

private:
  T* m_ptr;
#if defined(TRACK_POINTERS)
  bool m_isOwnerUnique;
#endif

  template <typename TResult>
  friend inline std::string DebugPrint(ref_ptr<TResult> const & v);
};

template <typename T>
inline std::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)
{
#if defined(TRACK_POINTERS)
  return ref_ptr<T>(drapePtr.get(), true /* isOwnerUnique */);
#else
  return ref_ptr<T>(drapePtr.get());
#endif
}

template <typename T>
ref_ptr<T> make_ref(T* ptr)
{
#if defined(TRACK_POINTERS)
  return ref_ptr<T>(ptr, false /* isOwnerUnique */);
#else
  return ref_ptr<T>(ptr);
#endif
}