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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 <stddef.h> | 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_(NULL) { |
74 } | 74 } |
75 | 75 |
76 scoped_refptr(T* p) : ptr_(p) { | 76 scoped_refptr(T* p) : ptr_(p) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 | 127 |
128 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { | 128 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { |
129 return *this = r.ptr_; | 129 return *this = r.ptr_; |
130 } | 130 } |
131 | 131 |
132 template <typename U> | 132 template <typename U> |
133 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { | 133 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { |
134 return *this = r.get(); | 134 return *this = r.get(); |
135 } | 135 } |
136 | 136 |
137 scoped_refptr<T>& operator=(scoped_refptr<T>&& r) { | |
138 scoped_refptr<T>(std::move(r)).swap(*this); | |
Taylor Brandstetter
2016/08/01 20:00:36
I realize this uses the same method as Chromium's
| |
139 return *this; | |
140 } | |
141 | |
142 template <typename U> | |
143 scoped_refptr<T>& operator=(scoped_refptr<U>&& r) { | |
144 scoped_refptr<T>(std::move(r)).swap(*this); | |
145 return *this; | |
146 } | |
147 | |
137 void swap(T** pp) { | 148 void swap(T** pp) { |
138 T* p = ptr_; | 149 T* p = ptr_; |
139 ptr_ = *pp; | 150 ptr_ = *pp; |
140 *pp = p; | 151 *pp = p; |
141 } | 152 } |
142 | 153 |
143 void swap(scoped_refptr<T>& r) { | 154 void swap(scoped_refptr<T>& r) { |
144 swap(&r.ptr_); | 155 swap(&r.ptr_); |
145 } | 156 } |
146 | 157 |
147 protected: | 158 protected: |
148 T* ptr_; | 159 T* ptr_; |
149 }; | 160 }; |
150 | 161 |
151 } // namespace rtc | 162 } // namespace rtc |
152 | 163 |
153 #endif // WEBRTC_BASE_SCOPED_REF_PTR_H_ | 164 #endif // WEBRTC_BASE_SCOPED_REF_PTR_H_ |
OLD | NEW |