OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/atomic_ref_count.h" | 9 #include "base/atomic_ref_count.h" |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 T* release() { | 253 T* release() { |
254 T* retVal = ptr_; | 254 T* retVal = ptr_; |
255 ptr_ = NULL; | 255 ptr_ = NULL; |
256 return retVal; | 256 return retVal; |
257 } | 257 } |
258 | 258 |
259 scoped_refptr<T>& operator=(T* p) { | 259 scoped_refptr<T>& operator=(T* p) { |
260 // AddRef first so that self assignment should work | 260 // AddRef first so that self assignment should work |
261 if (p) | 261 if (p) |
262 p->AddRef(); | 262 p->AddRef(); |
263 if (ptr_ ) | 263 if (ptr_) { |
264 ptr_ ->Release(); | 264 T* old_ptr = ptr_; |
265 ptr_ = p; | 265 ptr_ = p; |
266 old_ptr->Release(); | |
267 } else { | |
268 ptr_ = p; | |
269 } | |
joth
2011/12/21 16:51:27
Maybe marginally easier to read as..
T* old_ptr =
| |
266 return *this; | 270 return *this; |
267 } | 271 } |
268 | 272 |
269 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { | 273 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { |
270 return *this = r.ptr_; | 274 return *this = r.ptr_; |
271 } | 275 } |
272 | 276 |
273 template <typename U> | 277 template <typename U> |
274 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { | 278 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { |
275 return *this = r.get(); | 279 return *this = r.get(); |
(...skipping 14 matching lines...) Expand all Loading... | |
290 }; | 294 }; |
291 | 295 |
292 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 296 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
293 // having to retype all the template arguments | 297 // having to retype all the template arguments |
294 template <typename T> | 298 template <typename T> |
295 scoped_refptr<T> make_scoped_refptr(T* t) { | 299 scoped_refptr<T> make_scoped_refptr(T* t) { |
296 return scoped_refptr<T>(t); | 300 return scoped_refptr<T>(t); |
297 } | 301 } |
298 | 302 |
299 #endif // BASE_MEMORY_REF_COUNTED_H_ | 303 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |