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 #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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 if (ptr_) | 235 if (ptr_) |
236 ptr_->AddRef(); | 236 ptr_->AddRef(); |
237 } | 237 } |
238 | 238 |
239 ~scoped_refptr() { | 239 ~scoped_refptr() { |
240 if (ptr_) | 240 if (ptr_) |
241 ptr_->Release(); | 241 ptr_->Release(); |
242 } | 242 } |
243 | 243 |
244 T* get() const { return ptr_; } | 244 T* get() const { return ptr_; } |
245 operator T*() const { return ptr_; } | |
246 T* operator->() const { return ptr_; } | 245 T* operator->() const { return ptr_; } |
247 | 246 |
| 247 // This operator allows a scoped_refptr to be safely evaluated as bool, using |
| 248 // the "Safe bool" idiom. By "safely", we mean that we are opening up as few |
| 249 // unsafe operations as possible. Simply using operator bool() can allow |
| 250 // silliness like: |
| 251 // int x = my_scoped_refptr; |
| 252 // return (my_scoped_refptr < 5); |
| 253 // See, e.g., http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool |
| 254 // for more information on this idiom. |
| 255 typedef void (scoped_refptr<T>::*BoolType)() const; |
| 256 void this_type_does_not_support_comparisons() const {} |
| 257 operator BoolType() const { |
| 258 return ptr_ ? &scoped_refptr<T>::this_type_does_not_support_comparisons : |
| 259 NULL; |
| 260 } |
| 261 |
| 262 // Allow scoped_refptr<T> to be used in tree-based containers. Note that |
| 263 // operator< on pointers is technically implementation-defined, but it works |
| 264 // on all our platforms and is a common idiom in Chromium anyway. |
| 265 bool operator<(const scoped_refptr<T>& right) const { |
| 266 return ptr_ < right.ptr_; |
| 267 } |
| 268 |
248 // Release a pointer. | 269 // Release a pointer. |
249 // The return value is the current pointer held by this object. | 270 // The return value is the current pointer held by this object. |
250 // If this object holds a NULL pointer, the return value is NULL. | 271 // If this object holds a NULL pointer, the return value is NULL. |
251 // After this operation, this object will hold a NULL pointer, | 272 // After this operation, this object will hold a NULL pointer, |
252 // and will not own the object any more. | 273 // and will not own the object any more. |
253 T* release() { | 274 T* release() { |
254 T* retVal = ptr_; | 275 T* retVal = ptr_; |
255 ptr_ = NULL; | 276 ptr_ = NULL; |
256 return retVal; | 277 return retVal; |
257 } | 278 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 }; | 312 }; |
292 | 313 |
293 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 314 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
294 // having to retype all the template arguments | 315 // having to retype all the template arguments |
295 template <typename T> | 316 template <typename T> |
296 scoped_refptr<T> make_scoped_refptr(T* t) { | 317 scoped_refptr<T> make_scoped_refptr(T* t) { |
297 return scoped_refptr<T>(t); | 318 return scoped_refptr<T>(t); |
298 } | 319 } |
299 | 320 |
300 #endif // BASE_MEMORY_REF_COUNTED_H_ | 321 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |