forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.h
67 lines (54 loc) · 2.44 KB
/
callback.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_CALLBACK_H_
#define BASE_CALLBACK_H_
#include "base/callback_forward.h"
#include "base/callback_internal.h"
// NOTE: Header files that do not require the full definition of Callback or
// Closure should #include "base/callback_forward.h" instead of this file.
// -----------------------------------------------------------------------------
// Usage documentation
// -----------------------------------------------------------------------------
//
// See //docs/callback.md for documentation.
namespace base {
template <typename R, typename... Args, internal::CopyMode copy_mode>
class Callback<R(Args...), copy_mode>
: public internal::CallbackBase<copy_mode> {
private:
using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...);
public:
// MSVC 2013 doesn't support Type Alias of function types.
// Revisit this after we update it to newer version.
typedef R RunType(Args...);
Callback() : internal::CallbackBase<copy_mode>(nullptr) {}
Callback(internal::BindStateBase* bind_state, PolymorphicInvoke invoke_func)
: internal::CallbackBase<copy_mode>(bind_state) {
using InvokeFuncStorage =
typename internal::CallbackBase<copy_mode>::InvokeFuncStorage;
this->polymorphic_invoke_ =
reinterpret_cast<InvokeFuncStorage>(invoke_func);
}
bool Equals(const Callback& other) const {
return this->EqualsInternal(other);
}
// Run() makes an extra copy compared to directly calling the bound function
// if an argument is passed-by-value and is copyable-but-not-movable:
// i.e. below copies CopyableNonMovableType twice.
// void F(CopyableNonMovableType) {}
// Bind(&F).Run(CopyableNonMovableType());
//
// We can not fully apply Perfect Forwarding idiom to the callchain from
// Callback::Run() to the target function. Perfect Forwarding requires
// knowing how the caller will pass the arguments. However, the signature of
// InvokerType::Run() needs to be fixed in the callback constructor, so Run()
// cannot template its arguments based on how it's called.
R Run(Args... args) const {
PolymorphicInvoke f =
reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke_);
return f(this->bind_state_.get(), std::forward<Args>(args)...);
}
};
} // namespace base
#endif // BASE_CALLBACK_H_