Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Originally these classes are from Chromium. |
| 12 | // http://src.chromium.org/viewvc/chrome/trunk/src/base/memory/ref_counted.h?view=markup |
| 13 | |
| 14 | // |
| 15 | // A smart pointer class for reference counted objects. Use this class instead |
| 16 | // of calling AddRef and Release manually on a reference counted object to |
| 17 | // avoid common memory leaks caused by forgetting to Release an object |
| 18 | // reference. Sample usage: |
| 19 | // |
| 20 | // class MyFoo : public RefCounted<MyFoo> { |
| 21 | // ... |
| 22 | // }; |
| 23 | // |
| 24 | // void some_function() { |
| 25 | // scoped_refptr<MyFoo> foo = new MyFoo(); |
| 26 | // foo->Method(param); |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 27 | // // `foo` is released when this function returns |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 28 | // } |
| 29 | // |
| 30 | // void some_other_function() { |
| 31 | // scoped_refptr<MyFoo> foo = new MyFoo(); |
| 32 | // ... |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 33 | // foo = nullptr; // explicitly releases `foo` |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 34 | // ... |
| 35 | // if (foo) |
| 36 | // foo->Method(param); |
| 37 | // } |
| 38 | // |
| 39 | // The above examples show how scoped_refptr<T> acts like a pointer to T. |
| 40 | // Given two scoped_refptr<T> classes, it is also possible to exchange |
| 41 | // references between the two objects, like so: |
| 42 | // |
| 43 | // { |
| 44 | // scoped_refptr<MyFoo> a = new MyFoo(); |
| 45 | // scoped_refptr<MyFoo> b; |
| 46 | // |
| 47 | // b.swap(a); |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 48 | // // now, `b` references the MyFoo object, and `a` references null. |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 49 | // } |
| 50 | // |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 51 | // To make both `a` and `b` in the above example reference the same MyFoo |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 52 | // object, simply use the assignment operator: |
| 53 | // |
| 54 | // { |
| 55 | // scoped_refptr<MyFoo> a = new MyFoo(); |
| 56 | // scoped_refptr<MyFoo> b; |
| 57 | // |
| 58 | // b = a; |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 59 | // // now, `a` and `b` each own a reference to the same MyFoo object. |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 60 | // } |
| 61 | // |
| 62 | |
| 63 | #ifndef API_SCOPED_REFPTR_H_ |
| 64 | #define API_SCOPED_REFPTR_H_ |
| 65 | |
| 66 | #include <memory> |
| 67 | #include <utility> |
| 68 | |
| 69 | namespace rtc { |
| 70 | |
| 71 | template <class T> |
| 72 | class scoped_refptr { |
| 73 | public: |
Steve Anton | 9405efa | 2018-12-19 15:05:41 -0800 | [diff] [blame] | 74 | typedef T element_type; |
| 75 | |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 76 | scoped_refptr() : ptr_(nullptr) {} |
Niels Möller | 1a58a3f | 2022-01-25 15:54:17 +0100 | [diff] [blame] | 77 | scoped_refptr(std::nullptr_t) : ptr_(nullptr) {} // NOLINT(runtime/explicit) |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 78 | |
Niels Möller | 1a58a3f | 2022-01-25 15:54:17 +0100 | [diff] [blame] | 79 | explicit scoped_refptr(T* p) : ptr_(p) { |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 80 | if (ptr_) |
| 81 | ptr_->AddRef(); |
| 82 | } |
| 83 | |
| 84 | scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
| 85 | if (ptr_) |
| 86 | ptr_->AddRef(); |
| 87 | } |
| 88 | |
| 89 | template <typename U> |
| 90 | scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { |
| 91 | if (ptr_) |
| 92 | ptr_->AddRef(); |
| 93 | } |
| 94 | |
| 95 | // Move constructors. |
Danil Chapovalov | ba916b7 | 2019-11-12 10:24:43 +0100 | [diff] [blame] | 96 | scoped_refptr(scoped_refptr<T>&& r) noexcept : ptr_(r.release()) {} |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 97 | |
| 98 | template <typename U> |
Danil Chapovalov | ba916b7 | 2019-11-12 10:24:43 +0100 | [diff] [blame] | 99 | scoped_refptr(scoped_refptr<U>&& r) noexcept : ptr_(r.release()) {} |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 100 | |
| 101 | ~scoped_refptr() { |
| 102 | if (ptr_) |
| 103 | ptr_->Release(); |
| 104 | } |
| 105 | |
| 106 | T* get() const { return ptr_; } |
| 107 | operator T*() const { return ptr_; } |
Niels Möller | 662b306 | 2021-03-12 14:08:23 +0100 | [diff] [blame] | 108 | T& operator*() const { return *ptr_; } |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 109 | T* operator->() const { return ptr_; } |
| 110 | |
| 111 | // Returns the (possibly null) raw pointer, and makes the scoped_refptr hold a |
| 112 | // null pointer, all without touching the reference count of the underlying |
| 113 | // pointed-to object. The object is still reference counted, and the caller of |
| 114 | // release() is now the proud owner of one reference, so it is responsible for |
| 115 | // calling Release() once on the object when no longer using it. |
| 116 | T* release() { |
| 117 | T* retVal = ptr_; |
| 118 | ptr_ = nullptr; |
| 119 | return retVal; |
| 120 | } |
| 121 | |
| 122 | scoped_refptr<T>& operator=(T* p) { |
| 123 | // AddRef first so that self assignment should work |
| 124 | if (p) |
| 125 | p->AddRef(); |
| 126 | if (ptr_) |
| 127 | ptr_->Release(); |
| 128 | ptr_ = p; |
| 129 | return *this; |
| 130 | } |
| 131 | |
| 132 | scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { |
| 133 | return *this = r.ptr_; |
| 134 | } |
| 135 | |
| 136 | template <typename U> |
| 137 | scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { |
| 138 | return *this = r.get(); |
| 139 | } |
| 140 | |
Danil Chapovalov | ba916b7 | 2019-11-12 10:24:43 +0100 | [diff] [blame] | 141 | scoped_refptr<T>& operator=(scoped_refptr<T>&& r) noexcept { |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 142 | scoped_refptr<T>(std::move(r)).swap(*this); |
| 143 | return *this; |
| 144 | } |
| 145 | |
| 146 | template <typename U> |
Danil Chapovalov | ba916b7 | 2019-11-12 10:24:43 +0100 | [diff] [blame] | 147 | scoped_refptr<T>& operator=(scoped_refptr<U>&& r) noexcept { |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 148 | scoped_refptr<T>(std::move(r)).swap(*this); |
| 149 | return *this; |
| 150 | } |
| 151 | |
Danil Chapovalov | ba916b7 | 2019-11-12 10:24:43 +0100 | [diff] [blame] | 152 | void swap(T** pp) noexcept { |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 153 | T* p = ptr_; |
| 154 | ptr_ = *pp; |
| 155 | *pp = p; |
| 156 | } |
| 157 | |
Danil Chapovalov | ba916b7 | 2019-11-12 10:24:43 +0100 | [diff] [blame] | 158 | void swap(scoped_refptr<T>& r) noexcept { swap(&r.ptr_); } |
Mirko Bonadei | 85340ce | 2018-11-19 15:51:39 +0100 | [diff] [blame] | 159 | |
| 160 | protected: |
| 161 | T* ptr_; |
| 162 | }; |
| 163 | |
| 164 | } // namespace rtc |
| 165 | |
| 166 | #endif // API_SCOPED_REFPTR_H_ |