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