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 #include <iosfwd> | 9 #include <iosfwd> |
10 | 10 |
11 #include "base/atomic_ref_count.h" | 11 #include "base/atomic_ref_count.h" |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #ifndef NDEBUG | 14 #ifndef NDEBUG |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #endif | 16 #endif |
17 #include "base/threading/thread_collision_warner.h" | 17 #include "base/threading/thread_collision_warner.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 | 19 |
20 #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_AND
ROID) | |
21 #define DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR | |
22 #endif | |
23 | |
24 namespace base { | 20 namespace base { |
25 | 21 |
26 namespace subtle { | 22 namespace subtle { |
27 | 23 |
28 class BASE_EXPORT RefCountedBase { | 24 class BASE_EXPORT RefCountedBase { |
29 public: | 25 public: |
30 bool HasOneRef() const { return ref_count_ == 1; } | 26 bool HasOneRef() const { return ref_count_ == 1; } |
31 | 27 |
32 protected: | 28 protected: |
33 RefCountedBase() | 29 RefCountedBase() |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 AddRef(ptr_); | 286 AddRef(ptr_); |
291 } | 287 } |
292 | 288 |
293 ~scoped_refptr() { | 289 ~scoped_refptr() { |
294 if (ptr_) | 290 if (ptr_) |
295 Release(ptr_); | 291 Release(ptr_); |
296 } | 292 } |
297 | 293 |
298 T* get() const { return ptr_; } | 294 T* get() const { return ptr_; } |
299 | 295 |
300 #if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
301 // Allow scoped_refptr<C> to be used in boolean expression | |
302 // and comparison operations. | |
303 operator T*() const { return ptr_; } | |
304 #endif | |
305 | |
306 T& operator*() const { | 296 T& operator*() const { |
307 assert(ptr_ != NULL); | 297 assert(ptr_ != NULL); |
308 return *ptr_; | 298 return *ptr_; |
309 } | 299 } |
310 | 300 |
311 T* operator->() const { | 301 T* operator->() const { |
312 assert(ptr_ != NULL); | 302 assert(ptr_ != NULL); |
313 return ptr_; | 303 return ptr_; |
314 } | 304 } |
315 | 305 |
(...skipping 20 matching lines...) Expand all Loading... |
336 void swap(T** pp) { | 326 void swap(T** pp) { |
337 T* p = ptr_; | 327 T* p = ptr_; |
338 ptr_ = *pp; | 328 ptr_ = *pp; |
339 *pp = p; | 329 *pp = p; |
340 } | 330 } |
341 | 331 |
342 void swap(scoped_refptr<T>& r) { | 332 void swap(scoped_refptr<T>& r) { |
343 swap(&r.ptr_); | 333 swap(&r.ptr_); |
344 } | 334 } |
345 | 335 |
346 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | 336 private: |
| 337 // Allow scoped_refptr<T> to be used in boolean expressions, but not |
| 338 // implicitly convertible to a real bool (which is dangerous). |
| 339 // |
| 340 // Note that this trick is only safe when the == and != operators |
| 341 // are declared explicitly, as otherwise "refptr1 == refptr2" |
| 342 // will compile but do the wrong thing (i.e., convert to Testable |
| 343 // and then do the comparison). |
| 344 typedef T* scoped_refptr::*Testable; |
| 345 |
| 346 public: |
| 347 operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; } |
| 348 |
347 template <typename U> | 349 template <typename U> |
348 bool operator==(const scoped_refptr<U>& rhs) const { | 350 bool operator==(const scoped_refptr<U>& rhs) const { |
349 return ptr_ == rhs.get(); | 351 return ptr_ == rhs.get(); |
350 } | 352 } |
351 | 353 |
352 template <typename U> | 354 template <typename U> |
353 bool operator!=(const scoped_refptr<U>& rhs) const { | 355 bool operator!=(const scoped_refptr<U>& rhs) const { |
354 return !operator==(rhs); | 356 return !operator==(rhs); |
355 } | 357 } |
356 | 358 |
357 template <typename U> | 359 template <typename U> |
358 bool operator<(const scoped_refptr<U>& rhs) const { | 360 bool operator<(const scoped_refptr<U>& rhs) const { |
359 return ptr_ < rhs.get(); | 361 return ptr_ < rhs.get(); |
360 } | 362 } |
361 #endif | |
362 | 363 |
363 protected: | 364 protected: |
364 T* ptr_; | 365 T* ptr_; |
365 | 366 |
366 private: | 367 private: |
367 // Non-inline helpers to allow: | 368 // Non-inline helpers to allow: |
368 // class Opaque; | 369 // class Opaque; |
369 // extern template class scoped_refptr<Opaque>; | 370 // extern template class scoped_refptr<Opaque>; |
370 // Otherwise the compiler will complain that Opaque is an incomplete type. | 371 // Otherwise the compiler will complain that Opaque is an incomplete type. |
371 static void AddRef(T* ptr); | 372 static void AddRef(T* ptr); |
(...skipping 10 matching lines...) Expand all Loading... |
382 ptr->Release(); | 383 ptr->Release(); |
383 } | 384 } |
384 | 385 |
385 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 386 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
386 // having to retype all the template arguments | 387 // having to retype all the template arguments |
387 template <typename T> | 388 template <typename T> |
388 scoped_refptr<T> make_scoped_refptr(T* t) { | 389 scoped_refptr<T> make_scoped_refptr(T* t) { |
389 return scoped_refptr<T>(t); | 390 return scoped_refptr<T>(t); |
390 } | 391 } |
391 | 392 |
392 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | 393 // Temporary operator overloads to facilitate the transition. See |
393 // Temporary operator overloads to facilitate the transition... | 394 // https://crbug.com/110610. |
394 template <typename T, typename U> | 395 template <typename T, typename U> |
395 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { | 396 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { |
396 return lhs.get() == rhs; | 397 return lhs.get() == rhs; |
397 } | 398 } |
398 | 399 |
399 template <typename T, typename U> | 400 template <typename T, typename U> |
400 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { | 401 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { |
401 return lhs == rhs.get(); | 402 return lhs == rhs.get(); |
402 } | 403 } |
403 | 404 |
404 template <typename T, typename U> | 405 template <typename T, typename U> |
405 bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { | 406 bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { |
406 return !operator==(lhs, rhs); | 407 return !operator==(lhs, rhs); |
407 } | 408 } |
408 | 409 |
409 template <typename T, typename U> | 410 template <typename T, typename U> |
410 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 411 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
411 return !operator==(lhs, rhs); | 412 return !operator==(lhs, rhs); |
412 } | 413 } |
413 | 414 |
414 template <typename T> | 415 template <typename T> |
415 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 416 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
416 return out << p.get(); | 417 return out << p.get(); |
417 } | 418 } |
418 #endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
419 | 419 |
420 #endif // BASE_MEMORY_REF_COUNTED_H_ | 420 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |