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

Side by Side Diff: base/bind_to_task_runner.h

Issue 1553593002: Add base::BindToTaskRunner and BindToCurrentThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Created 4 years, 11 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
« no previous file with comments | « base/base.gyp ('k') | base/bind_to_task_runner_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_BIND_TO_TASK_RUNNER_H_
6 #define BASE_BIND_TO_TASK_RUNNER_H_
7
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/callback.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/task_runner.h"
16 #include "base/thread_task_runner_handle.h"
17
18 // This is a helper utility for Bind()ing callbacks to a given TaskRunner.
19 // The typical use is when |a| (of class |A|) wants to hand a callback such as
20 // base::Bind(&A::AMethod, a) to |b|, but needs to ensure that when |b| executes
21 // the callback, it does so on a specific TaskRunner (for example, |a|'s current
22 // MessageLoop).
23 //
24 // Typical usage: request to be called back on the current thread:
25 // other->StartAsyncProcessAndCallMeBack(
26 // base::BindToTaskRunner(my_task_runner_, base::Bind(&MyClass::MyMethod,
27 // this)));
28 //
29 // Note that like base::Bind(), BindToTaskRunner() can't bind non-constant
30 // references, and that *unlike* base::Bind(), BindToTaskRunner() makes copies
31 // of its arguments, and thus can't be used with arrays. Note that the callback
32 // is always posted to the target TaskRunner.
33 //
34 // As a convenience, you can use base::BindToCurrentThread() to bind to the
35 // TaskRunner for the current thread (ie, base::ThreadTaskRunnerHandle::Get()).
36
37 namespace base {
38 namespace internal {
39
40 template <typename T>
41 T& TrampolineForward(T& t) {
42 return t;
43 }
44
45 template <typename T, typename R>
46 PassedWrapper<scoped_ptr<T, R>> TrampolineForward(scoped_ptr<T, R>& p) {
47 return Passed(&p);
48 }
49
50 template <typename T>
51 PassedWrapper<ScopedVector<T>> TrampolineForward(ScopedVector<T>& p) {
52 return Passed(&p);
53 }
54
55 template <typename Sig>
56 struct BindToTaskRunnerTrampoline;
57
58 template <typename... Args>
59 struct BindToTaskRunnerTrampoline<void(Args...)> {
60 static void Run(const scoped_refptr<TaskRunner>& task_runner,
61 const Callback<void(Args...)>& cb,
62 Args... args) {
63 task_runner->PostTask(FROM_HERE, Bind(cb, TrampolineForward(args)...));
64 }
65 };
66
67 } // namespace internal
68
69 template <typename T>
70 Callback<T> BindToTaskRunner(const scoped_refptr<TaskRunner>& task_runner,
71 const Callback<T>& cb) {
72 return Bind(&internal::BindToTaskRunnerTrampoline<T>::Run, task_runner, cb);
73 }
74
75 template <typename T>
76 Callback<T> BindToCurrentThread(const Callback<T>& cb) {
77 return BindToTaskRunner(ThreadTaskRunnerHandle::Get(), cb);
78 }
79
80 } // namespace base
81
82 #endif // BASE_BIND_TO_TASK_RUNNER_H_
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/bind_to_task_runner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698