| 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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 if (--mRefCount == 0) | 27 if (--mRefCount == 0) |
| 28 delete this; | 28 delete this; |
| 29 } | 29 } |
| 30 | 30 |
| 31 protected: | 31 protected: |
| 32 ref_counted() | 32 ref_counted() |
| 33 : mRefCount(1) | 33 : mRefCount(1) |
| 34 { | 34 { |
| 35 } | 35 } |
| 36 | 36 |
| 37 // We need this virtual destructor, otherwise the numerical value of | |
| 38 // a ref_counted pointer and the numerical value of a pointer to a derived | |
| 39 // class will no longer be identical - despite still pointing to the same | |
| 40 // object. So for example `(int)(Filter*)ptr - (int)(ref_counted*)ptr` will be | |
| 41 // four, something that presents an issue for our JavaScript bindings which | |
| 42 // don't know how to convert pointers between different types. | |
| 43 virtual ~ref_counted() | 37 virtual ~ref_counted() |
| 44 { | 38 { |
| 45 assert(mRefCount == 0, u"Destroying a ref-counted object with a non-zero ref
erence count"_str); | 39 assert(mRefCount == 0, u"Destroying a ref-counted object with a non-zero ref
erence count"_str); |
| 46 } | 40 } |
| 47 | 41 |
| 48 private: | 42 private: |
| 49 int mRefCount; | 43 int mRefCount; |
| 50 }; | 44 }; |
| 51 | 45 |
| 52 template<typename T, | 46 template<typename T, |
| 53 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
> |
| 54 class intrusive_ptr | 48 class intrusive_ptr |
| 55 { | 49 { |
| 56 public: | 50 public: |
| 57 intrusive_ptr() | 51 explicit intrusive_ptr() |
| 58 : mPointer(nullptr) | 52 : mPointer(nullptr) |
| 59 { | 53 { |
| 60 } | 54 } |
| 61 | 55 |
| 62 explicit intrusive_ptr(T* pointer) | 56 explicit intrusive_ptr(T* pointer) |
| 63 : mPointer(pointer) | 57 : mPointer(pointer) |
| 64 { | 58 { |
| 65 // Raw pointers always had their reference count increased by whatever gave | 59 // Raw pointers always had their reference count increased by whatever gave |
| 66 // us the pointer so we don't need to do it here. | 60 // us the pointer so we don't need to do it here. |
| 67 } | 61 } |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 inline bool operator==(const T* a, const intrusive_ptr<U>& b) | 191 inline bool operator==(const T* a, const intrusive_ptr<U>& b) |
| 198 { | 192 { |
| 199 return a == b.get(); | 193 return a == b.get(); |
| 200 } | 194 } |
| 201 | 195 |
| 202 template<typename T, typename U> | 196 template<typename T, typename U> |
| 203 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) | 197 inline bool operator!=(const T* a, intrusive_ptr<U> const& b) |
| 204 { | 198 { |
| 205 return a != b.get(); | 199 return a != b.get(); |
| 206 } | 200 } |
| LEFT | RIGHT |