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

Side by Side Diff: media/base/bind_to_current_loop.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/bind_to_task_runner_unittest.cc ('k') | media/base/bind_to_current_loop_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
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 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 #ifndef MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 5 #ifndef MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
7 7
8 #include "base/bind.h" 8 #include "base/bind_to_task_runner.h"
9 #include "base/location.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 9
14 // This is a helper utility for base::Bind()ing callbacks to the current 10 // DEPRECATED. New code should use base::BindToCurrentThread() instead.
15 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a
16 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that
17 // when |b| executes the callback, it does so on |a|'s current MessageLoop.
18 //
19 // Typical usage: request to be called back on the current thread:
20 // other->StartAsyncProcessAndCallMeBack(
21 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this)));
22 //
23 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant
24 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies
25 // of its arguments, and thus can't be used with arrays.
26 11
27 namespace media { 12 namespace media {
28 13
29 // Mimic base::internal::CallbackForward, replacing p.Pass() with
30 // base::Passed(&p) to account for the extra layer of indirection.
31 namespace internal {
32 template <typename T> 14 template <typename T>
33 T& TrampolineForward(T& t) { return t; } 15 base::Callback<T> BindToCurrentLoop(const base::Callback<T>& cb) {
34 16 return base::BindToCurrentThread(cb);
35 template <typename T, typename R>
36 base::internal::PassedWrapper<scoped_ptr<T, R> > TrampolineForward(
37 scoped_ptr<T, R>& p) { return base::Passed(&p); }
38
39 template <typename T>
40 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
41 ScopedVector<T>& p) { return base::Passed(&p); }
42
43 // First, tell the compiler TrampolineHelper is a struct template with one
44 // type parameter. Then define specializations where the type is a function
45 // returning void and taking zero or more arguments.
46 template <typename Sig> struct TrampolineHelper;
47
48 template <typename... Args>
49 struct TrampolineHelper<void(Args...)> {
50 static void Run(
51 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
52 const base::Callback<void(Args...)>& cb,
53 Args... args) {
54 task_runner->PostTask(FROM_HERE,
55 base::Bind(cb, TrampolineForward(args)...));
56 }
57 };
58
59 } // namespace internal
60
61 template<typename T>
62 static base::Callback<T> BindToCurrentLoop(
63 const base::Callback<T>& cb) {
64 return base::Bind(&internal::TrampolineHelper<T>::Run,
65 base::ThreadTaskRunnerHandle::Get(), cb);
66 } 17 }
67 18
68 } // namespace media 19 } // namespace media
69 20
70 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 21 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
OLDNEW
« no previous file with comments | « base/bind_to_task_runner_unittest.cc ('k') | media/base/bind_to_current_loop_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698