| 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 #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) |
|
René Jeschke
2016/02/02 11:11:23
Nit: having a check on `0` here feels wrong and ma
Wladimir Palant
2016/02/02 17:55:19
It's quite the opposite I think - if we are alread
| |
| 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 { |
| 32 } | 35 } |
| 33 | 36 |
| 34 // We need this virtual destructor, otherwise pointers to ref_counted and | |
| 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 { |
| 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<class 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(); |
| 79 } | 80 } |
| 80 | 81 |
| 81 intrusive_ptr& operator=(intrusive_ptr& other) | 82 intrusive_ptr& operator=(intrusive_ptr& other) |
| 82 { | 83 { |
| 83 intrusive_ptr(other).swap(*this); | 84 intrusive_ptr(other).swap(*this); |
| 84 return *this; | 85 return *this; |
| 85 } | 86 } |
| 86 | 87 |
| 87 intrusive_ptr& operator=(intrusive_ptr&& other) | 88 intrusive_ptr& operator=(intrusive_ptr&& other) |
| 88 { | 89 { |
| 90 intrusive_ptr(std::move(other)).swap(*this); | |
| 91 return *this; | |
| 92 } | |
| 93 | |
| 94 intrusive_ptr& operator=(T* other) | |
| 95 { | |
| 89 intrusive_ptr(other).swap(*this); | 96 intrusive_ptr(other).swap(*this); |
| 90 return *this; | 97 return *this; |
| 91 } | 98 } |
| 92 | 99 |
| 93 intrusive_ptr& operator=(T* other) | 100 void reset() |
| 101 { | |
| 102 intrusive_ptr().swap(*this); | |
| 103 } | |
| 104 | |
| 105 void reset(T* other) | |
| 94 { | 106 { |
| 95 intrusive_ptr(other).swap(*this); | 107 intrusive_ptr(other).swap(*this); |
| 96 } | 108 } |
| 97 | 109 |
| 98 void reset() | 110 const T* get() const |
| 99 { | 111 { |
| 100 intrusive_ptr().swap(*this); | 112 return mPointer; |
| 101 } | 113 } |
| 102 | 114 |
| 103 void reset(T* other) | 115 T* get() |
| 104 { | 116 { |
| 105 intrusive_ptr(other).swap(*this); | 117 return mPointer; |
| 106 } | 118 } |
| 107 | 119 |
| 108 T* get() const | 120 const T& operator*() const |
| 109 { | |
| 110 return mPointer; | |
| 111 } | |
| 112 | |
| 113 T& operator*() const | |
| 114 { | 121 { |
| 115 return *mPointer; | 122 return *mPointer; |
| 116 } | 123 } |
| 117 | 124 |
| 118 T* operator->() const | 125 T& operator*() |
| 119 { | 126 { |
| 120 return mPointer; | 127 return *mPointer; |
| 121 } | 128 } |
| 122 | 129 |
| 123 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 | |
| 124 { | 141 { |
| 125 return mPointer != nullptr; | 142 return mPointer != nullptr; |
| 126 } | 143 } |
| 127 | 144 |
| 128 operator T*() const | |
| 129 { | |
| 130 return mPointer; | |
| 131 } | |
| 132 | |
| 133 bool operator!() const | 145 bool operator!() const |
| 134 { | 146 { |
| 135 return mPointer == nullptr; | 147 return mPointer == nullptr; |
| 136 } | 148 } |
| 137 | 149 |
| 150 T* release() | |
| 151 { | |
| 152 T* result = mPointer; | |
| 153 mPointer = nullptr; | |
| 154 return result; | |
| 155 } | |
| 156 | |
| 138 void swap(intrusive_ptr& other) | 157 void swap(intrusive_ptr& other) |
| 139 { | 158 { |
| 140 T* tmp = mPointer; | 159 std::swap(mPointer, other.mPointer); |
|
René Jeschke
2016/02/02 11:11:24
Nit: is there a specific reason why you're not usi
Wladimir Palant
2016/02/02 17:55:19
You have to ask Boost devs :)
I haven't heard abo
| |
| 141 mPointer = other.mPointer; | |
| 142 other.mPointer = tmp; | |
| 143 } | 160 } |
| 144 | 161 |
| 145 private: | 162 private: |
| 146 T* mPointer; | 163 T* mPointer; |
| 147 }; | 164 }; |
| 148 | 165 |
| 149 template<class T, class U> | 166 template<typename T, typename U> |
| 150 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) |
| 151 { | 168 { |
| 152 return a.get() == b.get(); | 169 return a.get() == b.get(); |
| 153 } | 170 } |
| 154 | 171 |
| 155 template<class T, class U> | 172 template<typename T, typename U> |
| 156 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) |
| 157 { | 174 { |
| 158 return a.get() != b.get(); | 175 return a.get() != b.get(); |
| 159 } | 176 } |
| 160 | 177 |
| 161 template<class T, class U> | 178 template<typename T, typename U> |
| 162 inline bool operator==(const intrusive_ptr<T>& a, const U* b) | 179 inline bool operator==(const intrusive_ptr<T>& a, const U* b) |
| 163 { | 180 { |
| 164 return a.get() == b; | 181 return a.get() == b; |
| 165 } | 182 } |
| 166 | 183 |
| 167 template<class T, class U> | 184 template<typename T, typename U> |
| 168 inline bool operator!=(const intrusive_ptr<T>& a, const U* b) | 185 inline bool operator!=(const intrusive_ptr<T>& a, const U* b) |
| 169 { | 186 { |
| 170 return a.get() != b; | 187 return a.get() != b; |
| 171 } | 188 } |
| 172 | 189 |
| 173 template<class T, class U> | 190 template<typename T, typename U> |
| 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<class T, class 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 |