OLD | NEW |
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 <memory> |
| 9 |
8 #include "base/bind.h" | 10 #include "base/bind.h" |
9 #include "base/location.h" | 11 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
14 | 15 |
15 // This is a helper utility for base::Bind()ing callbacks to the current | 16 // This is a helper utility for base::Bind()ing callbacks to the current |
16 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a | 17 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a |
17 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that | 18 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that |
18 // when |b| executes the callback, it does so on |a|'s current MessageLoop. | 19 // when |b| executes the callback, it does so on |a|'s current MessageLoop. |
19 // | 20 // |
20 // Typical usage: request to be called back on the current thread: | 21 // Typical usage: request to be called back on the current thread: |
21 // other->StartAsyncProcessAndCallMeBack( | 22 // other->StartAsyncProcessAndCallMeBack( |
22 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this))); | 23 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this))); |
23 // | 24 // |
24 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant | 25 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant |
25 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies | 26 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies |
26 // of its arguments, and thus can't be used with arrays. | 27 // of its arguments, and thus can't be used with arrays. |
27 | 28 |
28 namespace media { | 29 namespace media { |
29 | 30 |
30 // Mimic base::internal::CallbackForward, replacing std::move(p) with | 31 // Mimic base::internal::CallbackForward, replacing std::move(p) with |
31 // base::Passed(&p) to account for the extra layer of indirection. | 32 // base::Passed(&p) to account for the extra layer of indirection. |
32 namespace internal { | 33 namespace internal { |
33 template <typename T> | 34 template <typename T> |
34 T& TrampolineForward(T& t) { return t; } | 35 T& TrampolineForward(T& t) { return t; } |
35 | 36 |
36 template <typename T, typename R> | 37 template <typename T, typename R> |
37 base::internal::PassedWrapper<scoped_ptr<T, R> > TrampolineForward( | 38 base::internal::PassedWrapper<std::unique_ptr<T, R>> TrampolineForward( |
38 scoped_ptr<T, R>& p) { return base::Passed(&p); } | 39 std::unique_ptr<T, R>& p) { |
| 40 return base::Passed(&p); |
| 41 } |
39 | 42 |
40 template <typename T> | 43 template <typename T> |
41 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( | 44 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( |
42 ScopedVector<T>& p) { return base::Passed(&p); } | 45 ScopedVector<T>& p) { return base::Passed(&p); } |
43 | 46 |
44 // First, tell the compiler TrampolineHelper is a struct template with one | 47 // First, tell the compiler TrampolineHelper is a struct template with one |
45 // type parameter. Then define specializations where the type is a function | 48 // type parameter. Then define specializations where the type is a function |
46 // returning void and taking zero or more arguments. | 49 // returning void and taking zero or more arguments. |
47 template <typename Sig> struct TrampolineHelper; | 50 template <typename Sig> struct TrampolineHelper; |
48 | 51 |
(...skipping 13 matching lines...) Expand all Loading... |
62 template<typename T> | 65 template<typename T> |
63 static base::Callback<T> BindToCurrentLoop( | 66 static base::Callback<T> BindToCurrentLoop( |
64 const base::Callback<T>& cb) { | 67 const base::Callback<T>& cb) { |
65 return base::Bind(&internal::TrampolineHelper<T>::Run, | 68 return base::Bind(&internal::TrampolineHelper<T>::Run, |
66 base::ThreadTaskRunnerHandle::Get(), cb); | 69 base::ThreadTaskRunnerHandle::Get(), cb); |
67 } | 70 } |
68 | 71 |
69 } // namespace media | 72 } // namespace media |
70 | 73 |
71 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ | 74 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ |
OLD | NEW |