OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/callback_internal.h" | 5 #include "base/callback_internal.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 namespace internal { | 10 namespace internal { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 bool ReturnFalse(const BindStateBase*) { | 14 bool ReturnFalse(const BindStateBase*) { |
15 return false; | 15 return false; |
16 } | 16 } |
17 | 17 |
18 } // namespace | 18 } // namespace |
19 | 19 |
| 20 void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) { |
| 21 bind_state->destructor_(bind_state); |
| 22 } |
| 23 |
20 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, | 24 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, |
21 void (*destructor)(const BindStateBase*)) | 25 void (*destructor)(const BindStateBase*)) |
22 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) { | 26 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) { |
23 } | 27 } |
24 | 28 |
25 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, | 29 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, |
26 void (*destructor)(const BindStateBase*), | 30 void (*destructor)(const BindStateBase*), |
27 bool (*is_cancelled)(const BindStateBase*)) | 31 bool (*is_cancelled)(const BindStateBase*)) |
28 : polymorphic_invoke_(polymorphic_invoke), | 32 : polymorphic_invoke_(polymorphic_invoke), |
29 ref_count_(0), | |
30 destructor_(destructor), | 33 destructor_(destructor), |
31 is_cancelled_(is_cancelled) {} | 34 is_cancelled_(is_cancelled) {} |
32 | 35 |
33 void BindStateBase::AddRef() const { | |
34 AtomicRefCountInc(&ref_count_); | |
35 } | |
36 | |
37 void BindStateBase::Release() const { | |
38 if (!AtomicRefCountDec(&ref_count_)) | |
39 destructor_(this); | |
40 } | |
41 | |
42 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default; | 36 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default; |
43 | 37 |
44 CallbackBase<CopyMode::MoveOnly>& | 38 CallbackBase<CopyMode::MoveOnly>& |
45 CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default; | 39 CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default; |
46 | 40 |
47 CallbackBase<CopyMode::MoveOnly>::CallbackBase( | 41 CallbackBase<CopyMode::MoveOnly>::CallbackBase( |
48 const CallbackBase<CopyMode::Copyable>& c) | 42 const CallbackBase<CopyMode::Copyable>& c) |
49 : bind_state_(c.bind_state_) {} | 43 : bind_state_(c.bind_state_) {} |
50 | 44 |
51 CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=( | 45 CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=( |
(...skipping 24 matching lines...) Expand all Loading... |
76 } | 70 } |
77 | 71 |
78 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal( | 72 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal( |
79 const CallbackBase& other) const { | 73 const CallbackBase& other) const { |
80 return bind_state_ == other.bind_state_; | 74 return bind_state_ == other.bind_state_; |
81 } | 75 } |
82 | 76 |
83 CallbackBase<CopyMode::MoveOnly>::CallbackBase( | 77 CallbackBase<CopyMode::MoveOnly>::CallbackBase( |
84 BindStateBase* bind_state) | 78 BindStateBase* bind_state) |
85 : bind_state_(bind_state) { | 79 : bind_state_(bind_state) { |
86 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1); | 80 DCHECK(!bind_state_.get() || bind_state_->HasOneRef()); |
87 } | 81 } |
88 | 82 |
89 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {} | 83 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {} |
90 | 84 |
91 CallbackBase<CopyMode::Copyable>::CallbackBase( | 85 CallbackBase<CopyMode::Copyable>::CallbackBase( |
92 const CallbackBase& c) | 86 const CallbackBase& c) |
93 : CallbackBase<CopyMode::MoveOnly>(nullptr) { | 87 : CallbackBase<CopyMode::MoveOnly>(nullptr) { |
94 bind_state_ = c.bind_state_; | 88 bind_state_ = c.bind_state_; |
95 } | 89 } |
96 | 90 |
97 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default; | 91 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default; |
98 | 92 |
99 CallbackBase<CopyMode::Copyable>& | 93 CallbackBase<CopyMode::Copyable>& |
100 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) { | 94 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) { |
101 bind_state_ = c.bind_state_; | 95 bind_state_ = c.bind_state_; |
102 return *this; | 96 return *this; |
103 } | 97 } |
104 | 98 |
105 CallbackBase<CopyMode::Copyable>& | 99 CallbackBase<CopyMode::Copyable>& |
106 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default; | 100 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default; |
107 | 101 |
108 template class CallbackBase<CopyMode::MoveOnly>; | 102 template class CallbackBase<CopyMode::MoveOnly>; |
109 template class CallbackBase<CopyMode::Copyable>; | 103 template class CallbackBase<CopyMode::Copyable>; |
110 | 104 |
111 } // namespace internal | 105 } // namespace internal |
112 } // namespace base | 106 } // namespace base |
OLD | NEW |