[email protected] | 0f45036 | 2012-06-09 02:11:01 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 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 | |||||
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 7 | #include "base/logging.h" |
8 | |||||
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 9 | namespace base { |
10 | namespace internal { | ||||
11 | |||||
tzik | f1b37772 | 2016-09-08 06:58:07 | [diff] [blame] | 12 | BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 13 | void (*destructor)(BindStateBase*), |
14 | bool (*is_cancelled)(const BindStateBase*)) | ||||
tzik | f1b37772 | 2016-09-08 06:58:07 | [diff] [blame] | 15 | : polymorphic_invoke_(polymorphic_invoke), |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 16 | ref_count_(0), |
17 | destructor_(destructor), | ||||
18 | is_cancelled_(is_cancelled) {} | ||||
tzik | f1b37772 | 2016-09-08 06:58:07 | [diff] [blame] | 19 | |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 20 | void BindStateBase::AddRef() { |
21 | AtomicRefCountInc(&ref_count_); | ||||
22 | } | ||||
23 | |||||
24 | void BindStateBase::Release() { | ||||
25 | if (!AtomicRefCountDec(&ref_count_)) | ||||
26 | destructor_(this); | ||||
27 | } | ||||
28 | |||||
tzik | 9edfd1de | 2016-07-20 12:56:45 | [diff] [blame] | 29 | CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default; |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 30 | |
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 31 | CallbackBase<CopyMode::MoveOnly>& |
tzik | 9edfd1de | 2016-07-20 12:56:45 | [diff] [blame] | 32 | CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default; |
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 33 | |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame^] | 34 | CallbackBase<CopyMode::MoveOnly>::CallbackBase( |
35 | const CallbackBase<CopyMode::Copyable>& c) | ||||
36 | : bind_state_(c.bind_state_) {} | ||||
37 | |||||
38 | CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=( | ||||
39 | const CallbackBase<CopyMode::Copyable>& c) { | ||||
40 | bind_state_ = c.bind_state_; | ||||
41 | return *this; | ||||
42 | } | ||||
43 | |||||
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 44 | void CallbackBase<CopyMode::MoveOnly>::Reset() { |
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 45 | // 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 | |||||
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 50 | bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const { |
51 | DCHECK(bind_state_); | ||||
52 | return bind_state_->IsCancelled(); | ||||
53 | } | ||||
54 | |||||
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 55 | bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal( |
56 | const CallbackBase& other) const { | ||||
tzik | 1886c27 | 2016-09-08 05:45:38 | [diff] [blame] | 57 | return bind_state_ == other.bind_state_; |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 58 | } |
59 | |||||
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 60 | CallbackBase<CopyMode::MoveOnly>::CallbackBase( |
61 | BindStateBase* bind_state) | ||||
62 | : bind_state_(bind_state) { | ||||
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 63 | DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1); |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 64 | } |
65 | |||||
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 66 | CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {} |
67 | |||||
68 | CallbackBase<CopyMode::Copyable>::CallbackBase( | ||||
69 | const CallbackBase& c) | ||||
70 | : CallbackBase<CopyMode::MoveOnly>(nullptr) { | ||||
71 | bind_state_ = c.bind_state_; | ||||
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 72 | } |
73 | |||||
tzik | 9edfd1de | 2016-07-20 12:56:45 | [diff] [blame] | 74 | CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default; |
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 75 | |
76 | CallbackBase<CopyMode::Copyable>& | ||||
77 | CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) { | ||||
78 | bind_state_ = c.bind_state_; | ||||
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 79 | return *this; |
80 | } | ||||
81 | |||||
82 | CallbackBase<CopyMode::Copyable>& | ||||
tzik | 9edfd1de | 2016-07-20 12:56:45 | [diff] [blame] | 83 | CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default; |
tzik | 77d41139 | 2016-03-09 09:47:03 | [diff] [blame] | 84 | |
85 | template class CallbackBase<CopyMode::MoveOnly>; | ||||
86 | template class CallbackBase<CopyMode::Copyable>; | ||||
87 | |||||
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 88 | } // namespace internal |
[email protected] | 0f45036 | 2012-06-09 02:11:01 | [diff] [blame] | 89 | } // namespace base |