blob: e29dd694efb5e2fabc899ef1183fbac333b042b2 [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
tzikf1b377722016-09-08 06:58:0712BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
tzik59aa6bb12016-09-08 10:58:5313 void (*destructor)(BindStateBase*),
14 bool (*is_cancelled)(const BindStateBase*))
tzikf1b377722016-09-08 06:58:0715 : polymorphic_invoke_(polymorphic_invoke),
tzik59aa6bb12016-09-08 10:58:5316 ref_count_(0),
17 destructor_(destructor),
18 is_cancelled_(is_cancelled) {}
tzikf1b377722016-09-08 06:58:0719
taptede7e804c2015-05-14 08:03:3220void BindStateBase::AddRef() {
21 AtomicRefCountInc(&ref_count_);
22}
23
24void BindStateBase::Release() {
25 if (!AtomicRefCountDec(&ref_count_))
26 destructor_(this);
27}
28
tzik9edfd1de2016-07-20 12:56:4529CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default;
[email protected]59eff912011-02-18 23:29:3130
tzik77d411392016-03-09 09:47:0331CallbackBase<CopyMode::MoveOnly>&
tzik9edfd1de2016-07-20 12:56:4532CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0333
34void CallbackBase<CopyMode::MoveOnly>::Reset() {
tzik77d411392016-03-09 09:47:0335 // NULL the bind_state_ last, since it may be holding the last ref to whatever
36 // object owns us, and we may be deleted after that.
37 bind_state_ = nullptr;
38}
39
tzik59aa6bb12016-09-08 10:58:5340bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const {
41 DCHECK(bind_state_);
42 return bind_state_->IsCancelled();
43}
44
tzik77d411392016-03-09 09:47:0345bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
46 const CallbackBase& other) const {
tzik1886c272016-09-08 05:45:3847 return bind_state_ == other.bind_state_;
[email protected]59eff912011-02-18 23:29:3148}
49
tzik77d411392016-03-09 09:47:0350CallbackBase<CopyMode::MoveOnly>::CallbackBase(
51 BindStateBase* bind_state)
52 : bind_state_(bind_state) {
taptede7e804c2015-05-14 08:03:3253 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1);
[email protected]59eff912011-02-18 23:29:3154}
55
tzik77d411392016-03-09 09:47:0356CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
57
58CallbackBase<CopyMode::Copyable>::CallbackBase(
59 const CallbackBase& c)
60 : CallbackBase<CopyMode::MoveOnly>(nullptr) {
61 bind_state_ = c.bind_state_;
[email protected]59eff912011-02-18 23:29:3162}
63
tzik9edfd1de2016-07-20 12:56:4564CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0365
66CallbackBase<CopyMode::Copyable>&
67CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
68 bind_state_ = c.bind_state_;
tzik77d411392016-03-09 09:47:0369 return *this;
70}
71
72CallbackBase<CopyMode::Copyable>&
tzik9edfd1de2016-07-20 12:56:4573CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0374
75template class CallbackBase<CopyMode::MoveOnly>;
76template class CallbackBase<CopyMode::Copyable>;
77
[email protected]59eff912011-02-18 23:29:3178} // namespace internal
[email protected]0f450362012-06-09 02:11:0179} // namespace base