Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: base/callback_internal.cc

Issue 1699773002: Extend base::Callback to have a move-only variant (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 void BindStateBase::AddRef() { 12 void BindStateBase::AddRef() {
13 AtomicRefCountInc(&ref_count_); 13 AtomicRefCountInc(&ref_count_);
14 } 14 }
15 15
16 void BindStateBase::Release() { 16 void BindStateBase::Release() {
17 if (!AtomicRefCountDec(&ref_count_)) 17 if (!AtomicRefCountDec(&ref_count_))
18 destructor_(this); 18 destructor_(this);
19 } 19 }
20 20
21 CallbackBase::CallbackBase(const CallbackBase& c) = default; 21 MoveOnlyCallbackBase::MoveOnlyCallbackBase(MoveOnlyCallbackBase&& c)
22 CallbackBase& CallbackBase::operator=(const CallbackBase& c) = default; 22 : bind_state_(std::move(c.bind_state_)),
23 polymorphic_invoke_(c.polymorphic_invoke_) {
24 c.polymorphic_invoke_ = nullptr;
25 }
23 26
24 void CallbackBase::Reset() { 27 MoveOnlyCallbackBase& MoveOnlyCallbackBase::operator=(
25 polymorphic_invoke_ = NULL; 28 MoveOnlyCallbackBase&& c) {
29 bind_state_ = std::move(c.bind_state_);
30 polymorphic_invoke_ = c.polymorphic_invoke_;
31 c.polymorphic_invoke_ = nullptr;
32 return *this;
33 }
34
35 void MoveOnlyCallbackBase::Reset() {
36 polymorphic_invoke_ = nullptr;
26 // NULL the bind_state_ last, since it may be holding the last ref to whatever 37 // NULL the bind_state_ last, since it may be holding the last ref to whatever
27 // object owns us, and we may be deleted after that. 38 // object owns us, and we may be deleted after that.
28 bind_state_ = NULL; 39 bind_state_ = nullptr;
29 } 40 }
30 41
31 bool CallbackBase::Equals(const CallbackBase& other) const { 42 bool MoveOnlyCallbackBase::EqualsInternal(
43 const MoveOnlyCallbackBase& other) const {
32 return bind_state_.get() == other.bind_state_.get() && 44 return bind_state_.get() == other.bind_state_.get() &&
33 polymorphic_invoke_ == other.polymorphic_invoke_; 45 polymorphic_invoke_ == other.polymorphic_invoke_;
34 } 46 }
35 47
36 CallbackBase::CallbackBase(BindStateBase* bind_state) 48 void MoveOnlyCallbackBase::set_bind_state(BindStateBase* bind_state) {
37 : bind_state_(bind_state), 49 bind_state_ = bind_state;
38 polymorphic_invoke_(NULL) {
39 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1); 50 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1);
40 } 51 }
41 52
42 CallbackBase::~CallbackBase() { 53 MoveOnlyCallbackBase::MoveOnlyCallbackBase() {}
54 MoveOnlyCallbackBase::~MoveOnlyCallbackBase() {}
55
56 CopyableCallbackBase::CopyableCallbackBase(CopyableCallbackBase&& c)
57 : MoveOnlyCallbackBase(std::move(c)) {}
58
59 CopyableCallbackBase& CopyableCallbackBase::operator=(
60 CopyableCallbackBase&& c) {
61 *static_cast<MoveOnlyCallbackBase*>(this) = std::move(c);
62 return *this;
63 }
64
65 CopyableCallbackBase::CopyableCallbackBase(const CopyableCallbackBase& c) {
66 bind_state_ = c.bind_state_;
67 polymorphic_invoke_ = c.polymorphic_invoke_;
68 }
69
70 CopyableCallbackBase& CopyableCallbackBase::operator=(
71 const CopyableCallbackBase& c) {
72 bind_state_ = c.bind_state_;
73 polymorphic_invoke_ = c.polymorphic_invoke_;
74 return *this;
43 } 75 }
44 76
45 } // namespace internal 77 } // namespace internal
46 } // namespace base 78 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698