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 | 10 |
10 #include "base/atomic_ref_count.h" | 11 #include "base/atomic_ref_count.h" |
11 #include "base/base_export.h" | 12 #include "base/base_export.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #ifndef NDEBUG | 14 #ifndef NDEBUG |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #endif | 16 #endif |
16 #include "base/threading/thread_collision_warner.h" | 17 #include "base/threading/thread_collision_warner.h" |
| 18 #include "build/build_config.h" |
| 19 |
| 20 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 21 #define DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR |
| 22 #endif |
17 | 23 |
18 namespace base { | 24 namespace base { |
19 | 25 |
20 namespace subtle { | 26 namespace subtle { |
21 | 27 |
22 class BASE_EXPORT RefCountedBase { | 28 class BASE_EXPORT RefCountedBase { |
23 public: | 29 public: |
24 bool HasOneRef() const { return ref_count_ == 1; } | 30 bool HasOneRef() const { return ref_count_ == 1; } |
25 | 31 |
26 protected: | 32 protected: |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 ptr_->AddRef(); | 290 ptr_->AddRef(); |
285 } | 291 } |
286 | 292 |
287 ~scoped_refptr() { | 293 ~scoped_refptr() { |
288 if (ptr_) | 294 if (ptr_) |
289 ptr_->Release(); | 295 ptr_->Release(); |
290 } | 296 } |
291 | 297 |
292 T* get() const { return ptr_; } | 298 T* get() const { return ptr_; } |
293 | 299 |
| 300 #if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) |
294 // Allow scoped_refptr<C> to be used in boolean expression | 301 // Allow scoped_refptr<C> to be used in boolean expression |
295 // and comparison operations. | 302 // and comparison operations. |
296 operator T*() const { return ptr_; } | 303 operator T*() const { return ptr_; } |
| 304 #endif |
297 | 305 |
298 T& operator*() const { | 306 T& operator*() const { |
299 assert(ptr_ != NULL); | 307 assert(ptr_ != NULL); |
300 return *ptr_; | 308 return *ptr_; |
301 } | 309 } |
302 | 310 |
303 T* operator->() const { | 311 T* operator->() const { |
304 assert(ptr_ != NULL); | 312 assert(ptr_ != NULL); |
305 return ptr_; | 313 return ptr_; |
306 } | 314 } |
(...skipping 21 matching lines...) Expand all Loading... |
328 void swap(T** pp) { | 336 void swap(T** pp) { |
329 T* p = ptr_; | 337 T* p = ptr_; |
330 ptr_ = *pp; | 338 ptr_ = *pp; |
331 *pp = p; | 339 *pp = p; |
332 } | 340 } |
333 | 341 |
334 void swap(scoped_refptr<T>& r) { | 342 void swap(scoped_refptr<T>& r) { |
335 swap(&r.ptr_); | 343 swap(&r.ptr_); |
336 } | 344 } |
337 | 345 |
| 346 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) |
| 347 template <typename U> |
| 348 bool operator==(const scoped_refptr<U>& rhs) const { |
| 349 return ptr_ == rhs.get(); |
| 350 } |
| 351 |
| 352 template <typename U> |
| 353 bool operator!=(const scoped_refptr<U>& rhs) const { |
| 354 return !operator==(rhs); |
| 355 } |
| 356 |
| 357 template <typename U> |
| 358 bool operator<(const scoped_refptr<U>& rhs) const { |
| 359 return ptr_ < rhs.get(); |
| 360 } |
| 361 #endif |
| 362 |
338 protected: | 363 protected: |
339 T* ptr_; | 364 T* ptr_; |
340 }; | 365 }; |
341 | 366 |
342 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 367 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
343 // having to retype all the template arguments | 368 // having to retype all the template arguments |
344 template <typename T> | 369 template <typename T> |
345 scoped_refptr<T> make_scoped_refptr(T* t) { | 370 scoped_refptr<T> make_scoped_refptr(T* t) { |
346 return scoped_refptr<T>(t); | 371 return scoped_refptr<T>(t); |
347 } | 372 } |
348 | 373 |
| 374 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) |
| 375 // Temporary operator overloads to facilitate the transition... |
| 376 template <typename T, typename U> |
| 377 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { |
| 378 return lhs.get() == rhs; |
| 379 } |
| 380 |
| 381 template <typename T, typename U> |
| 382 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { |
| 383 return lhs == rhs.get(); |
| 384 } |
| 385 |
| 386 template <typename T, typename U> |
| 387 bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { |
| 388 return !operator==(lhs, rhs); |
| 389 } |
| 390 |
| 391 template <typename T, typename U> |
| 392 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
| 393 return !operator==(lhs, rhs); |
| 394 } |
| 395 |
| 396 template <typename T> |
| 397 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 398 return out << p.get(); |
| 399 } |
| 400 #endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) |
| 401 |
349 #endif // BASE_MEMORY_REF_COUNTED_H_ | 402 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |