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

Side by Side Diff: chromecast/base/bind_to_task_runner.h

Issue 1551843002: [Chromecast] Add BindToTaskRunner and BindToCurrentThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Correct copyright notice 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 | « chromecast/base/BUILD.gn ('k') | chromecast/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 CHROMECAST_BASE_BIND_TO_TASK_RUNNER_H_
6 #define CHROMECAST_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 // BindToTaskRunner(my_task_runner_, base::Bind(&MyClass::MyMethod, this)));
27 //
28 // Note that like base::Bind(), BindToTaskRunner() can't bind non-constant
29 // references, and that *unlike* base::Bind(), BindToTaskRunner() makes copies
30 // of its arguments, and thus can't be used with arrays. Note that the callback
31 // is always posted to the target TaskRunner.
32 //
33 // As a convenience, you can use BindToCurrentThread() to bind to the
34 // TaskRunner for the current thread (ie, base::ThreadTaskRunnerHandle::Get()).
35
36 namespace chromecast {
37 namespace bind_helpers {
38
39 template <typename T>
40 T& TrampolineForward(T& t) {
41 return t;
42 }
43
44 template <typename T, typename R>
45 base::internal::PassedWrapper<scoped_ptr<T, R>> TrampolineForward(
46 scoped_ptr<T, R>& p) {
47 return base::Passed(&p);
48 }
49
50 template <typename T>
51 base::internal::PassedWrapper<ScopedVector<T>> TrampolineForward(
52 ScopedVector<T>& p) {
53 return base::Passed(&p);
54 }
55
56 template <typename Sig>
57 struct BindToTaskRunnerTrampoline;
58
59 template <typename... Args>
60 struct BindToTaskRunnerTrampoline<void(Args...)> {
61 static void Run(const scoped_refptr<base::TaskRunner>& task_runner,
62 const base::Callback<void(Args...)>& cb,
63 Args... args) {
64 task_runner->PostTask(FROM_HERE,
65 base::Bind(cb, TrampolineForward(args)...));
66 }
67 };
68
69 } // namespace bind_helpers
70
71 template <typename T>
72 base::Callback<T> BindToTaskRunner(
73 const scoped_refptr<base::TaskRunner>& task_runner,
74 const base::Callback<T>& cb) {
75 return base::Bind(&bind_helpers::BindToTaskRunnerTrampoline<T>::Run,
76 task_runner, cb);
77 }
78
79 template <typename T>
80 base::Callback<T> BindToCurrentThread(const base::Callback<T>& cb) {
81 return BindToTaskRunner(base::ThreadTaskRunnerHandle::Get(), cb);
82 }
83
84 } // namespace chromecast
85
86 #endif // CHROMECAST_BASE_BIND_TO_TASK_RUNNER_H_
OLDNEW
« no previous file with comments | « chromecast/base/BUILD.gn ('k') | chromecast/base/bind_to_task_runner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698