OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 // | 23 // |
24 // void some_function() { | 24 // void some_function() { |
25 // scoped_refptr<MyFoo> foo = new MyFoo(); | 25 // scoped_refptr<MyFoo> foo = new MyFoo(); |
26 // foo->Method(param); | 26 // foo->Method(param); |
27 // // |foo| is released when this function returns | 27 // // |foo| is released when this function returns |
28 // } | 28 // } |
29 // | 29 // |
30 // void some_other_function() { | 30 // void some_other_function() { |
31 // scoped_refptr<MyFoo> foo = new MyFoo(); | 31 // scoped_refptr<MyFoo> foo = new MyFoo(); |
32 // ... | 32 // ... |
33 // foo = NULL; // explicitly releases |foo| | 33 // foo = nullptr; // explicitly releases |foo| |
34 // ... | 34 // ... |
35 // if (foo) | 35 // if (foo) |
36 // foo->Method(param); | 36 // foo->Method(param); |
37 // } | 37 // } |
38 // | 38 // |
39 // The above examples show how scoped_refptr<T> acts like a pointer to T. | 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 | 40 // Given two scoped_refptr<T> classes, it is also possible to exchange |
41 // references between the two objects, like so: | 41 // references between the two objects, like so: |
42 // | 42 // |
43 // { | 43 // { |
44 // scoped_refptr<MyFoo> a = new MyFoo(); | 44 // scoped_refptr<MyFoo> a = new MyFoo(); |
45 // scoped_refptr<MyFoo> b; | 45 // scoped_refptr<MyFoo> b; |
46 // | 46 // |
47 // b.swap(a); | 47 // b.swap(a); |
48 // // now, |b| references the MyFoo object, and |a| references NULL. | 48 // // now, |b| references the MyFoo object, and |a| references null. |
49 // } | 49 // } |
50 // | 50 // |
51 // To make both |a| and |b| in the above example reference the same MyFoo | 51 // To make both |a| and |b| in the above example reference the same MyFoo |
52 // object, simply use the assignment operator: | 52 // object, simply use the assignment operator: |
53 // | 53 // |
54 // { | 54 // { |
55 // scoped_refptr<MyFoo> a = new MyFoo(); | 55 // scoped_refptr<MyFoo> a = new MyFoo(); |
56 // scoped_refptr<MyFoo> b; | 56 // scoped_refptr<MyFoo> b; |
57 // | 57 // |
58 // b = a; | 58 // b = a; |
59 // // now, |a| and |b| each own a reference to the same MyFoo object. | 59 // // now, |a| and |b| each own a reference to the same MyFoo object. |
60 // } | 60 // } |
61 // | 61 // |
62 | 62 |
63 #ifndef WEBRTC_BASE_SCOPED_REF_PTR_H_ | 63 #ifndef WEBRTC_BASE_SCOPED_REF_PTR_H_ |
64 #define WEBRTC_BASE_SCOPED_REF_PTR_H_ | 64 #define WEBRTC_BASE_SCOPED_REF_PTR_H_ |
65 | 65 |
66 #include <memory> | 66 #include <memory> |
67 | 67 |
68 namespace rtc { | 68 namespace rtc { |
69 | 69 |
70 template <class T> | 70 template <class T> |
71 class scoped_refptr { | 71 class scoped_refptr { |
72 public: | 72 public: |
73 scoped_refptr() : ptr_(NULL) { | 73 scoped_refptr() : ptr_(nullptr) {} |
74 } | |
75 | 74 |
76 scoped_refptr(T* p) : ptr_(p) { | 75 scoped_refptr(T* p) : ptr_(p) { |
77 if (ptr_) | 76 if (ptr_) |
78 ptr_->AddRef(); | 77 ptr_->AddRef(); |
79 } | 78 } |
80 | 79 |
81 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 80 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
82 if (ptr_) | 81 if (ptr_) |
83 ptr_->AddRef(); | 82 ptr_->AddRef(); |
84 } | 83 } |
(...skipping 14 matching lines...) Expand all Loading... |
99 if (ptr_) | 98 if (ptr_) |
100 ptr_->Release(); | 99 ptr_->Release(); |
101 } | 100 } |
102 | 101 |
103 T* get() const { return ptr_; } | 102 T* get() const { return ptr_; } |
104 operator T*() const { return ptr_; } | 103 operator T*() const { return ptr_; } |
105 T* operator->() const { return ptr_; } | 104 T* operator->() const { return ptr_; } |
106 | 105 |
107 // Release a pointer. | 106 // Release a pointer. |
108 // The return value is the current pointer held by this object. | 107 // The return value is the current pointer held by this object. |
109 // If this object holds a NULL pointer, the return value is NULL. | 108 // If this object holds a null pointer, the return value is null. |
110 // After this operation, this object will hold a NULL pointer, | 109 // After this operation, this object will hold a null pointer, |
111 // and will not own the object any more. | 110 // and will not own the object any more. |
112 T* release() { | 111 T* release() { |
113 T* retVal = ptr_; | 112 T* retVal = ptr_; |
114 ptr_ = NULL; | 113 ptr_ = nullptr; |
115 return retVal; | 114 return retVal; |
116 } | 115 } |
117 | 116 |
118 scoped_refptr<T>& operator=(T* p) { | 117 scoped_refptr<T>& operator=(T* p) { |
119 // AddRef first so that self assignment should work | 118 // AddRef first so that self assignment should work |
120 if (p) | 119 if (p) |
121 p->AddRef(); | 120 p->AddRef(); |
122 if (ptr_ ) | 121 if (ptr_ ) |
123 ptr_ ->Release(); | 122 ptr_ ->Release(); |
124 ptr_ = p; | 123 ptr_ = p; |
(...skipping 30 matching lines...) Expand all Loading... |
155 swap(&r.ptr_); | 154 swap(&r.ptr_); |
156 } | 155 } |
157 | 156 |
158 protected: | 157 protected: |
159 T* ptr_; | 158 T* ptr_; |
160 }; | 159 }; |
161 | 160 |
162 } // namespace rtc | 161 } // namespace rtc |
163 | 162 |
164 #endif // WEBRTC_BASE_SCOPED_REF_PTR_H_ | 163 #endif // WEBRTC_BASE_SCOPED_REF_PTR_H_ |
OLD | NEW |