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