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

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: fix android build Created 4 years, 9 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 BASE_EXPORT CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c)
dcheng 2016/03/02 08:02:36 Huh. It's surprising to see _EXPORT macros in a .c
tzik 2016/03/02 13:09:52 Each clang, gcc and cl.exe behave differently arou
dcheng 2016/03/02 17:04:56 I /think/ you should only need this on the declara
Nico 2016/03/09 03:24:53 See also ipc/export_template.h for some text (and
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 BASE_EXPORT CallbackBase<CopyMode::MoveOnly>&
25 polymorphic_invoke_ = NULL; 28 CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& 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 BASE_EXPORT void CallbackBase<CopyMode::MoveOnly>::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 BASE_EXPORT bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
43 const CallbackBase& 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 BASE_EXPORT CallbackBase<CopyMode::MoveOnly>::CallbackBase(
37 : bind_state_(bind_state), 49 BindStateBase* bind_state)
38 polymorphic_invoke_(NULL) { 50 : bind_state_(bind_state) {
39 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1); 51 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1);
40 } 52 }
41 53
42 CallbackBase::~CallbackBase() { 54 BASE_EXPORT CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
55
56 BASE_EXPORT CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c)
57 : CallbackBase<CopyMode::MoveOnly>(std::move(c)) {}
58
59 BASE_EXPORT CallbackBase<CopyMode::Copyable>&
60 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) {
61 *static_cast<CallbackBase<CopyMode::MoveOnly>*>(this) = std::move(c);
62 return *this;
43 } 63 }
dcheng 2016/03/02 08:02:36 Can we default the move operators? It seems like
tzik 2016/03/02 08:13:14 We are still using MSVC2013, which does not suppor
44 64
65 BASE_EXPORT CallbackBase<CopyMode::Copyable>::CallbackBase(
66 const CallbackBase& c)
67 : CallbackBase<CopyMode::MoveOnly>(nullptr) {
68 bind_state_ = c.bind_state_;
69 polymorphic_invoke_ = c.polymorphic_invoke_;
70 }
71
72 BASE_EXPORT CallbackBase<CopyMode::Copyable>&
73 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
74 bind_state_ = c.bind_state_;
75 polymorphic_invoke_ = c.polymorphic_invoke_;
76 return *this;
77 }
78
79 template class CallbackBase<CopyMode::MoveOnly>;
80 template class CallbackBase<CopyMode::Copyable>;
81
45 } // namespace internal 82 } // namespace internal
46 } // namespace base 83 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698