Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Parts of this code have been copied from boost/smart_ptr/intrusive_ptr.hpp. | 1 // Parts of this code have been copied from boost/smart_ptr/intrusive_ptr.hpp. |
2 // | 2 // |
3 // Copyright (c) 2001, 2002 Peter Dimov | 3 // Copyright (c) 2001, 2002 Peter Dimov |
4 // | 4 // |
5 // Distributed under the Boost Software License, Version 1.0. (See | 5 // Distributed under the Boost Software License, Version 1.0. (See |
6 // accompanying file LICENSE_1_0.txt or copy at | 6 // accompanying file LICENSE_1_0.txt or copy at |
7 // http://www.boost.org/LICENSE_1_0.txt) | 7 // http://www.boost.org/LICENSE_1_0.txt) |
8 | 8 |
9 #pragma once | 9 #pragma once |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <type_traits> | 12 #include <type_traits> |
13 | 13 |
14 #include "debug.h" | |
15 | |
14 class ref_counted | 16 class ref_counted |
15 { | 17 { |
16 public: | 18 public: |
17 void AddRef() | 19 void AddRef() |
18 { | 20 { |
19 mRefCount++; | 21 mRefCount++; |
20 } | 22 } |
21 | 23 |
22 void ReleaseRef() | 24 void ReleaseRef() |
23 { | 25 { |
sergei
2016/06/16 21:17:29
It would be good to have assert that mRefCount is
Wladimir Palant
2016/12/06 10:48:29
Done.
| |
26 assert(mRefCount > 0, u"Unexpected zero or negative reference count"_str); | |
24 if (--mRefCount == 0) | 27 if (--mRefCount == 0) |
25 delete this; | 28 delete this; |
26 } | 29 } |
27 | 30 |
28 protected: | 31 protected: |
29 ref_counted() | 32 ref_counted() |
30 : mRefCount(0) | 33 : mRefCount(1) |
sergei
2016/06/16 21:17:24
To tell the truth I'm not sure that the initial va
Wladimir Palant
2016/12/06 10:48:24
Yes, I changed that already along with the general
| |
31 { | 34 { |
32 } | 35 } |
33 | 36 |
34 // We need this virtual destructor, otherwise pointers to ref_counted and | |
sergei
2016/06/16 21:17:18
This comment is incorrect, pointers are equal, so
Wladimir Palant
2016/12/06 10:48:28
Pointers are equal if you compare them in C++. How
sergei
2017/01/10 15:57:22
Pointers in c++ are already numeric values and are
Wladimir Palant
2017/03/13 17:41:46
Ok, I removed the comment here and instead added o
Wladimir Palant
2017/03/14 10:23:12
Actually, it is better to remove the footgun inste
| |
35 // pointers to derived classes won't have the same value (e.g. converting from | |
36 // Filter* to ref_counted* decreases pointer value by 4). | |
37 virtual ~ref_counted() | 37 virtual ~ref_counted() |
38 { | 38 { |
sergei
2016/06/16 21:17:20
it would be good to have assert that mRefCount is
Wladimir Palant
2016/12/06 10:48:22
Done.
| |
39 assert(mRefCount == 0, u"Destroying a ref-counted object with a non-zero ref erence count"_str); | |
39 } | 40 } |
40 | 41 |
41 private: | 42 private: |
42 int mRefCount; | 43 int mRefCount; |
43 }; | 44 }; |
44 | 45 |
45 template<typename T, | 46 template<typename T, |
46 class = typename std::enable_if<std::is_base_of<ref_counted,T>::value>::type > | 47 class = typename std::enable_if<std::is_base_of<ref_counted,T>::value>::type > |
47 class intrusive_ptr | 48 class intrusive_ptr |
48 { | 49 { |
49 public: | 50 public: |
50 intrusive_ptr() | 51 explicit intrusive_ptr() |
51 : mPointer(nullptr) | 52 : mPointer(nullptr) |
52 { | 53 { |
53 } | 54 } |
54 | 55 |
55 intrusive_ptr(T* pointer) | 56 explicit intrusive_ptr(T* pointer) |
56 : mPointer(pointer) | 57 : mPointer(pointer) |
58 { | |
59 // Raw pointers always had their reference count increased by whatever gave | |
60 // us the pointer so we don't need to do it here. | |
61 } | |
62 | |
63 intrusive_ptr(const intrusive_ptr& other) | |
64 : mPointer(other.mPointer) | |
57 { | 65 { |
58 if (mPointer) | 66 if (mPointer) |
59 mPointer->AddRef(); | 67 mPointer->AddRef(); |
60 } | 68 } |
61 | 69 |
62 intrusive_ptr(const intrusive_ptr& other) | |
63 : mPointer(other.mPointer) | |
64 { | |
65 if (mPointer) | |
66 mPointer->AddRef(); | |
67 } | |
68 | |
69 intrusive_ptr(intrusive_ptr&& other) | 70 intrusive_ptr(intrusive_ptr&& other) |
70 : mPointer(other.mPointer) | 71 : mPointer(other.mPointer) |
71 { | 72 { |
72 other.mPointer = nullptr; | 73 other.mPointer = nullptr; |
73 } | 74 } |
74 | 75 |
75 ~intrusive_ptr() | 76 ~intrusive_ptr() |
76 { | 77 { |
77 if (mPointer) | 78 if (mPointer) |
78 mPointer->ReleaseRef(); | 79 mPointer->ReleaseRef(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 const T* operator->() const | 130 const T* operator->() const |
130 { | 131 { |
131 return mPointer; | 132 return mPointer; |
132 } | 133 } |
133 | 134 |
134 T* operator->() | 135 T* operator->() |
135 { | 136 { |
136 return mPointer; | 137 return mPointer; |
137 } | 138 } |
138 | 139 |
139 operator bool() const | 140 explicit operator bool() const |
sergei
2016/06/16 21:17:27
It would be also better to make it explicit operat
Wladimir Palant
2016/12/06 10:48:26
Done.
| |
140 { | 141 { |
141 return mPointer != nullptr; | 142 return mPointer != nullptr; |
142 } | 143 } |
143 | 144 |
144 operator T*() const | |
sergei
2016/06/16 21:17:22
I personally don't like this operator because of i
Wladimir Palant
2016/12/06 10:48:16
Done.
| |
145 { | |
146 return mPointer; | |
147 } | |
148 | |
149 bool operator!() const | 145 bool operator!() const |
150 { | 146 { |
151 return mPointer == nullptr; | 147 return mPointer == nullptr; |
148 } | |
149 | |
150 T* release() | |
151 { | |
152 T* result = mPointer; | |
153 mPointer = nullptr; | |
154 return result; | |
152 } | 155 } |
153 | 156 |
154 void swap(intrusive_ptr& other) | 157 void swap(intrusive_ptr& other) |
155 { | 158 { |
156 std::swap(mPointer, other.mPointer); | 159 std::swap(mPointer, other.mPointer); |
157 } | 160 } |
158 | 161 |
159 private: | 162 private: |
160 T* mPointer; | 163 T* mPointer; |
161 }; | 164 }; |
162 | 165 |
163 template<typename T, typename U> | 166 template<typename T, typename U> |
164 inline bool operator==(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b) | 167 inline bool operator==(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b) |
sergei
2016/06/16 21:17:21
JIC, these operators don't always work, e.g. with
Wladimir Palant
2016/12/06 10:48:21
Won't C++ cast these pointers automatically in ord
sergei
2017/01/10 15:57:20
No.
| |
165 { | 168 { |
166 return a.get() == b.get(); | 169 return a.get() == b.get(); |
167 } | 170 } |
168 | 171 |
169 template<typename T, typename U> | 172 template<typename T, typename U> |
170 inline bool operator!=(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b) | 173 inline bool operator!=(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b) |
171 { | 174 { |
172 return a.get() != b.get(); | 175 return a.get() != b.get(); |
173 } | 176 } |
174 | 177 |
(...skipping 13 matching lines...) Expand all Loading... | |
188 inline bool operator==(const T* a, const intrusive_ptr<U>& b) | 191 inline bool operator==(const T* a, const intrusive_ptr<U>& b) |
189 { | 192 { |
190 return a == b.get(); | 193 return a == b.get(); |
191 } | 194 } |
192 | 195 |
193 template<typename T, typename U> | 196 template<typename T, typename U> |
194 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) | 197 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) |
195 { | 198 { |
196 return a != b.get(); | 199 return a != b.get(); |
197 } | 200 } |
LEFT | RIGHT |