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 CallbackBase::CallbackBase(const CallbackBase& c) = default; | 12 void BindStateBase::AddRef() { |
13 CallbackBase& CallbackBase::operator=(const CallbackBase& c) = default; | 13 AtomicRefCountInc(&ref_count_); |
| 14 } |
| 15 |
| 16 void BindStateBase::Release() { |
| 17 if (!AtomicRefCountDec(&ref_count_)) |
| 18 destructor_(this); |
| 19 } |
14 | 20 |
15 void CallbackBase::Reset() { | 21 void CallbackBase::Reset() { |
16 polymorphic_invoke_ = NULL; | 22 polymorphic_invoke_ = nullptr; |
17 // NULL the bind_state_ last, since it may be holding the last ref to whatever | 23 // NULL the bind_state_ last, since it may be holding the last ref to whatever |
18 // object owns us, and we may be deleted after that. | 24 // object owns us, and we may be deleted after that. |
19 bind_state_ = NULL; | 25 if (bind_state_) { |
| 26 bind_state_->Release(); |
| 27 bind_state_ = nullptr; |
| 28 } |
20 } | 29 } |
21 | 30 |
22 bool CallbackBase::Equals(const CallbackBase& other) const { | 31 bool CallbackBase::Equals(const CallbackBase& other) const { |
23 return bind_state_.get() == other.bind_state_.get() && | 32 return bind_state_ == other.bind_state_ && |
24 polymorphic_invoke_ == other.polymorphic_invoke_; | 33 polymorphic_invoke_ == other.polymorphic_invoke_; |
25 } | 34 } |
26 | 35 |
| 36 CallbackBase::CallbackBase() |
| 37 : bind_state_(nullptr), polymorphic_invoke_(nullptr) { |
| 38 } |
| 39 |
27 CallbackBase::CallbackBase(BindStateBase* bind_state) | 40 CallbackBase::CallbackBase(BindStateBase* bind_state) |
28 : bind_state_(bind_state), | 41 : bind_state_(bind_state) { |
29 polymorphic_invoke_(NULL) { | 42 bind_state_->AddRef(); |
30 DCHECK(!bind_state_.get() || bind_state_->HasOneRef()); | 43 DCHECK_EQ(1, bind_state_->ref_count_); |
| 44 } |
| 45 |
| 46 CallbackBase::CallbackBase(const CallbackBase& c) |
| 47 : bind_state_(c.bind_state_), polymorphic_invoke_(c.polymorphic_invoke_) { |
| 48 if (bind_state_) |
| 49 bind_state_->AddRef(); |
| 50 } |
| 51 |
| 52 CallbackBase& CallbackBase::operator=(const CallbackBase& c) { |
| 53 // Increment first to handle self-assignment correctly. |
| 54 if (c.bind_state_) |
| 55 c.bind_state_->AddRef(); |
| 56 if (bind_state_) |
| 57 bind_state_->Release(); |
| 58 |
| 59 bind_state_ = c.bind_state_; |
| 60 polymorphic_invoke_ = c.polymorphic_invoke_; |
| 61 return *this; |
31 } | 62 } |
32 | 63 |
33 CallbackBase::~CallbackBase() { | 64 CallbackBase::~CallbackBase() { |
| 65 if (bind_state_) |
| 66 bind_state_->Release(); |
34 } | 67 } |
35 | 68 |
36 } // namespace internal | 69 } // namespace internal |
37 } // namespace base | 70 } // namespace base |
OLD | NEW |