blob: 3a665d28cae8fc6d5280acad88313de0a8a8ec41 [file] [log] [blame]
[email protected]0f450362012-06-09 02:11:011// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]59eff912011-02-18 23:29:312// 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
[email protected]e24f8762011-12-20 00:10:047#include "base/logging.h"
8
[email protected]59eff912011-02-18 23:29:319namespace base {
10namespace internal {
11
tzik1fdcca32016-09-14 07:15:0012namespace {
13
14bool ReturnFalse(const BindStateBase*) {
15 return false;
16}
17
18} // namespace
19
tzik65adef82017-03-30 06:45:2120void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) {
21 bind_state->destructor_(bind_state);
22}
23
tzik1fdcca32016-09-14 07:15:0024BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
tzik30e0c312016-09-21 08:06:5425 void (*destructor)(const BindStateBase*))
tzik1fdcca32016-09-14 07:15:0026 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
27}
28
tzikf1b377722016-09-08 06:58:0729BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
tzik30e0c312016-09-21 08:06:5430 void (*destructor)(const BindStateBase*),
tzik59aa6bb12016-09-08 10:58:5331 bool (*is_cancelled)(const BindStateBase*))
tzik1fdcca32016-09-14 07:15:0032 : polymorphic_invoke_(polymorphic_invoke),
tzik1fdcca32016-09-14 07:15:0033 destructor_(destructor),
34 is_cancelled_(is_cancelled) {}
tzikf1b377722016-09-08 06:58:0735
tzik9edfd1de2016-07-20 12:56:4536CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default;
[email protected]59eff912011-02-18 23:29:3137
tzik77d411392016-03-09 09:47:0338CallbackBase<CopyMode::MoveOnly>&
tzik9edfd1de2016-07-20 12:56:4539CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0340
tzik27d1e312016-09-13 05:28:5941CallbackBase<CopyMode::MoveOnly>::CallbackBase(
42 const CallbackBase<CopyMode::Copyable>& c)
43 : bind_state_(c.bind_state_) {}
44
45CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=(
46 const CallbackBase<CopyMode::Copyable>& c) {
47 bind_state_ = c.bind_state_;
48 return *this;
49}
50
tzikf44c2f8d2017-03-08 08:41:1551CallbackBase<CopyMode::MoveOnly>::CallbackBase(
52 CallbackBase<CopyMode::Copyable>&& c)
53 : bind_state_(std::move(c.bind_state_)) {}
54
55CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=(
56 CallbackBase<CopyMode::Copyable>&& c) {
57 bind_state_ = std::move(c.bind_state_);
58 return *this;
59}
60
tzik77d411392016-03-09 09:47:0361void CallbackBase<CopyMode::MoveOnly>::Reset() {
tzik77d411392016-03-09 09:47:0362 // NULL the bind_state_ last, since it may be holding the last ref to whatever
63 // object owns us, and we may be deleted after that.
64 bind_state_ = nullptr;
65}
66
tzik59aa6bb12016-09-08 10:58:5367bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const {
68 DCHECK(bind_state_);
69 return bind_state_->IsCancelled();
70}
71
tzik77d411392016-03-09 09:47:0372bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
73 const CallbackBase& other) const {
tzik1886c272016-09-08 05:45:3874 return bind_state_ == other.bind_state_;
[email protected]59eff912011-02-18 23:29:3175}
76
tzik77d411392016-03-09 09:47:0377CallbackBase<CopyMode::MoveOnly>::CallbackBase(
78 BindStateBase* bind_state)
79 : bind_state_(bind_state) {
tzik65adef82017-03-30 06:45:2180 DCHECK(!bind_state_.get() || bind_state_->HasOneRef());
[email protected]59eff912011-02-18 23:29:3181}
82
tzik77d411392016-03-09 09:47:0383CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
84
85CallbackBase<CopyMode::Copyable>::CallbackBase(
86 const CallbackBase& c)
87 : CallbackBase<CopyMode::MoveOnly>(nullptr) {
88 bind_state_ = c.bind_state_;
[email protected]59eff912011-02-18 23:29:3189}
90
tzik9edfd1de2016-07-20 12:56:4591CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0392
93CallbackBase<CopyMode::Copyable>&
94CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
95 bind_state_ = c.bind_state_;
tzik77d411392016-03-09 09:47:0396 return *this;
97}
98
99CallbackBase<CopyMode::Copyable>&
tzik9edfd1de2016-07-20 12:56:45100CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:03101
102template class CallbackBase<CopyMode::MoveOnly>;
103template class CallbackBase<CopyMode::Copyable>;
104
[email protected]59eff912011-02-18 23:29:31105} // namespace internal
[email protected]0f450362012-06-09 02:11:01106} // namespace base