File: shared_ptr.h

package info (click to toggle)
aseprite 1.0.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,504 kB
  • ctags: 18,296
  • sloc: cpp: 84,144; ansic: 49,119; xml: 1,971; objc: 1,211; asm: 117; makefile: 45
file content (256 lines) | stat: -rw-r--r-- 5,226 bytes parent folder | download
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
251
252
253
254
255
256
// Aseprite Base Library
// Copyright (c) 2001-2013 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#ifndef BASE_SHARED_PTR_H_INCLUDED
#define BASE_SHARED_PTR_H_INCLUDED
#pragma once

// This class counts references for a SharedPtr.
class SharedPtrRefCounterBase
{
public:
  SharedPtrRefCounterBase() : m_count(0) { }
  virtual ~SharedPtrRefCounterBase() { }

  void add_ref()
  {
    ++m_count;
  }

  void release()
  {
    --m_count;
    if (m_count == 0)
      delete this;
  }

  long use_count() const
  {
    return m_count;
  }

private:
  long m_count;         // Number of references.
};

// Default deleter used by shared pointer (it calls "delete"
// operator).
template<class T>
class DefaultSharedPtrDeleter
{
public:
  void operator()(T* ptr)
  {
    delete ptr;
  }
};

// A reference counter with a custom deleter.
template<class T, class Deleter>
class SharedPtrRefCounterImpl : public SharedPtrRefCounterBase
{
public:
  SharedPtrRefCounterImpl(T* ptr, Deleter deleter)
    : m_ptr(ptr)
    , m_deleter(deleter)
  {
  }

  ~SharedPtrRefCounterImpl()
  {
    if (m_ptr)
      m_deleter(m_ptr);
  }

private:
  T* m_ptr;
  Deleter m_deleter;            // Used to destroy the pointer.
};

// Wraps a pointer and keeps reference counting to automatically
// delete the pointed object when it is no longer used.
template<class T>
class SharedPtr
{
public:
  typedef T element_type;

  SharedPtr()
    : m_ptr(0)
    , m_refCount(0)
  {
  }

  // Constructor with default deleter.
  explicit SharedPtr(T* ptr)
  {
    try {
      m_refCount = new SharedPtrRefCounterImpl<T, DefaultSharedPtrDeleter<T> >(ptr, DefaultSharedPtrDeleter<T>());
    }
    catch (...) {
      if (ptr)
        DefaultSharedPtrDeleter<T>()(ptr);
      throw;
    }
    m_ptr = ptr;
    add_ref();
  }

  // Constructor with customized deleter.
  template<class Deleter>
  SharedPtr(T* ptr, Deleter deleter)
  {
    try {
      m_refCount = new SharedPtrRefCounterImpl<T, Deleter>(ptr, deleter);
    }
    catch (...) {
      if (ptr)
        deleter(ptr);
      throw;
    }
    m_ptr = ptr;
    add_ref();
  }

  // Copy other pointer
  SharedPtr(const SharedPtr<T>& other)
    : m_ptr(other.m_ptr)
    , m_refCount(other.m_refCount)
  {
    add_ref();
  }

  // Copy other pointer (of static_casteable type)
  template<class Y>
  SharedPtr(const SharedPtr<Y>& other)
    : m_ptr(static_cast<T*>(const_cast<Y*>(other.m_ptr)))
    , m_refCount(const_cast<SharedPtrRefCounterBase*>(other.m_refCount))
  {
    add_ref();
  }

  // Releases one reference from the pointee.
  virtual ~SharedPtr()
  {
    release();
  }

  void reset(T* ptr = 0)
  {
    if (m_ptr != ptr) {
      release();
      m_ptr = 0;
      m_refCount = 0;

      if (ptr) {
        try {
          m_refCount = new SharedPtrRefCounterImpl<T, DefaultSharedPtrDeleter<T> >(ptr, DefaultSharedPtrDeleter<T>());
        }
        catch (...) {
          if (ptr)
            DefaultSharedPtrDeleter<T>()(ptr);
          throw;
        }
        m_ptr = ptr;
        add_ref();
      }
    }
  }

  template<class Deleter>
  void reset(T* ptr, Deleter deleter)
  {
    if (m_ptr != ptr) {
      release();
      m_ptr = 0;
      m_refCount = 0;

      if (ptr) {
        try {
          m_refCount = new SharedPtrRefCounterImpl<T, Deleter>(ptr, deleter);
        }
        catch (...) {
          if (ptr)
            deleter(ptr);
          throw;
        }
        m_ptr = ptr;
        add_ref();
      }
    }
  }

  SharedPtr& operator=(const SharedPtr<T>& other)
  {
    if (m_ptr != other.m_ptr) {
      release();
      m_ptr = other.m_ptr;
      m_refCount = other.m_refCount;
      add_ref();
    }
    return *this;
  }

  template<class Y>
  SharedPtr& operator=(const SharedPtr<Y>& other)
  {
    if (m_ptr != static_cast<T*>(other.m_ptr)) {
      release();
      m_ptr = static_cast<T*>(const_cast<Y*>(other.m_ptr));
      m_refCount = const_cast<SharedPtrRefCounterBase*>(other.m_refCount);
      add_ref();
    }
    return *this;
  }

  T* get() const { return m_ptr; }
  T& operator*() const { return *m_ptr; }
  T* operator->() const { return m_ptr; }
  operator T*() const { return m_ptr; }

  long use_count() const { return (m_refCount ? m_refCount->use_count(): 0); }
  bool unique() const { return use_count() == 1; }

private:

  // Adds a reference to the pointee.
  void add_ref()
  {
    if (m_refCount)
      m_refCount->add_ref();
  }

  // Removes the reference to the pointee.
  void release()
  {
    if (m_refCount)
      m_refCount->release();
  }


  T* m_ptr;                            // The pointee object.
  SharedPtrRefCounterBase* m_refCount; // Number of references.

  template<class> friend class SharedPtr;
};

// Compares if two shared-pointers points to the same place (object,
// memory address).
template<class T>
bool operator==(const SharedPtr<T>& ptr1, const SharedPtr<T>& ptr2)
{
  return ptr1.get() == ptr2.get();
}

// Compares if two shared-pointers points to different places
// (objects, memory addresses).
template<class T>
bool operator!=(const SharedPtr<T>& ptr1, const SharedPtr<T>& ptr2)
{
  return ptr1.get() != ptr2.get();
}

#endif