blob: 0f4698a256199beafb5dfae5b48ce422ac604d77 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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);
27// // |foo| is released when this function returns
28// }
29//
30// void some_other_function() {
31// scoped_refptr<MyFoo> foo = new MyFoo();
32// ...
deadbeef37f5ecf2017-02-27 14:06:41 -080033// foo = nullptr; // explicitly releases |foo|
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034// ...
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);
deadbeef37f5ecf2017-02-27 14:06:41 -080048// // now, |b| references the MyFoo object, and |a| references null.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049// }
50//
51// To make both |a| and |b| in the above example reference the same MyFoo
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;
59// // now, |a| and |b| each own a reference to the same MyFoo object.
60// }
61//
62
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#ifndef RTC_BASE_SCOPED_REF_PTR_H_
64#define RTC_BASE_SCOPED_REF_PTR_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068namespace rtc {
69
70template <class T>
71class scoped_refptr {
72 public:
73 scoped_refptr() : ptr_(nullptr) {}
74
75 scoped_refptr(T* p) : ptr_(p) {
76 if (ptr_)
77 ptr_->AddRef();
78 }
79
80 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
81 if (ptr_)
82 ptr_->AddRef();
83 }
84
85 template <typename U>
86 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
87 if (ptr_)
88 ptr_->AddRef();
89 }
90
91 // Move constructors.
92 scoped_refptr(scoped_refptr<T>&& r) : ptr_(r.release()) {}
93
94 template <typename U>
95 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.release()) {}
96
97 ~scoped_refptr() {
98 if (ptr_)
99 ptr_->Release();
100 }
101
102 T* get() const { return ptr_; }
103 operator T*() const { return ptr_; }
104 T* operator->() const { return ptr_; }
105
106 // Release a pointer.
107 // The return value is the current pointer held by this object.
108 // If this object holds a null pointer, the return value is null.
109 // After this operation, this object will hold a null pointer,
110 // and will not own the object any more.
111 T* release() {
112 T* retVal = ptr_;
113 ptr_ = nullptr;
114 return retVal;
115 }
116
117 scoped_refptr<T>& operator=(T* p) {
118 // AddRef first so that self assignment should work
119 if (p)
120 p->AddRef();
121 if (ptr_ )
122 ptr_ ->Release();
123 ptr_ = p;
124 return *this;
125 }
126
127 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
128 return *this = r.ptr_;
129 }
130
131 template <typename U>
132 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
133 return *this = r.get();
134 }
135
136 scoped_refptr<T>& operator=(scoped_refptr<T>&& r) {
137 scoped_refptr<T>(std::move(r)).swap(*this);
138 return *this;
139 }
140
141 template <typename U>
142 scoped_refptr<T>& operator=(scoped_refptr<U>&& r) {
143 scoped_refptr<T>(std::move(r)).swap(*this);
144 return *this;
145 }
146
147 void swap(T** pp) {
148 T* p = ptr_;
149 ptr_ = *pp;
150 *pp = p;
151 }
152
153 void swap(scoped_refptr<T>& r) {
154 swap(&r.ptr_);
155 }
156
157 protected:
158 T* ptr_;
159};
160
161} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200163#endif // RTC_BASE_SCOPED_REF_PTR_H_