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 | |
11 | 10 |
12 #include <algorithm> | 11 #include <algorithm> |
13 #include <type_traits> | 12 #include <type_traits> |
14 | 13 |
| 14 #include "debug.h" |
| 15 |
15 class ref_counted | 16 class ref_counted |
16 { | 17 { |
17 public: | 18 public: |
18 void AddRef() | 19 void AddRef() |
19 { | 20 { |
20 mRefCount++; | 21 mRefCount++; |
21 } | 22 } |
22 | 23 |
23 void ReleaseRef() | 24 void ReleaseRef() |
24 { | 25 { |
| 26 assert(mRefCount > 0, u"Unexpected zero or negative reference count"_str); |
25 if (--mRefCount == 0) | 27 if (--mRefCount == 0) |
26 delete this; | 28 delete this; |
27 } | 29 } |
28 | 30 |
29 protected: | 31 protected: |
30 ref_counted() | 32 ref_counted() |
31 : mRefCount(0) | 33 : mRefCount(1) |
32 { | 34 { |
33 } | 35 } |
34 | 36 |
35 // We need this virtual destructor, otherwise pointers to ref_counted and | |
36 // pointers to derived classes won't have the same value (e.g. converting from | |
37 // Filter* to ref_counted* decreases pointer value by 4). | |
38 virtual ~ref_counted() | 37 virtual ~ref_counted() |
39 { | 38 { |
| 39 assert(mRefCount == 0, u"Destroying a ref-counted object with a non-zero ref
erence count"_str); |
40 } | 40 } |
41 | 41 |
42 private: | 42 private: |
43 int mRefCount; | 43 int mRefCount; |
44 }; | 44 }; |
45 | 45 |
46 template<typename T, | 46 template<typename T, |
47 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
> |
48 class intrusive_ptr | 48 class intrusive_ptr |
49 { | 49 { |
50 public: | 50 public: |
51 intrusive_ptr() | 51 explicit intrusive_ptr() |
52 : mPointer(nullptr) | 52 : mPointer(nullptr) |
53 { | 53 { |
54 } | 54 } |
55 | 55 |
56 intrusive_ptr(T* pointer) | 56 explicit intrusive_ptr(T* pointer) |
57 : 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) |
58 { | 65 { |
59 if (mPointer) | 66 if (mPointer) |
60 mPointer->AddRef(); | 67 mPointer->AddRef(); |
61 } | 68 } |
62 | 69 |
63 intrusive_ptr(const intrusive_ptr& other) | |
64 : mPointer(other.mPointer) | |
65 { | |
66 if (mPointer) | |
67 mPointer->AddRef(); | |
68 } | |
69 | |
70 intrusive_ptr(intrusive_ptr&& other) | 70 intrusive_ptr(intrusive_ptr&& other) |
71 : mPointer(other.mPointer) | 71 : mPointer(other.mPointer) |
72 { | 72 { |
73 other.mPointer = nullptr; | 73 other.mPointer = nullptr; |
74 } | 74 } |
75 | 75 |
76 ~intrusive_ptr() | 76 ~intrusive_ptr() |
77 { | 77 { |
78 if (mPointer) | 78 if (mPointer) |
79 mPointer->ReleaseRef(); | 79 mPointer->ReleaseRef(); |
80 } | 80 } |
81 | 81 |
82 intrusive_ptr& operator=(intrusive_ptr& other) | 82 intrusive_ptr& operator=(intrusive_ptr& other) |
83 { | 83 { |
84 intrusive_ptr(other).swap(*this); | 84 intrusive_ptr(other).swap(*this); |
85 return *this; | 85 return *this; |
86 } | 86 } |
87 | 87 |
88 intrusive_ptr& operator=(intrusive_ptr&& other) | 88 intrusive_ptr& operator=(intrusive_ptr&& other) |
89 { | 89 { |
| 90 intrusive_ptr(std::move(other)).swap(*this); |
| 91 return *this; |
| 92 } |
| 93 |
| 94 intrusive_ptr& operator=(T* other) |
| 95 { |
90 intrusive_ptr(other).swap(*this); | 96 intrusive_ptr(other).swap(*this); |
91 return *this; | 97 return *this; |
92 } | 98 } |
93 | 99 |
94 intrusive_ptr& operator=(T* other) | 100 void reset() |
| 101 { |
| 102 intrusive_ptr().swap(*this); |
| 103 } |
| 104 |
| 105 void reset(T* other) |
95 { | 106 { |
96 intrusive_ptr(other).swap(*this); | 107 intrusive_ptr(other).swap(*this); |
97 return *this; | 108 } |
98 } | 109 |
99 | 110 const T* get() const |
100 void reset() | 111 { |
101 { | 112 return mPointer; |
102 intrusive_ptr().swap(*this); | 113 } |
103 } | 114 |
104 | 115 T* get() |
105 void reset(T* other) | 116 { |
106 { | 117 return mPointer; |
107 intrusive_ptr(other).swap(*this); | 118 } |
108 } | 119 |
109 | 120 const T& operator*() const |
110 T* get() const | |
111 { | |
112 return mPointer; | |
113 } | |
114 | |
115 T& operator*() const | |
116 { | 121 { |
117 return *mPointer; | 122 return *mPointer; |
118 } | 123 } |
119 | 124 |
120 T* operator->() const | 125 T& operator*() |
121 { | 126 { |
122 return mPointer; | 127 return *mPointer; |
123 } | 128 } |
124 | 129 |
125 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 |
126 { | 141 { |
127 return mPointer != nullptr; | 142 return mPointer != nullptr; |
128 } | 143 } |
129 | 144 |
130 operator T*() const | |
131 { | |
132 return mPointer; | |
133 } | |
134 | |
135 bool operator!() const | 145 bool operator!() const |
136 { | 146 { |
137 return mPointer == nullptr; | 147 return mPointer == nullptr; |
| 148 } |
| 149 |
| 150 T* release() |
| 151 { |
| 152 T* result = mPointer; |
| 153 mPointer = nullptr; |
| 154 return result; |
138 } | 155 } |
139 | 156 |
140 void swap(intrusive_ptr& other) | 157 void swap(intrusive_ptr& other) |
141 { | 158 { |
142 std::swap(mPointer, other.mPointer); | 159 std::swap(mPointer, other.mPointer); |
143 } | 160 } |
144 | 161 |
145 private: | 162 private: |
146 T* mPointer; | 163 T* mPointer; |
147 }; | 164 }; |
(...skipping 26 matching lines...) Expand all Loading... |
174 inline bool operator==(const T* a, const intrusive_ptr<U>& b) | 191 inline bool operator==(const T* a, const intrusive_ptr<U>& b) |
175 { | 192 { |
176 return a == b.get(); | 193 return a == b.get(); |
177 } | 194 } |
178 | 195 |
179 template<typename T, typename U> | 196 template<typename T, typename U> |
180 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) | 197 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) |
181 { | 198 { |
182 return a != b.get(); | 199 return a != b.get(); |
183 } | 200 } |
184 | |
185 #endif | |
LEFT | RIGHT |