You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2016 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/weak_ptr.h"
  11. // The implementation is borrowed from chromium except that it does not
  12. // implement SupportsWeakPtr.
  13. namespace rtc {
  14. namespace internal {
  15. WeakReference::Flag::Flag() : is_valid_(true) {
  16. // Flags only become bound when checked for validity, or invalidated,
  17. // so that we can check that later validity/invalidation operations on
  18. // the same Flag take place on the same sequence.
  19. checker_.Detach();
  20. }
  21. void WeakReference::Flag::Invalidate() {
  22. RTC_DCHECK(checker_.IsCurrent())
  23. << "WeakPtrs must be invalidated on the same sequence.";
  24. is_valid_ = false;
  25. }
  26. bool WeakReference::Flag::IsValid() const {
  27. RTC_DCHECK(checker_.IsCurrent())
  28. << "WeakPtrs must be checked on the same sequence.";
  29. return is_valid_;
  30. }
  31. WeakReference::Flag::~Flag() {}
  32. WeakReference::WeakReference() {}
  33. WeakReference::WeakReference(const Flag* flag) : flag_(flag) {}
  34. WeakReference::~WeakReference() {}
  35. WeakReference::WeakReference(WeakReference&& other) = default;
  36. WeakReference::WeakReference(const WeakReference& other) = default;
  37. bool WeakReference::is_valid() const {
  38. return flag_.get() && flag_->IsValid();
  39. }
  40. WeakReferenceOwner::WeakReferenceOwner() {}
  41. WeakReferenceOwner::~WeakReferenceOwner() {
  42. Invalidate();
  43. }
  44. WeakReference WeakReferenceOwner::GetRef() const {
  45. // If we hold the last reference to the Flag then create a new one.
  46. if (!HasRefs())
  47. flag_ = new RefCountedObject<WeakReference::Flag>();
  48. return WeakReference(flag_.get());
  49. }
  50. void WeakReferenceOwner::Invalidate() {
  51. if (flag_.get()) {
  52. flag_->Invalidate();
  53. flag_ = nullptr;
  54. }
  55. }
  56. WeakPtrBase::WeakPtrBase() {}
  57. WeakPtrBase::~WeakPtrBase() {}
  58. WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {}
  59. } // namespace internal
  60. } // namespace rtc