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 }
25 
26 }  // namespace
27 
Destruct(const BindStateBase * bind_state)28 void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) {
29   bind_state->destructor_(bind_state);
30 }
31 
BindStateBase(InvokeFuncStorage polymorphic_invoke,void (* destructor)(const BindStateBase *))32 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
33                              void (*destructor)(const BindStateBase*))
34     : BindStateBase(polymorphic_invoke,
35                     destructor,
36                     &QueryCancellationTraitsForNonCancellables) {}
37 
BindStateBase(InvokeFuncStorage polymorphic_invoke,void (* destructor)(const BindStateBase *),bool (* query_cancellation_traits)(const BindStateBase *,CancellationQueryMode))38 BindStateBase::BindStateBase(
39     InvokeFuncStorage polymorphic_invoke,
40     void (*destructor)(const BindStateBase*),
41     bool (*query_cancellation_traits)(const BindStateBase*,
42                                       CancellationQueryMode))
43     : polymorphic_invoke_(polymorphic_invoke),
44       destructor_(destructor),
45       query_cancellation_traits_(query_cancellation_traits) {}
46 
47 CallbackBase& CallbackBase::operator=(CallbackBase&& c) noexcept = default;
CallbackBase(const CallbackBaseCopyable & c)48 CallbackBase::CallbackBase(const CallbackBaseCopyable& c)
49     : bind_state_(c.bind_state_) {}
50 
operator =(const CallbackBaseCopyable & c)51 CallbackBase& CallbackBase::operator=(const CallbackBaseCopyable& c) {
52   bind_state_ = c.bind_state_;
53   return *this;
54 }
55 
CallbackBase(CallbackBaseCopyable && c)56 CallbackBase::CallbackBase(CallbackBaseCopyable&& c) noexcept
57     : bind_state_(std::move(c.bind_state_)) {}
58 
operator =(CallbackBaseCopyable && c)59 CallbackBase& CallbackBase::operator=(CallbackBaseCopyable&& c) noexcept {
60   bind_state_ = std::move(c.bind_state_);
61   return *this;
62 }
63 
Reset()64 void CallbackBase::Reset() {
65   // NULL the bind_state_ last, since it may be holding the last ref to whatever
66   // object owns us, and we may be deleted after that.
67   bind_state_ = nullptr;
68 }
69 
IsCancelled() const70 bool CallbackBase::IsCancelled() const {
71   DCHECK(bind_state_);
72   return bind_state_->IsCancelled();
73 }
74 
MaybeValid() const75 bool CallbackBase::MaybeValid() const {
76   DCHECK(bind_state_);
77   return bind_state_->MaybeValid();
78 }
79 
EqualsInternal(const CallbackBase & other) const80 bool CallbackBase::EqualsInternal(const CallbackBase& other) const {
81   return bind_state_ == other.bind_state_;
82 }
83 
84 CallbackBase::~CallbackBase() = default;
85 
CallbackBaseCopyable(const CallbackBaseCopyable & c)86 CallbackBaseCopyable::CallbackBaseCopyable(const CallbackBaseCopyable& c) {
87   bind_state_ = c.bind_state_;
88 }
89 
operator =(const CallbackBaseCopyable & c)90 CallbackBaseCopyable& CallbackBaseCopyable::operator=(
91     const CallbackBaseCopyable& c) {
92   bind_state_ = c.bind_state_;
93   return *this;
94 }
95 
96 CallbackBaseCopyable& CallbackBaseCopyable::operator=(
97     CallbackBaseCopyable&& c) noexcept = default;
98 
99 }  // namespace internal
100 }  // namespace base
101