blob: b37a319f103eeef925b36a281d4ad6f30f78757f [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
tzik27d1e312016-09-13 05:28:5934CallbackBase<CopyMode::MoveOnly>::CallbackBase(
35 const CallbackBase<CopyMode::Copyable>& c)
36 : bind_state_(c.bind_state_) {}
37
38CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=(
39 const CallbackBase<CopyMode::Copyable>& c) {
40 bind_state_ = c.bind_state_;
41 return *this;
42}
43
tzik77d411392016-03-09 09:47:0344void CallbackBase<CopyMode::MoveOnly>::Reset() {
tzik77d411392016-03-09 09:47:0345 // NULL the bind_state_ last, since it may be holding the last ref to whatever
46 // object owns us, and we may be deleted after that.
47 bind_state_ = nullptr;
48}
49
tzik59aa6bb12016-09-08 10:58:5350bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const {
51 DCHECK(bind_state_);
52 return bind_state_->IsCancelled();
53}
54
tzik77d411392016-03-09 09:47:0355bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
56 const CallbackBase& other) const {
tzik1886c272016-09-08 05:45:3857 return bind_state_ == other.bind_state_;
[email protected]59eff912011-02-18 23:29:3158}
59
tzik77d411392016-03-09 09:47:0360CallbackBase<CopyMode::MoveOnly>::CallbackBase(
61 BindStateBase* bind_state)
62 : bind_state_(bind_state) {
taptede7e804c2015-05-14 08:03:3263 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1);
[email protected]59eff912011-02-18 23:29:3164}
65
tzik77d411392016-03-09 09:47:0366CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
67
68CallbackBase<CopyMode::Copyable>::CallbackBase(
69 const CallbackBase& c)
70 : CallbackBase<CopyMode::MoveOnly>(nullptr) {
71 bind_state_ = c.bind_state_;
[email protected]59eff912011-02-18 23:29:3172}
73
tzik9edfd1de2016-07-20 12:56:4574CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0375
76CallbackBase<CopyMode::Copyable>&
77CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
78 bind_state_ = c.bind_state_;
tzik77d411392016-03-09 09:47:0379 return *this;
80}
81
82CallbackBase<CopyMode::Copyable>&
tzik9edfd1de2016-07-20 12:56:4583CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default;
tzik77d411392016-03-09 09:47:0384
85template class CallbackBase<CopyMode::MoveOnly>;
86template class CallbackBase<CopyMode::Copyable>;
87
[email protected]59eff912011-02-18 23:29:3188} // namespace internal
[email protected]0f450362012-06-09 02:11:0189} // namespace base