OLD | NEW |
| (Empty) |
1 $$ This is a pump file for generating file templates. Pump is a python | |
2 $$ script that is part of the Google Test suite of utilities. Description | |
3 $$ can be found here: | |
4 $$ | |
5 $$ http://code.google.com/p/googletest/wiki/PumpManual | |
6 $$ | |
7 $$ See comment for MAX_ARITY in base/bind.h.pump. | |
8 $var MAX_ARITY = 7 | |
9 | |
10 // Copyright 2014 The Chromium Authors. All rights reserved. | |
11 // Use of this source code is governed by a BSD-style license that can be | |
12 // found in the LICENSE file. | |
13 | |
14 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | |
15 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | |
16 | |
17 #include "base/bind.h" | |
18 #include "base/location.h" | |
19 #include "base/logging.h" | |
20 #include "base/sequenced_task_runner.h" | |
21 #include "base/thread_task_runner_handle.h" | |
22 | |
23 // TODO(tzik): Merge this file to media/base/bind_to_current_loop.h. | |
24 | |
25 namespace sync_file_system { | |
26 namespace drive_backend { | |
27 | |
28 namespace internal { | |
29 | |
30 template <typename T> | |
31 typename base::enable_if< | |
32 base::internal::IsMoveOnlyType<T>::value, | |
33 base::internal::PassedWrapper<T> >::type | |
34 RebindForward(T& t) { | |
35 return base::Passed(&t); | |
36 } | |
37 | |
38 template <typename T> | |
39 typename base::enable_if< | |
40 !base::internal::IsMoveOnlyType<T>::value, | |
41 T&>::type | |
42 RebindForward(T& t) { | |
43 return t; | |
44 } | |
45 | |
46 template <typename T> | |
47 class CallbackHolder { | |
48 public: | |
49 CallbackHolder(base::SequencedTaskRunner* task_runner, | |
50 const tracked_objects::Location& from_here, | |
51 const base::Callback<T>& callback) | |
52 : task_runner_(task_runner), | |
53 from_here_(from_here), | |
54 callback_(new base::Callback<T>(callback)) { | |
55 DCHECK(task_runner_); | |
56 } | |
57 | |
58 ~CallbackHolder() { | |
59 base::Callback<T>* callback = callback_.release(); | |
60 if (!task_runner_->DeleteSoon(from_here_, callback)) | |
61 delete callback; | |
62 } | |
63 | |
64 base::SequencedTaskRunner* task_runner() const { return task_runner_; } | |
65 const tracked_objects::Location& from_here() const { return from_here_; } | |
66 const base::Callback<T>& callback() const { return *callback_; } | |
67 | |
68 private: | |
69 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
70 const tracked_objects::Location from_here_; | |
71 scoped_ptr<base::Callback<T> > callback_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(CallbackHolder); | |
74 }; | |
75 | |
76 template <typename> | |
77 struct RelayToTaskRunnerHelper; | |
78 | |
79 $range ARITY 0..MAX_ARITY | |
80 $for ARITY [[ | |
81 $range ARG 1..ARITY | |
82 | |
83 template <$for ARG , [[typename A$(ARG)]]> | |
84 struct RelayToTaskRunnerHelper<void($for ARG , [[A$(ARG)]])> { | |
85 static void Run(CallbackHolder<void($for ARG , [[A$(ARG)]])>* holder | |
86 $if ARITY != 0 [[, ]] | |
87 $for ARG , [[A$(ARG) a$(ARG)]] | |
88 ) { | |
89 holder->task_runner()->PostTask( | |
90 holder->from_here(), base::Bind(holder->callback() | |
91 $if ARITY != 0 [[, ]] | |
92 $for ARG , [[RebindForward(a$(ARG))]])); | |
93 } | |
94 }; | |
95 | |
96 ]] $$ for ARITY | |
97 | |
98 } // namespace internal | |
99 | |
100 template <typename T> | |
101 base::Callback<T> RelayCallbackToTaskRunner( | |
102 base::SequencedTaskRunner* task_runner, | |
103 const tracked_objects::Location& from_here, | |
104 const base::Callback<T>& callback) { | |
105 DCHECK(task_runner->RunsTasksOnCurrentThread()); | |
106 | |
107 if (callback.is_null()) | |
108 return base::Callback<T>(); | |
109 | |
110 return base::Bind(&internal::RelayToTaskRunnerHelper<T>::Run, | |
111 base::Owned(new internal::CallbackHolder<T>( | |
112 task_runner, from_here, callback))); | |
113 } | |
114 | |
115 template <typename T> | |
116 base::Callback<T> RelayCallbackToCurrentThread( | |
117 const tracked_objects::Location& from_here, | |
118 const base::Callback<T>& callback) { | |
119 return RelayCallbackToTaskRunner( | |
120 base::ThreadTaskRunnerHandle::Get(), | |
121 from_here, callback); | |
122 } | |
123 | |
124 } // namespace drive_backend | |
125 } // namespace sync_file_system | |
126 | |
127 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | |
OLD | NEW |