callback_internal.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  3. // found in the LICENSE file.
  4. #include "base/callback_internal.h"
  5. #include "base/check.h"
  6. #include "base/notreached.h"
  7. namespace base {
  8. namespace internal {
  9. namespace {
  10. bool QueryCancellationTraitsForNonCancellables(
  11. const BindStateBase*,
  12. BindStateBase::CancellationQueryMode mode) {
  13. switch (mode) {
  14. case BindStateBase::IS_CANCELLED:
  15. return false;
  16. case BindStateBase::MAYBE_VALID:
  17. return true;
  18. }
  19. NOTREACHED();
  20. }
  21. } // namespace
  22. void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) {
  23. bind_state->destructor_(bind_state);
  24. }
  25. BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
  26. void (*destructor)(const BindStateBase*))
  27. : BindStateBase(polymorphic_invoke,
  28. destructor,
  29. &QueryCancellationTraitsForNonCancellables) {}
  30. BindStateBase::BindStateBase(
  31. InvokeFuncStorage polymorphic_invoke,
  32. void (*destructor)(const BindStateBase*),
  33. bool (*query_cancellation_traits)(const BindStateBase*,
  34. CancellationQueryMode))
  35. : polymorphic_invoke_(polymorphic_invoke),
  36. destructor_(destructor),
  37. query_cancellation_traits_(query_cancellation_traits) {}
  38. CallbackBase& CallbackBase::operator=(CallbackBase&& c) noexcept = default;
  39. CallbackBase::CallbackBase(const CallbackBaseCopyable& c)
  40. : bind_state_(c.bind_state_) {}
  41. CallbackBase& CallbackBase::operator=(const CallbackBaseCopyable& c) {
  42. bind_state_ = c.bind_state_;
  43. return *this;
  44. }
  45. CallbackBase::CallbackBase(CallbackBaseCopyable&& c) noexcept
  46. : bind_state_(std::move(c.bind_state_)) {}
  47. CallbackBase& CallbackBase::operator=(CallbackBaseCopyable&& c) noexcept {
  48. bind_state_ = std::move(c.bind_state_);
  49. return *this;
  50. }
  51. void CallbackBase::Reset() {
  52. // NULL the bind_state_ last, since it may be holding the last ref to whatever
  53. // object owns us, and we may be deleted after that.
  54. bind_state_ = nullptr;
  55. }
  56. bool CallbackBase::IsCancelled() const {
  57. DCHECK(bind_state_);
  58. return bind_state_->IsCancelled();
  59. }
  60. bool CallbackBase::MaybeValid() const {
  61. DCHECK(bind_state_);
  62. return bind_state_->MaybeValid();
  63. }
  64. bool CallbackBase::EqualsInternal(const CallbackBase& other) const {
  65. return bind_state_ == other.bind_state_;
  66. }
  67. CallbackBase::~CallbackBase() = default;
  68. CallbackBaseCopyable::CallbackBaseCopyable(const CallbackBaseCopyable& c) {
  69. bind_state_ = c.bind_state_;
  70. }
  71. CallbackBaseCopyable& CallbackBaseCopyable::operator=(
  72. const CallbackBaseCopyable& c) {
  73. bind_state_ = c.bind_state_;
  74. return *this;
  75. }
  76. CallbackBaseCopyable& CallbackBaseCopyable::operator=(
  77. CallbackBaseCopyable&& c) noexcept = default;
  78. } // namespace internal
  79. } // namespace base