blob: b1c90fcbbe8d40785b6688e7a68f9fca73b875a5 [file] [log] [blame]
Mirko Bonadei85340ce2018-11-19 15:51:39 +01001/*
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 Titov0e61fdd2021-07-25 21:50:14 +020027// // `foo` is released when this function returns
Mirko Bonadei85340ce2018-11-19 15:51:39 +010028// }
29//
30// void some_other_function() {
31// scoped_refptr<MyFoo> foo = new MyFoo();
32// ...
Artem Titov0e61fdd2021-07-25 21:50:14 +020033// foo = nullptr; // explicitly releases `foo`
Mirko Bonadei85340ce2018-11-19 15:51:39 +010034// ...
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 Titov0e61fdd2021-07-25 21:50:14 +020048// // now, `b` references the MyFoo object, and `a` references null.
Mirko Bonadei85340ce2018-11-19 15:51:39 +010049// }
50//
Artem Titov0e61fdd2021-07-25 21:50:14 +020051// To make both `a` and `b` in the above example reference the same MyFoo
Mirko Bonadei85340ce2018-11-19 15:51:39 +010052// 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 Titov0e61fdd2021-07-25 21:50:14 +020059// // now, `a` and `b` each own a reference to the same MyFoo object.
Mirko Bonadei85340ce2018-11-19 15:51:39 +010060// }
61//
62
63#ifndef API_SCOPED_REFPTR_H_
64#define API_SCOPED_REFPTR_H_
65
66#include <memory>
67#include <utility>
68
69namespace rtc {
70
71template <class T>
72class scoped_refptr {
73 public:
Steve Anton9405efa2018-12-19 15:05:41 -080074 typedef T element_type;
75
Mirko Bonadei85340ce2018-11-19 15:51:39 +010076 scoped_refptr() : ptr_(nullptr) {}
Niels Möller1a58a3f2022-01-25 15:54:17 +010077 scoped_refptr(std::nullptr_t) : ptr_(nullptr) {} // NOLINT(runtime/explicit)
Mirko Bonadei85340ce2018-11-19 15:51:39 +010078
Niels Möller1a58a3f2022-01-25 15:54:17 +010079 explicit scoped_refptr(T* p) : ptr_(p) {
Mirko Bonadei85340ce2018-11-19 15:51:39 +010080 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 Chapovalovba916b72019-11-12 10:24:43 +010096 scoped_refptr(scoped_refptr<T>&& r) noexcept : ptr_(r.release()) {}
Mirko Bonadei85340ce2018-11-19 15:51:39 +010097
98 template <typename U>
Danil Chapovalovba916b72019-11-12 10:24:43 +010099 scoped_refptr(scoped_refptr<U>&& r) noexcept : ptr_(r.release()) {}
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100100
101 ~scoped_refptr() {
102 if (ptr_)
103 ptr_->Release();
104 }
105
106 T* get() const { return ptr_; }
Niels Möllerafb246b2022-04-20 14:26:50 +0200107 explicit operator bool() const { return ptr_ != nullptr; }
Niels Möller94327682022-04-28 13:20:29 +0200108 // TODO(bugs.webrtc.org/13464): Delete this conversion operator.
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100109 operator T*() const { return ptr_; }
Niels Möller662b3062021-03-12 14:08:23 +0100110 T& operator*() const { return *ptr_; }
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100111 T* operator->() const { return ptr_; }
112
113 // Returns the (possibly null) raw pointer, and makes the scoped_refptr hold a
114 // null pointer, all without touching the reference count of the underlying
115 // pointed-to object. The object is still reference counted, and the caller of
116 // release() is now the proud owner of one reference, so it is responsible for
117 // calling Release() once on the object when no longer using it.
118 T* release() {
119 T* retVal = ptr_;
120 ptr_ = nullptr;
121 return retVal;
122 }
123
124 scoped_refptr<T>& operator=(T* p) {
125 // AddRef first so that self assignment should work
126 if (p)
127 p->AddRef();
128 if (ptr_)
129 ptr_->Release();
130 ptr_ = p;
131 return *this;
132 }
133
134 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
135 return *this = r.ptr_;
136 }
137
138 template <typename U>
139 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
140 return *this = r.get();
141 }
142
Danil Chapovalovba916b72019-11-12 10:24:43 +0100143 scoped_refptr<T>& operator=(scoped_refptr<T>&& r) noexcept {
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100144 scoped_refptr<T>(std::move(r)).swap(*this);
145 return *this;
146 }
147
148 template <typename U>
Danil Chapovalovba916b72019-11-12 10:24:43 +0100149 scoped_refptr<T>& operator=(scoped_refptr<U>&& r) noexcept {
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100150 scoped_refptr<T>(std::move(r)).swap(*this);
151 return *this;
152 }
153
Danil Chapovalovba916b72019-11-12 10:24:43 +0100154 void swap(T** pp) noexcept {
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100155 T* p = ptr_;
156 ptr_ = *pp;
157 *pp = p;
158 }
159
Danil Chapovalovba916b72019-11-12 10:24:43 +0100160 void swap(scoped_refptr<T>& r) noexcept { swap(&r.ptr_); }
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100161
162 protected:
163 T* ptr_;
164};
165
Niels Möller94327682022-04-28 13:20:29 +0200166template <typename T, typename U>
167bool operator==(const rtc::scoped_refptr<T>& a,
168 const rtc::scoped_refptr<U>& b) {
169 return a.get() == b.get();
170}
171template <typename T, typename U>
172bool operator!=(const rtc::scoped_refptr<T>& a,
173 const rtc::scoped_refptr<U>& b) {
174 return !(a == b);
175}
176
177template <typename T>
178bool operator==(const rtc::scoped_refptr<T>& a, std::nullptr_t) {
179 return a.get() == nullptr;
180}
181
182template <typename T>
183bool operator!=(const rtc::scoped_refptr<T>& a, std::nullptr_t) {
184 return !(a == nullptr);
185}
186
187template <typename T>
188bool operator==(std::nullptr_t, const rtc::scoped_refptr<T>& a) {
189 return a.get() == nullptr;
190}
191
192template <typename T>
193bool operator!=(std::nullptr_t, const rtc::scoped_refptr<T>& a) {
194 return !(a == nullptr);
195}
196
197// Ordered comparison, needed for use as a std::map key.
198template <typename T, typename U>
199bool operator<(const rtc::scoped_refptr<T>& a, const rtc::scoped_refptr<U>& b) {
200 return a.get() < b.get();
201}
202
Mirko Bonadei85340ce2018-11-19 15:51:39 +0100203} // namespace rtc
204
205#endif // API_SCOPED_REFPTR_H_