1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_MEMORY_SCOPED_REFPTR_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_MEMORY_SCOPED_REFPTR_H_
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker
10*635a8641SAndroid Build Coastguard Worker #include <iosfwd>
11*635a8641SAndroid Build Coastguard Worker #include <type_traits>
12*635a8641SAndroid Build Coastguard Worker #include <utility>
13*635a8641SAndroid Build Coastguard Worker
14*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
17*635a8641SAndroid Build Coastguard Worker
18*635a8641SAndroid Build Coastguard Worker template <class T>
19*635a8641SAndroid Build Coastguard Worker class scoped_refptr;
20*635a8641SAndroid Build Coastguard Worker
21*635a8641SAndroid Build Coastguard Worker namespace base {
22*635a8641SAndroid Build Coastguard Worker
23*635a8641SAndroid Build Coastguard Worker template <class, typename>
24*635a8641SAndroid Build Coastguard Worker class RefCounted;
25*635a8641SAndroid Build Coastguard Worker template <class, typename>
26*635a8641SAndroid Build Coastguard Worker class RefCountedThreadSafe;
27*635a8641SAndroid Build Coastguard Worker
28*635a8641SAndroid Build Coastguard Worker template <typename T>
29*635a8641SAndroid Build Coastguard Worker scoped_refptr<T> AdoptRef(T* t);
30*635a8641SAndroid Build Coastguard Worker
31*635a8641SAndroid Build Coastguard Worker namespace subtle {
32*635a8641SAndroid Build Coastguard Worker
33*635a8641SAndroid Build Coastguard Worker enum AdoptRefTag { kAdoptRefTag };
34*635a8641SAndroid Build Coastguard Worker enum StartRefCountFromZeroTag { kStartRefCountFromZeroTag };
35*635a8641SAndroid Build Coastguard Worker enum StartRefCountFromOneTag { kStartRefCountFromOneTag };
36*635a8641SAndroid Build Coastguard Worker
37*635a8641SAndroid Build Coastguard Worker template <typename T, typename U, typename V>
IsRefCountPreferenceOverridden(const T *,const RefCounted<U,V> *)38*635a8641SAndroid Build Coastguard Worker constexpr bool IsRefCountPreferenceOverridden(const T*,
39*635a8641SAndroid Build Coastguard Worker const RefCounted<U, V>*) {
40*635a8641SAndroid Build Coastguard Worker return !std::is_same<std::decay_t<decltype(T::kRefCountPreference)>,
41*635a8641SAndroid Build Coastguard Worker std::decay_t<decltype(U::kRefCountPreference)>>::value;
42*635a8641SAndroid Build Coastguard Worker }
43*635a8641SAndroid Build Coastguard Worker
44*635a8641SAndroid Build Coastguard Worker template <typename T, typename U, typename V>
IsRefCountPreferenceOverridden(const T *,const RefCountedThreadSafe<U,V> *)45*635a8641SAndroid Build Coastguard Worker constexpr bool IsRefCountPreferenceOverridden(
46*635a8641SAndroid Build Coastguard Worker const T*,
47*635a8641SAndroid Build Coastguard Worker const RefCountedThreadSafe<U, V>*) {
48*635a8641SAndroid Build Coastguard Worker return !std::is_same<std::decay_t<decltype(T::kRefCountPreference)>,
49*635a8641SAndroid Build Coastguard Worker std::decay_t<decltype(U::kRefCountPreference)>>::value;
50*635a8641SAndroid Build Coastguard Worker }
51*635a8641SAndroid Build Coastguard Worker
IsRefCountPreferenceOverridden(...)52*635a8641SAndroid Build Coastguard Worker constexpr bool IsRefCountPreferenceOverridden(...) {
53*635a8641SAndroid Build Coastguard Worker return false;
54*635a8641SAndroid Build Coastguard Worker }
55*635a8641SAndroid Build Coastguard Worker
56*635a8641SAndroid Build Coastguard Worker } // namespace subtle
57*635a8641SAndroid Build Coastguard Worker
58*635a8641SAndroid Build Coastguard Worker // Creates a scoped_refptr from a raw pointer without incrementing the reference
59*635a8641SAndroid Build Coastguard Worker // count. Use this only for a newly created object whose reference count starts
60*635a8641SAndroid Build Coastguard Worker // from 1 instead of 0.
61*635a8641SAndroid Build Coastguard Worker template <typename T>
AdoptRef(T * obj)62*635a8641SAndroid Build Coastguard Worker scoped_refptr<T> AdoptRef(T* obj) {
63*635a8641SAndroid Build Coastguard Worker using Tag = std::decay_t<decltype(T::kRefCountPreference)>;
64*635a8641SAndroid Build Coastguard Worker static_assert(std::is_same<subtle::StartRefCountFromOneTag, Tag>::value,
65*635a8641SAndroid Build Coastguard Worker "Use AdoptRef only for the reference count starts from one.");
66*635a8641SAndroid Build Coastguard Worker
67*635a8641SAndroid Build Coastguard Worker DCHECK(obj);
68*635a8641SAndroid Build Coastguard Worker DCHECK(obj->HasOneRef());
69*635a8641SAndroid Build Coastguard Worker obj->Adopted();
70*635a8641SAndroid Build Coastguard Worker return scoped_refptr<T>(obj, subtle::kAdoptRefTag);
71*635a8641SAndroid Build Coastguard Worker }
72*635a8641SAndroid Build Coastguard Worker
73*635a8641SAndroid Build Coastguard Worker namespace subtle {
74*635a8641SAndroid Build Coastguard Worker
75*635a8641SAndroid Build Coastguard Worker template <typename T>
AdoptRefIfNeeded(T * obj,StartRefCountFromZeroTag)76*635a8641SAndroid Build Coastguard Worker scoped_refptr<T> AdoptRefIfNeeded(T* obj, StartRefCountFromZeroTag) {
77*635a8641SAndroid Build Coastguard Worker return scoped_refptr<T>(obj);
78*635a8641SAndroid Build Coastguard Worker }
79*635a8641SAndroid Build Coastguard Worker
80*635a8641SAndroid Build Coastguard Worker template <typename T>
AdoptRefIfNeeded(T * obj,StartRefCountFromOneTag)81*635a8641SAndroid Build Coastguard Worker scoped_refptr<T> AdoptRefIfNeeded(T* obj, StartRefCountFromOneTag) {
82*635a8641SAndroid Build Coastguard Worker return AdoptRef(obj);
83*635a8641SAndroid Build Coastguard Worker }
84*635a8641SAndroid Build Coastguard Worker
85*635a8641SAndroid Build Coastguard Worker } // namespace subtle
86*635a8641SAndroid Build Coastguard Worker
87*635a8641SAndroid Build Coastguard Worker // Constructs an instance of T, which is a ref counted type, and wraps the
88*635a8641SAndroid Build Coastguard Worker // object into a scoped_refptr<T>.
89*635a8641SAndroid Build Coastguard Worker template <typename T, typename... Args>
MakeRefCounted(Args &&...args)90*635a8641SAndroid Build Coastguard Worker scoped_refptr<T> MakeRefCounted(Args&&... args) {
91*635a8641SAndroid Build Coastguard Worker T* obj = new T(std::forward<Args>(args)...);
92*635a8641SAndroid Build Coastguard Worker return subtle::AdoptRefIfNeeded(obj, T::kRefCountPreference);
93*635a8641SAndroid Build Coastguard Worker }
94*635a8641SAndroid Build Coastguard Worker
95*635a8641SAndroid Build Coastguard Worker // Takes an instance of T, which is a ref counted type, and wraps the object
96*635a8641SAndroid Build Coastguard Worker // into a scoped_refptr<T>.
97*635a8641SAndroid Build Coastguard Worker template <typename T>
WrapRefCounted(T * t)98*635a8641SAndroid Build Coastguard Worker scoped_refptr<T> WrapRefCounted(T* t) {
99*635a8641SAndroid Build Coastguard Worker return scoped_refptr<T>(t);
100*635a8641SAndroid Build Coastguard Worker }
101*635a8641SAndroid Build Coastguard Worker
102*635a8641SAndroid Build Coastguard Worker } // namespace base
103*635a8641SAndroid Build Coastguard Worker
104*635a8641SAndroid Build Coastguard Worker //
105*635a8641SAndroid Build Coastguard Worker // A smart pointer class for reference counted objects. Use this class instead
106*635a8641SAndroid Build Coastguard Worker // of calling AddRef and Release manually on a reference counted object to
107*635a8641SAndroid Build Coastguard Worker // avoid common memory leaks caused by forgetting to Release an object
108*635a8641SAndroid Build Coastguard Worker // reference. Sample usage:
109*635a8641SAndroid Build Coastguard Worker //
110*635a8641SAndroid Build Coastguard Worker // class MyFoo : public RefCounted<MyFoo> {
111*635a8641SAndroid Build Coastguard Worker // ...
112*635a8641SAndroid Build Coastguard Worker // private:
113*635a8641SAndroid Build Coastguard Worker // friend class RefCounted<MyFoo>; // Allow destruction by RefCounted<>.
114*635a8641SAndroid Build Coastguard Worker // ~MyFoo(); // Destructor must be private/protected.
115*635a8641SAndroid Build Coastguard Worker // };
116*635a8641SAndroid Build Coastguard Worker //
117*635a8641SAndroid Build Coastguard Worker // void some_function() {
118*635a8641SAndroid Build Coastguard Worker // scoped_refptr<MyFoo> foo = MakeRefCounted<MyFoo>();
119*635a8641SAndroid Build Coastguard Worker // foo->Method(param);
120*635a8641SAndroid Build Coastguard Worker // // |foo| is released when this function returns
121*635a8641SAndroid Build Coastguard Worker // }
122*635a8641SAndroid Build Coastguard Worker //
123*635a8641SAndroid Build Coastguard Worker // void some_other_function() {
124*635a8641SAndroid Build Coastguard Worker // scoped_refptr<MyFoo> foo = MakeRefCounted<MyFoo>();
125*635a8641SAndroid Build Coastguard Worker // ...
126*635a8641SAndroid Build Coastguard Worker // foo.reset(); // explicitly releases |foo|
127*635a8641SAndroid Build Coastguard Worker // ...
128*635a8641SAndroid Build Coastguard Worker // if (foo)
129*635a8641SAndroid Build Coastguard Worker // foo->Method(param);
130*635a8641SAndroid Build Coastguard Worker // }
131*635a8641SAndroid Build Coastguard Worker //
132*635a8641SAndroid Build Coastguard Worker // The above examples show how scoped_refptr<T> acts like a pointer to T.
133*635a8641SAndroid Build Coastguard Worker // Given two scoped_refptr<T> classes, it is also possible to exchange
134*635a8641SAndroid Build Coastguard Worker // references between the two objects, like so:
135*635a8641SAndroid Build Coastguard Worker //
136*635a8641SAndroid Build Coastguard Worker // {
137*635a8641SAndroid Build Coastguard Worker // scoped_refptr<MyFoo> a = MakeRefCounted<MyFoo>();
138*635a8641SAndroid Build Coastguard Worker // scoped_refptr<MyFoo> b;
139*635a8641SAndroid Build Coastguard Worker //
140*635a8641SAndroid Build Coastguard Worker // b.swap(a);
141*635a8641SAndroid Build Coastguard Worker // // now, |b| references the MyFoo object, and |a| references nullptr.
142*635a8641SAndroid Build Coastguard Worker // }
143*635a8641SAndroid Build Coastguard Worker //
144*635a8641SAndroid Build Coastguard Worker // To make both |a| and |b| in the above example reference the same MyFoo
145*635a8641SAndroid Build Coastguard Worker // object, simply use the assignment operator:
146*635a8641SAndroid Build Coastguard Worker //
147*635a8641SAndroid Build Coastguard Worker // {
148*635a8641SAndroid Build Coastguard Worker // scoped_refptr<MyFoo> a = MakeRefCounted<MyFoo>();
149*635a8641SAndroid Build Coastguard Worker // scoped_refptr<MyFoo> b;
150*635a8641SAndroid Build Coastguard Worker //
151*635a8641SAndroid Build Coastguard Worker // b = a;
152*635a8641SAndroid Build Coastguard Worker // // now, |a| and |b| each own a reference to the same MyFoo object.
153*635a8641SAndroid Build Coastguard Worker // }
154*635a8641SAndroid Build Coastguard Worker //
155*635a8641SAndroid Build Coastguard Worker // Also see Chromium's ownership and calling conventions:
156*635a8641SAndroid Build Coastguard Worker // https://chromium.googlesource.com/chromium/src/+/lkgr/styleguide/c++/c++.md#object-ownership-and-calling-conventions
157*635a8641SAndroid Build Coastguard Worker // Specifically:
158*635a8641SAndroid Build Coastguard Worker // If the function (at least sometimes) takes a ref on a refcounted object,
159*635a8641SAndroid Build Coastguard Worker // declare the param as scoped_refptr<T>. The caller can decide whether it
160*635a8641SAndroid Build Coastguard Worker // wishes to transfer ownership (by calling std::move(t) when passing t) or
161*635a8641SAndroid Build Coastguard Worker // retain its ref (by simply passing t directly).
162*635a8641SAndroid Build Coastguard Worker // In other words, use scoped_refptr like you would a std::unique_ptr except
163*635a8641SAndroid Build Coastguard Worker // in the odd case where it's required to hold on to a ref while handing one
164*635a8641SAndroid Build Coastguard Worker // to another component (if a component merely needs to use t on the stack
165*635a8641SAndroid Build Coastguard Worker // without keeping a ref: pass t as a raw T*).
166*635a8641SAndroid Build Coastguard Worker template <class T>
167*635a8641SAndroid Build Coastguard Worker class scoped_refptr {
168*635a8641SAndroid Build Coastguard Worker public:
169*635a8641SAndroid Build Coastguard Worker typedef T element_type;
170*635a8641SAndroid Build Coastguard Worker
171*635a8641SAndroid Build Coastguard Worker constexpr scoped_refptr() = default;
172*635a8641SAndroid Build Coastguard Worker
173*635a8641SAndroid Build Coastguard Worker // Constructs from raw pointer. constexpr if |p| is null.
scoped_refptr(T * p)174*635a8641SAndroid Build Coastguard Worker constexpr scoped_refptr(T* p) : ptr_(p) {
175*635a8641SAndroid Build Coastguard Worker if (ptr_)
176*635a8641SAndroid Build Coastguard Worker AddRef(ptr_);
177*635a8641SAndroid Build Coastguard Worker }
178*635a8641SAndroid Build Coastguard Worker
179*635a8641SAndroid Build Coastguard Worker // Copy constructor. This is required in addition to the copy conversion
180*635a8641SAndroid Build Coastguard Worker // constructor below.
scoped_refptr(const scoped_refptr & r)181*635a8641SAndroid Build Coastguard Worker scoped_refptr(const scoped_refptr& r) : scoped_refptr(r.ptr_) {}
182*635a8641SAndroid Build Coastguard Worker
183*635a8641SAndroid Build Coastguard Worker // Copy conversion constructor.
184*635a8641SAndroid Build Coastguard Worker template <typename U,
185*635a8641SAndroid Build Coastguard Worker typename = typename std::enable_if<
186*635a8641SAndroid Build Coastguard Worker std::is_convertible<U*, T*>::value>::type>
scoped_refptr(const scoped_refptr<U> & r)187*635a8641SAndroid Build Coastguard Worker scoped_refptr(const scoped_refptr<U>& r) : scoped_refptr(r.ptr_) {}
188*635a8641SAndroid Build Coastguard Worker
189*635a8641SAndroid Build Coastguard Worker // Move constructor. This is required in addition to the move conversion
190*635a8641SAndroid Build Coastguard Worker // constructor below.
scoped_refptr(scoped_refptr && r)191*635a8641SAndroid Build Coastguard Worker scoped_refptr(scoped_refptr&& r) noexcept : ptr_(r.ptr_) { r.ptr_ = nullptr; }
192*635a8641SAndroid Build Coastguard Worker
193*635a8641SAndroid Build Coastguard Worker // Move conversion constructor.
194*635a8641SAndroid Build Coastguard Worker template <typename U,
195*635a8641SAndroid Build Coastguard Worker typename = typename std::enable_if<
196*635a8641SAndroid Build Coastguard Worker std::is_convertible<U*, T*>::value>::type>
scoped_refptr(scoped_refptr<U> && r)197*635a8641SAndroid Build Coastguard Worker scoped_refptr(scoped_refptr<U>&& r) noexcept : ptr_(r.ptr_) {
198*635a8641SAndroid Build Coastguard Worker r.ptr_ = nullptr;
199*635a8641SAndroid Build Coastguard Worker }
200*635a8641SAndroid Build Coastguard Worker
~scoped_refptr()201*635a8641SAndroid Build Coastguard Worker ~scoped_refptr() {
202*635a8641SAndroid Build Coastguard Worker static_assert(!base::subtle::IsRefCountPreferenceOverridden(
203*635a8641SAndroid Build Coastguard Worker static_cast<T*>(nullptr), static_cast<T*>(nullptr)),
204*635a8641SAndroid Build Coastguard Worker "It's unsafe to override the ref count preference."
205*635a8641SAndroid Build Coastguard Worker " Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE"
206*635a8641SAndroid Build Coastguard Worker " from subclasses.");
207*635a8641SAndroid Build Coastguard Worker if (ptr_)
208*635a8641SAndroid Build Coastguard Worker Release(ptr_);
209*635a8641SAndroid Build Coastguard Worker }
210*635a8641SAndroid Build Coastguard Worker
get()211*635a8641SAndroid Build Coastguard Worker T* get() const { return ptr_; }
212*635a8641SAndroid Build Coastguard Worker
213*635a8641SAndroid Build Coastguard Worker T& operator*() const {
214*635a8641SAndroid Build Coastguard Worker DCHECK(ptr_);
215*635a8641SAndroid Build Coastguard Worker return *ptr_;
216*635a8641SAndroid Build Coastguard Worker }
217*635a8641SAndroid Build Coastguard Worker
218*635a8641SAndroid Build Coastguard Worker T* operator->() const {
219*635a8641SAndroid Build Coastguard Worker DCHECK(ptr_);
220*635a8641SAndroid Build Coastguard Worker return ptr_;
221*635a8641SAndroid Build Coastguard Worker }
222*635a8641SAndroid Build Coastguard Worker
223*635a8641SAndroid Build Coastguard Worker scoped_refptr& operator=(T* p) { return *this = scoped_refptr(p); }
224*635a8641SAndroid Build Coastguard Worker
225*635a8641SAndroid Build Coastguard Worker // Unified assignment operator.
226*635a8641SAndroid Build Coastguard Worker scoped_refptr& operator=(scoped_refptr r) noexcept {
227*635a8641SAndroid Build Coastguard Worker swap(r);
228*635a8641SAndroid Build Coastguard Worker return *this;
229*635a8641SAndroid Build Coastguard Worker }
230*635a8641SAndroid Build Coastguard Worker
231*635a8641SAndroid Build Coastguard Worker // Sets managed object to null and releases reference to the previous managed
232*635a8641SAndroid Build Coastguard Worker // object, if it existed.
reset()233*635a8641SAndroid Build Coastguard Worker void reset() { scoped_refptr().swap(*this); }
234*635a8641SAndroid Build Coastguard Worker
swap(scoped_refptr & r)235*635a8641SAndroid Build Coastguard Worker void swap(scoped_refptr& r) noexcept { std::swap(ptr_, r.ptr_); }
236*635a8641SAndroid Build Coastguard Worker
237*635a8641SAndroid Build Coastguard Worker explicit operator bool() const { return ptr_ != nullptr; }
238*635a8641SAndroid Build Coastguard Worker
239*635a8641SAndroid Build Coastguard Worker template <typename U>
240*635a8641SAndroid Build Coastguard Worker bool operator==(const scoped_refptr<U>& rhs) const {
241*635a8641SAndroid Build Coastguard Worker return ptr_ == rhs.get();
242*635a8641SAndroid Build Coastguard Worker }
243*635a8641SAndroid Build Coastguard Worker
244*635a8641SAndroid Build Coastguard Worker template <typename U>
245*635a8641SAndroid Build Coastguard Worker bool operator!=(const scoped_refptr<U>& rhs) const {
246*635a8641SAndroid Build Coastguard Worker return !operator==(rhs);
247*635a8641SAndroid Build Coastguard Worker }
248*635a8641SAndroid Build Coastguard Worker
249*635a8641SAndroid Build Coastguard Worker template <typename U>
250*635a8641SAndroid Build Coastguard Worker bool operator<(const scoped_refptr<U>& rhs) const {
251*635a8641SAndroid Build Coastguard Worker return ptr_ < rhs.get();
252*635a8641SAndroid Build Coastguard Worker }
253*635a8641SAndroid Build Coastguard Worker
254*635a8641SAndroid Build Coastguard Worker protected:
255*635a8641SAndroid Build Coastguard Worker T* ptr_ = nullptr;
256*635a8641SAndroid Build Coastguard Worker
257*635a8641SAndroid Build Coastguard Worker private:
258*635a8641SAndroid Build Coastguard Worker template <typename U>
259*635a8641SAndroid Build Coastguard Worker friend scoped_refptr<U> base::AdoptRef(U*);
260*635a8641SAndroid Build Coastguard Worker
scoped_refptr(T * p,base::subtle::AdoptRefTag)261*635a8641SAndroid Build Coastguard Worker scoped_refptr(T* p, base::subtle::AdoptRefTag) : ptr_(p) {}
262*635a8641SAndroid Build Coastguard Worker
263*635a8641SAndroid Build Coastguard Worker // Friend required for move constructors that set r.ptr_ to null.
264*635a8641SAndroid Build Coastguard Worker template <typename U>
265*635a8641SAndroid Build Coastguard Worker friend class scoped_refptr;
266*635a8641SAndroid Build Coastguard Worker
267*635a8641SAndroid Build Coastguard Worker // Non-inline helpers to allow:
268*635a8641SAndroid Build Coastguard Worker // class Opaque;
269*635a8641SAndroid Build Coastguard Worker // extern template class scoped_refptr<Opaque>;
270*635a8641SAndroid Build Coastguard Worker // Otherwise the compiler will complain that Opaque is an incomplete type.
271*635a8641SAndroid Build Coastguard Worker static void AddRef(T* ptr);
272*635a8641SAndroid Build Coastguard Worker static void Release(T* ptr);
273*635a8641SAndroid Build Coastguard Worker };
274*635a8641SAndroid Build Coastguard Worker
275*635a8641SAndroid Build Coastguard Worker // static
276*635a8641SAndroid Build Coastguard Worker template <typename T>
AddRef(T * ptr)277*635a8641SAndroid Build Coastguard Worker void scoped_refptr<T>::AddRef(T* ptr) {
278*635a8641SAndroid Build Coastguard Worker ptr->AddRef();
279*635a8641SAndroid Build Coastguard Worker }
280*635a8641SAndroid Build Coastguard Worker
281*635a8641SAndroid Build Coastguard Worker // static
282*635a8641SAndroid Build Coastguard Worker template <typename T>
Release(T * ptr)283*635a8641SAndroid Build Coastguard Worker void scoped_refptr<T>::Release(T* ptr) {
284*635a8641SAndroid Build Coastguard Worker ptr->Release();
285*635a8641SAndroid Build Coastguard Worker }
286*635a8641SAndroid Build Coastguard Worker
287*635a8641SAndroid Build Coastguard Worker template <typename T, typename U>
288*635a8641SAndroid Build Coastguard Worker bool operator==(const scoped_refptr<T>& lhs, const U* rhs) {
289*635a8641SAndroid Build Coastguard Worker return lhs.get() == rhs;
290*635a8641SAndroid Build Coastguard Worker }
291*635a8641SAndroid Build Coastguard Worker
292*635a8641SAndroid Build Coastguard Worker template <typename T, typename U>
293*635a8641SAndroid Build Coastguard Worker bool operator==(const T* lhs, const scoped_refptr<U>& rhs) {
294*635a8641SAndroid Build Coastguard Worker return lhs == rhs.get();
295*635a8641SAndroid Build Coastguard Worker }
296*635a8641SAndroid Build Coastguard Worker
297*635a8641SAndroid Build Coastguard Worker template <typename T>
298*635a8641SAndroid Build Coastguard Worker bool operator==(const scoped_refptr<T>& lhs, std::nullptr_t null) {
299*635a8641SAndroid Build Coastguard Worker return !static_cast<bool>(lhs);
300*635a8641SAndroid Build Coastguard Worker }
301*635a8641SAndroid Build Coastguard Worker
302*635a8641SAndroid Build Coastguard Worker template <typename T>
303*635a8641SAndroid Build Coastguard Worker bool operator==(std::nullptr_t null, const scoped_refptr<T>& rhs) {
304*635a8641SAndroid Build Coastguard Worker return !static_cast<bool>(rhs);
305*635a8641SAndroid Build Coastguard Worker }
306*635a8641SAndroid Build Coastguard Worker
307*635a8641SAndroid Build Coastguard Worker template <typename T, typename U>
308*635a8641SAndroid Build Coastguard Worker bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) {
309*635a8641SAndroid Build Coastguard Worker return !operator==(lhs, rhs);
310*635a8641SAndroid Build Coastguard Worker }
311*635a8641SAndroid Build Coastguard Worker
312*635a8641SAndroid Build Coastguard Worker template <typename T, typename U>
313*635a8641SAndroid Build Coastguard Worker bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) {
314*635a8641SAndroid Build Coastguard Worker return !operator==(lhs, rhs);
315*635a8641SAndroid Build Coastguard Worker }
316*635a8641SAndroid Build Coastguard Worker
317*635a8641SAndroid Build Coastguard Worker template <typename T>
318*635a8641SAndroid Build Coastguard Worker bool operator!=(const scoped_refptr<T>& lhs, std::nullptr_t null) {
319*635a8641SAndroid Build Coastguard Worker return !operator==(lhs, null);
320*635a8641SAndroid Build Coastguard Worker }
321*635a8641SAndroid Build Coastguard Worker
322*635a8641SAndroid Build Coastguard Worker template <typename T>
323*635a8641SAndroid Build Coastguard Worker bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) {
324*635a8641SAndroid Build Coastguard Worker return !operator==(null, rhs);
325*635a8641SAndroid Build Coastguard Worker }
326*635a8641SAndroid Build Coastguard Worker
327*635a8641SAndroid Build Coastguard Worker template <typename T>
328*635a8641SAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) {
329*635a8641SAndroid Build Coastguard Worker return out << p.get();
330*635a8641SAndroid Build Coastguard Worker }
331*635a8641SAndroid Build Coastguard Worker
332*635a8641SAndroid Build Coastguard Worker template <typename T>
swap(scoped_refptr<T> & lhs,scoped_refptr<T> & rhs)333*635a8641SAndroid Build Coastguard Worker void swap(scoped_refptr<T>& lhs, scoped_refptr<T>& rhs) noexcept {
334*635a8641SAndroid Build Coastguard Worker lhs.swap(rhs);
335*635a8641SAndroid Build Coastguard Worker }
336*635a8641SAndroid Build Coastguard Worker
337*635a8641SAndroid Build Coastguard Worker #endif // BASE_MEMORY_SCOPED_REFPTR_H_
338