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 #ifndef ADBLOCK_PLUS_INTRUSIVE_PTR_H | 9 #pragma once |
10 #define ADBLOCK_PLUS_INTRUSIVE_PTR_H | 10 |
11 | 11 #include <algorithm> |
12 #include <type_traits> | 12 #include <type_traits> |
| 13 |
| 14 #include "debug.h" |
13 | 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 { |
| 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) |
31 { | 34 { |
| 35 } |
| 36 |
| 37 virtual ~ref_counted() |
| 38 { |
| 39 assert(mRefCount == 0, u"Destroying a ref-counted object with a non-zero ref
erence count"_str); |
32 } | 40 } |
33 | 41 |
34 private: | 42 private: |
35 int mRefCount; | 43 int mRefCount; |
36 }; | 44 }; |
37 | 45 |
38 template<class T, | 46 template<typename T, |
39 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
> |
40 class intrusive_ptr | 48 class intrusive_ptr |
41 { | 49 { |
42 public: | 50 public: |
43 intrusive_ptr() | 51 explicit intrusive_ptr() |
44 : mPointer(nullptr) | 52 : mPointer(nullptr) |
45 { | 53 { |
46 } | 54 } |
47 | 55 |
48 intrusive_ptr(T* pointer) | 56 explicit intrusive_ptr(T* pointer) |
49 : 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) |
50 { | 65 { |
51 if (mPointer) | 66 if (mPointer) |
52 mPointer->AddRef(); | 67 mPointer->AddRef(); |
53 } | 68 } |
54 | 69 |
55 intrusive_ptr(const intrusive_ptr& other) | |
56 : mPointer(other.mPointer) | |
57 { | |
58 if (mPointer) | |
59 mPointer->AddRef(); | |
60 } | |
61 | |
62 intrusive_ptr(intrusive_ptr&& other) | 70 intrusive_ptr(intrusive_ptr&& other) |
63 : mPointer(other.mPointer) | 71 : mPointer(other.mPointer) |
64 { | 72 { |
65 other.mPointer = nullptr; | 73 other.mPointer = nullptr; |
66 } | 74 } |
67 | 75 |
68 ~intrusive_ptr() | 76 ~intrusive_ptr() |
69 { | 77 { |
70 if (mPointer) | 78 if (mPointer) |
71 mPointer->ReleaseRef(); | 79 mPointer->ReleaseRef(); |
72 } | 80 } |
73 | 81 |
74 intrusive_ptr& operator=(intrusive_ptr& other) | 82 intrusive_ptr& operator=(intrusive_ptr& other) |
75 { | 83 { |
76 intrusive_ptr(other).swap(*this); | 84 intrusive_ptr(other).swap(*this); |
77 return *this; | 85 return *this; |
78 } | 86 } |
79 | 87 |
80 intrusive_ptr& operator=(intrusive_ptr&& other) | 88 intrusive_ptr& operator=(intrusive_ptr&& other) |
81 { | 89 { |
| 90 intrusive_ptr(std::move(other)).swap(*this); |
| 91 return *this; |
| 92 } |
| 93 |
| 94 intrusive_ptr& operator=(T* other) |
| 95 { |
82 intrusive_ptr(other).swap(*this); | 96 intrusive_ptr(other).swap(*this); |
83 return *this; | 97 return *this; |
84 } | 98 } |
85 | 99 |
86 intrusive_ptr& operator=(T* other) | 100 void reset() |
| 101 { |
| 102 intrusive_ptr().swap(*this); |
| 103 } |
| 104 |
| 105 void reset(T* other) |
87 { | 106 { |
88 intrusive_ptr(other).swap(*this); | 107 intrusive_ptr(other).swap(*this); |
89 } | 108 } |
90 | 109 |
91 void reset() | 110 const T* get() const |
92 { | 111 { |
93 intrusive_ptr().swap(*this); | 112 return mPointer; |
94 } | 113 } |
95 | 114 |
96 void reset(T* other) | 115 T* get() |
97 { | 116 { |
98 intrusive_ptr(other).swap(*this); | 117 return mPointer; |
99 } | 118 } |
100 | 119 |
101 T* get() const | 120 const T& operator*() const |
102 { | |
103 return mPointer; | |
104 } | |
105 | |
106 T& operator*() const | |
107 { | 121 { |
108 return *mPointer; | 122 return *mPointer; |
109 } | 123 } |
110 | 124 |
111 T* operator->() const | 125 T& operator*() |
112 { | 126 { |
113 return mPointer; | 127 return *mPointer; |
114 } | 128 } |
115 | 129 |
116 operator bool() const | 130 const T* operator->() const |
| 131 { |
| 132 return mPointer; |
| 133 } |
| 134 |
| 135 T* operator->() |
| 136 { |
| 137 return mPointer; |
| 138 } |
| 139 |
| 140 explicit operator bool() const |
117 { | 141 { |
118 return mPointer != nullptr; | 142 return mPointer != nullptr; |
119 } | 143 } |
120 | 144 |
121 operator T*() const | |
122 { | |
123 return mPointer; | |
124 } | |
125 | |
126 bool operator!() const | 145 bool operator!() const |
127 { | 146 { |
128 return mPointer == nullptr; | 147 return mPointer == nullptr; |
129 } | 148 } |
130 | 149 |
| 150 T* release() |
| 151 { |
| 152 T* result = mPointer; |
| 153 mPointer = nullptr; |
| 154 return result; |
| 155 } |
| 156 |
131 void swap(intrusive_ptr& other) | 157 void swap(intrusive_ptr& other) |
132 { | 158 { |
133 T* tmp = mPointer; | 159 std::swap(mPointer, other.mPointer); |
134 mPointer = other.mPointer; | |
135 other.mPointer = tmp; | |
136 } | 160 } |
137 | 161 |
138 private: | 162 private: |
139 T* mPointer; | 163 T* mPointer; |
140 }; | 164 }; |
141 | 165 |
142 template<class T, class U> | 166 template<typename T, typename U> |
143 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) |
144 { | 168 { |
145 return a.get() == b.get(); | 169 return a.get() == b.get(); |
146 } | 170 } |
147 | 171 |
148 template<class T, class U> | 172 template<typename T, typename U> |
149 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) |
150 { | 174 { |
151 return a.get() != b.get(); | 175 return a.get() != b.get(); |
152 } | 176 } |
153 | 177 |
154 template<class T, class U> | 178 template<typename T, typename U> |
155 inline bool operator==(const intrusive_ptr<T>& a, const U* b) | 179 inline bool operator==(const intrusive_ptr<T>& a, const U* b) |
156 { | 180 { |
157 return a.get() == b; | 181 return a.get() == b; |
158 } | 182 } |
159 | 183 |
160 template<class T, class U> | 184 template<typename T, typename U> |
161 inline bool operator!=(const intrusive_ptr<T>& a, const U* b) | 185 inline bool operator!=(const intrusive_ptr<T>& a, const U* b) |
162 { | 186 { |
163 return a.get() != b; | 187 return a.get() != b; |
164 } | 188 } |
165 | 189 |
166 template<class T, class U> | 190 template<typename T, typename U> |
167 inline bool operator==(const T* a, const intrusive_ptr<U>& b) | 191 inline bool operator==(const T* a, const intrusive_ptr<U>& b) |
168 { | 192 { |
169 return a == b.get(); | 193 return a == b.get(); |
170 } | 194 } |
171 | 195 |
172 template<class T, class U> | 196 template<typename T, typename U> |
173 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) | 197 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) |
174 { | 198 { |
175 return a != b.get(); | 199 return a != b.get(); |
176 } | 200 } |
177 | |
178 #endif | |
LEFT | RIGHT |