| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
| 8 #include <cassert> | 8 #include <cassert> |
| 9 | 9 |
| 10 #include "base/atomic_ref_count.h" | 10 #include "base/atomic_ref_count.h" |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 ptr_->Release(); | 250 ptr_->Release(); |
| 251 } | 251 } |
| 252 | 252 |
| 253 T* get() const { return ptr_; } | 253 T* get() const { return ptr_; } |
| 254 operator T*() const { return ptr_; } | 254 operator T*() const { return ptr_; } |
| 255 T* operator->() const { | 255 T* operator->() const { |
| 256 assert(ptr_ != NULL); | 256 assert(ptr_ != NULL); |
| 257 return ptr_; | 257 return ptr_; |
| 258 } | 258 } |
| 259 | 259 |
| 260 // Release a pointer. | |
| 261 // The return value is the current pointer held by this object. | |
| 262 // If this object holds a NULL pointer, the return value is NULL. | |
| 263 // After this operation, this object will hold a NULL pointer, | |
| 264 // and will not own the object any more. | |
| 265 T* release() WARN_UNUSED_RESULT { | |
| 266 T* retVal = ptr_; | |
| 267 ptr_ = NULL; | |
| 268 return retVal; | |
| 269 } | |
| 270 | |
| 271 scoped_refptr<T>& operator=(T* p) { | 260 scoped_refptr<T>& operator=(T* p) { |
| 272 // AddRef first so that self assignment should work | 261 // AddRef first so that self assignment should work |
| 273 if (p) | 262 if (p) |
| 274 p->AddRef(); | 263 p->AddRef(); |
| 275 T* old_ptr = ptr_; | 264 T* old_ptr = ptr_; |
| 276 ptr_ = p; | 265 ptr_ = p; |
| 277 if (old_ptr) | 266 if (old_ptr) |
| 278 old_ptr->Release(); | 267 old_ptr->Release(); |
| 279 return *this; | 268 return *this; |
| 280 } | 269 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 303 }; | 292 }; |
| 304 | 293 |
| 305 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 294 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
| 306 // having to retype all the template arguments | 295 // having to retype all the template arguments |
| 307 template <typename T> | 296 template <typename T> |
| 308 scoped_refptr<T> make_scoped_refptr(T* t) { | 297 scoped_refptr<T> make_scoped_refptr(T* t) { |
| 309 return scoped_refptr<T>(t); | 298 return scoped_refptr<T>(t); |
| 310 } | 299 } |
| 311 | 300 |
| 312 #endif // BASE_MEMORY_REF_COUNTED_H_ | 301 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |