• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ReturnFalse(const BindStateBase *)14 bool ReturnFalse(const BindStateBase*) {
15   return false;
16 }
17 
18 }  // namespace
19 
Destruct(const BindStateBase * bind_state)20 void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) {
21   bind_state->destructor_(bind_state);
22 }
23 
BindStateBase(InvokeFuncStorage polymorphic_invoke,void (* destructor)(const BindStateBase *))24 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
25                              void (*destructor)(const BindStateBase*))
26     : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
27 }
28 
BindStateBase(InvokeFuncStorage polymorphic_invoke,void (* destructor)(const BindStateBase *),bool (* is_cancelled)(const BindStateBase *))29 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
30                              void (*destructor)(const BindStateBase*),
31                              bool (*is_cancelled)(const BindStateBase*))
32     : polymorphic_invoke_(polymorphic_invoke),
33       destructor_(destructor),
34       is_cancelled_(is_cancelled) {}
35 
36 CallbackBase& CallbackBase::operator=(CallbackBase&& c) noexcept = default;
CallbackBase(const CallbackBaseCopyable & c)37 CallbackBase::CallbackBase(const CallbackBaseCopyable& c)
38     : bind_state_(c.bind_state_) {}
39 
operator =(const CallbackBaseCopyable & c)40 CallbackBase& CallbackBase::operator=(const CallbackBaseCopyable& c) {
41   bind_state_ = c.bind_state_;
42   return *this;
43 }
44 
CallbackBase(CallbackBaseCopyable && c)45 CallbackBase::CallbackBase(CallbackBaseCopyable&& c) noexcept
46     : bind_state_(std::move(c.bind_state_)) {}
47 
operator =(CallbackBaseCopyable && c)48 CallbackBase& CallbackBase::operator=(CallbackBaseCopyable&& c) noexcept {
49   bind_state_ = std::move(c.bind_state_);
50   return *this;
51 }
52 
Reset()53 void CallbackBase::Reset() {
54   // NULL the bind_state_ last, since it may be holding the last ref to whatever
55   // object owns us, and we may be deleted after that.
56   bind_state_ = nullptr;
57 }
58 
IsCancelled() const59 bool CallbackBase::IsCancelled() const {
60   DCHECK(bind_state_);
61   return bind_state_->IsCancelled();
62 }
63 
EqualsInternal(const CallbackBase & other) const64 bool CallbackBase::EqualsInternal(const CallbackBase& other) const {
65   return bind_state_ == other.bind_state_;
66 }
67 
68 CallbackBase::~CallbackBase() = default;
69 
CallbackBaseCopyable(const CallbackBaseCopyable & c)70 CallbackBaseCopyable::CallbackBaseCopyable(const CallbackBaseCopyable& c) {
71   bind_state_ = c.bind_state_;
72 }
73 
operator =(const CallbackBaseCopyable & c)74 CallbackBaseCopyable& CallbackBaseCopyable::operator=(
75     const CallbackBaseCopyable& c) {
76   bind_state_ = c.bind_state_;
77   return *this;
78 }
79 
80 CallbackBaseCopyable& CallbackBaseCopyable::operator=(
81     CallbackBaseCopyable&& c) noexcept = default;
82 
83 }  // namespace internal
84 }  // namespace base
85