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

Side by Side Diff: base/bind_internal.h.pump

Issue 754433003: Update from https://crrev.com/305340 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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_internal.h ('k') | base/callback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 $$ This is a pump file for generating file templates. Pump is a python 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 2 $$ script that is part of the Google Test suite of utilities. Description
3 $$ can be found here: 3 $$ can be found here:
4 $$ 4 $$
5 $$ http://code.google.com/p/googletest/wiki/PumpManual 5 $$ http://code.google.com/p/googletest/wiki/PumpManual
6 $$ 6 $$
7 7
8 $$ See comment for MAX_ARITY in base/bind.h.pump. 8 $$ See comment for MAX_ARITY in base/bind.h.pump.
9 $var MAX_ARITY = 7 9 $var MAX_ARITY = 7
10 $range ARITY 0..MAX_ARITY 10 $range ARITY 0..MAX_ARITY
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 // (Bound)ArgsType -- A function type that is being (ab)used to store the 50 // (Bound)ArgsType -- A function type that is being (ab)used to store the
51 // types of set of arguments. The "return" type is always 51 // types of set of arguments. The "return" type is always
52 // void here. We use this hack so that we do not need 52 // void here. We use this hack so that we do not need
53 // a new type name for each arity of type. (eg., 53 // a new type name for each arity of type. (eg.,
54 // BindState1, BindState2). This makes forward 54 // BindState1, BindState2). This makes forward
55 // declarations and friending much much easier. 55 // declarations and friending much much easier.
56 // 56 //
57 // Types: 57 // Types:
58 // RunnableAdapter<> -- Wraps the various "function" pointer types into an 58 // RunnableAdapter<> -- Wraps the various "function" pointer types into an
59 // object that adheres to the Runnable interface. 59 // object that adheres to the Runnable interface.
60 // There are |3*ARITY| RunnableAdapter types.
61 // FunctionTraits<> -- Type traits that unwrap a function signature into a 60 // FunctionTraits<> -- Type traits that unwrap a function signature into a
62 // a set of easier to use typedefs. Used mainly for 61 // a set of easier to use typedefs. Used mainly for
63 // compile time asserts. 62 // compile time asserts.
64 // There are |ARITY| FunctionTraits types. 63 // There are |ARITY| FunctionTraits types.
65 // ForceVoidReturn<> -- Helper class for translating function signatures to 64 // ForceVoidReturn<> -- Helper class for translating function signatures to
66 // equivalent forms with a "void" return type. 65 // equivalent forms with a "void" return type.
67 // There are |ARITY| ForceVoidReturn types.
68 // FunctorTraits<> -- Type traits used determine the correct RunType and 66 // FunctorTraits<> -- Type traits used determine the correct RunType and
69 // RunnableType for a Functor. This is where function 67 // RunnableType for a Functor. This is where function
70 // signature adapters are applied. 68 // signature adapters are applied.
71 // There are |ARITY| ForceVoidReturn types.
72 // MakeRunnable<> -- Takes a Functor and returns an object in the Runnable 69 // MakeRunnable<> -- Takes a Functor and returns an object in the Runnable
73 // type class that represents the underlying Functor. 70 // type class that represents the underlying Functor.
74 // There are |O(1)| MakeRunnable types. 71 // There are |O(1)| MakeRunnable types.
75 // InvokeHelper<> -- Take a Runnable + arguments and actully invokes it. 72 // InvokeHelper<> -- Take a Runnable + arguments and actully invokes it.
76 // Handle the differing syntaxes needed for WeakPtr<> support , 73 // Handle the differing syntaxes needed for WeakPtr<> support ,
77 // and for ignoring return values. This is separate from 74 // and for ignoring return values. This is separate from
78 // Invoker to avoid creating multiple version of Invoker<> 75 // Invoker to avoid creating multiple version of Invoker<>
79 // which grows at O(n^2) with the arity. 76 // which grows at O(n^2) with the arity.
80 // There are |k*ARITY| InvokeHelper types.
81 // Invoker<> -- Unwraps the curried parameters and executes the Runnable. 77 // Invoker<> -- Unwraps the curried parameters and executes the Runnable.
82 // There are |(ARITY^2 + ARITY)/2| Invoketypes. 78 // There are |(ARITY^2 + ARITY)/2| Invoketypes.
83 // BindState<> -- Stores the curried parameters, and is the main entry point 79 // BindState<> -- Stores the curried parameters, and is the main entry point
84 // into the Bind() system, doing most of the type resolution. 80 // into the Bind() system, doing most of the type resolution.
85 // There are ARITY BindState types. 81 // There are ARITY BindState types.
86 82
87 // RunnableAdapter<> 83 // RunnableAdapter<>
88 // 84 //
89 // The RunnableAdapter<> templates provide a uniform interface for invoking 85 // The RunnableAdapter<> templates provide a uniform interface for invoking
90 // a function pointer, method pointer, or const method pointer. The adapter 86 // a function pointer, method pointer, or const method pointer. The adapter
91 // exposes a Run() method with an appropriate signature. Using this wrapper 87 // exposes a Run() method with an appropriate signature. Using this wrapper
92 // allows for writing code that supports all three pointer types without 88 // allows for writing code that supports all three pointer types without
93 // undue repetition. Without it, a lot of code would need to be repeated 3 89 // undue repetition. Without it, a lot of code would need to be repeated 3
94 // times. 90 // times.
95 // 91 //
96 // For method pointers and const method pointers the first argument to Run() 92 // For method pointers and const method pointers the first argument to Run()
97 // is considered to be the received of the method. This is similar to STL's 93 // is considered to be the received of the method. This is similar to STL's
98 // mem_fun(). 94 // mem_fun().
99 // 95 //
100 // This class also exposes a RunType typedef that is the function type of the 96 // This class also exposes a RunType typedef that is the function type of the
101 // Run() function. 97 // Run() function.
102 // 98 //
103 // If and only if the wrapper contains a method or const method pointer, an 99 // If and only if the wrapper contains a method or const method pointer, an
104 // IsMethod typedef is exposed. The existence of this typedef (NOT the value) 100 // IsMethod typedef is exposed. The existence of this typedef (NOT the value)
105 // marks that the wrapper should be considered a method wrapper. 101 // marks that the wrapper should be considered a method wrapper.
106 102
107 template <typename Functor> 103 template <typename Functor>
108 class RunnableAdapter; 104 class RunnableAdapter;
109 105
110 $for ARITY [[ 106 // Function.
111 $range ARG 1..ARITY 107 template <typename R, typename... Args>
108 class RunnableAdapter<R(*)(Args...)> {
109 public:
110 typedef R (RunType)(Args...);
112 111
113 // Function: Arity $(ARITY). 112 explicit RunnableAdapter(R(*function)(Args...))
114 template <typename R[[]]
115 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]>
116 class RunnableAdapter<R(*)($for ARG , [[A$(ARG)]])> {
117 public:
118 typedef R (RunType)($for ARG , [[A$(ARG)]]);
119
120 explicit RunnableAdapter(R(*function)($for ARG , [[A$(ARG)]]))
121 : function_(function) { 113 : function_(function) {
122 } 114 }
123 115
124 R Run($for ARG , [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)] ]) { 116 R Run(typename CallbackParamTraits<Args>::ForwardType... args) {
125 return function_($for ARG , [[CallbackForward(a$(ARG))]]); 117 return function_(CallbackForward(args)...);
126 } 118 }
127 119
128 private: 120 private:
129 R (*function_)($for ARG , [[A$(ARG)]]); 121 R (*function_)(Args...);
130 }; 122 };
131 123
132 // Method: Arity $(ARITY). 124 // Method.
133 template <typename R, typename T[[]] 125 template <typename R, typename T, typename... Args>
134 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> 126 class RunnableAdapter<R(T::*)(Args...)> {
135 class RunnableAdapter<R(T::*)($for ARG , [[A$(ARG)]])> {
136 public: 127 public:
137 typedef R (RunType)(T*[[]] 128 typedef R (RunType)(T*, Args...);
138 $if ARITY > 0[[, ]] $for ARG , [[A$(ARG)]]);
139 typedef true_type IsMethod; 129 typedef true_type IsMethod;
140 130
141 explicit RunnableAdapter(R(T::*method)($for ARG , [[A$(ARG)]])) 131 explicit RunnableAdapter(R(T::*method)(Args...))
142 : method_(method) { 132 : method_(method) {
143 } 133 }
144 134
145 R Run(T* object[[]] 135 R Run(T* object, typename CallbackParamTraits<Args>::ForwardType... args) {
146 $if ARITY > 0[[, ]] $for ARG, [[typename CallbackParamTraits<A$(ARG)>::ForwardT ype a$(ARG)]]) { 136 return (object->*method_)(CallbackForward(args)...);
147 return (object->*method_)($for ARG , [[CallbackForward(a$(ARG))]]);
148 } 137 }
149 138
150 private: 139 private:
151 R (T::*method_)($for ARG , [[A$(ARG)]]); 140 R (T::*method_)(Args...);
152 }; 141 };
153 142
154 // Const Method: Arity $(ARITY). 143 // Const Method.
155 template <typename R, typename T[[]] 144 template <typename R, typename T, typename... Args>
156 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> 145 class RunnableAdapter<R(T::*)(Args...) const> {
157 class RunnableAdapter<R(T::*)($for ARG , [[A$(ARG)]]) const> {
158 public: 146 public:
159 typedef R (RunType)(const T*[[]] 147 typedef R (RunType)(const T*, Args...);
160 $if ARITY > 0[[, ]] $for ARG , [[A$(ARG)]]);
161 typedef true_type IsMethod; 148 typedef true_type IsMethod;
162 149
163 explicit RunnableAdapter(R(T::*method)($for ARG , [[A$(ARG)]]) const) 150 explicit RunnableAdapter(R(T::*method)(Args...) const)
164 : method_(method) { 151 : method_(method) {
165 } 152 }
166 153
167 R Run(const T* object[[]] 154 R Run(const T* object,
168 $if ARITY > 0[[, ]] $for ARG, [[typename CallbackParamTraits<A$(ARG)>::ForwardT ype a$(ARG)]]) { 155 typename CallbackParamTraits<Args>::ForwardType... args) {
169 return (object->*method_)($for ARG , [[CallbackForward(a$(ARG))]]); 156 return (object->*method_)(CallbackForward(args)...);
170 } 157 }
171 158
172 private: 159 private:
173 R (T::*method_)($for ARG , [[A$(ARG)]]) const; 160 R (T::*method_)(Args...) const;
174 }; 161 };
175 162
176 ]] $$ for ARITY 163 // TODO(tzik): Remove FunctionTraits after we finish removing bind.pump.
177
178
179 // FunctionTraits<> 164 // FunctionTraits<>
180 // 165 //
181 // Breaks a function signature apart into typedefs for easier introspection. 166 // Breaks a function signature apart into typedefs for easier introspection.
182 template <typename Sig> 167 template <typename Sig>
183 struct FunctionTraits; 168 struct FunctionTraits;
184 169
185 $for ARITY [[ 170 $for ARITY [[
186 $range ARG 1..ARITY 171 $range ARG 1..ARITY
187 172
188 template <typename R[[]] 173 template <typename R[[]]
189 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> 174 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]>
190 struct FunctionTraits<R($for ARG , [[A$(ARG)]])> { 175 struct FunctionTraits<R($for ARG , [[A$(ARG)]])> {
191 typedef R ReturnType; 176 typedef R ReturnType;
192 $for ARG [[ 177 $for ARG [[
193 178
194 typedef A$(ARG) A$(ARG)Type; 179 typedef A$(ARG) A$(ARG)Type;
195 ]] 180 ]]
196 181
197 }; 182 };
198 183
199 ]] 184 ]]
200 185
201 186
202 // ForceVoidReturn<> 187 // ForceVoidReturn<>
203 // 188 //
204 // Set of templates that support forcing the function return type to void. 189 // Set of templates that support forcing the function return type to void.
205 template <typename Sig> 190 template <typename Sig>
206 struct ForceVoidReturn; 191 struct ForceVoidReturn;
207 192
208 $for ARITY [[ 193 template <typename R, typename... Args>
209 $range ARG 1..ARITY 194 struct ForceVoidReturn<R(Args...)> {
210 195 typedef void(RunType)(Args...);
211 template <typename R[[]]
212 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]>
213 struct ForceVoidReturn<R($for ARG , [[A$(ARG)]])> {
214 typedef void(RunType)($for ARG , [[A$(ARG)]]);
215 }; 196 };
216 197
217 ]] $$ for ARITY
218
219 198
220 // FunctorTraits<> 199 // FunctorTraits<>
221 // 200 //
222 // See description at top of file. 201 // See description at top of file.
223 template <typename T> 202 template <typename T>
224 struct FunctorTraits { 203 struct FunctorTraits {
225 typedef RunnableAdapter<T> RunnableType; 204 typedef RunnableAdapter<T> RunnableType;
226 typedef typename RunnableType::RunType RunType; 205 typedef typename RunnableType::RunType RunType;
227 }; 206 };
228 207
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 // sugar breaks though when the Runnable's RunType is not void. Thus, we 256 // sugar breaks though when the Runnable's RunType is not void. Thus, we
278 // need a partial specialization to change the syntax to drop the "return" 257 // need a partial specialization to change the syntax to drop the "return"
279 // from the invocation call. 258 // from the invocation call.
280 // 259 //
281 // WeakCalls similarly need special syntax that is applied to the first 260 // WeakCalls similarly need special syntax that is applied to the first
282 // argument to check if they should no-op themselves. 261 // argument to check if they should no-op themselves.
283 template <bool IsWeakCall, typename ReturnType, typename Runnable, 262 template <bool IsWeakCall, typename ReturnType, typename Runnable,
284 typename ArgsType> 263 typename ArgsType>
285 struct InvokeHelper; 264 struct InvokeHelper;
286 265
287 $for ARITY [[ 266 template <typename ReturnType, typename Runnable, typename... Args>
288 $range ARG 1..ARITY
289 $range WEAKCALL_ARG 2..ARITY
290
291 template <typename ReturnType, typename Runnable[[]]
292 $if ARITY > 0 [[,]] $for ARG , [[typename A$(ARG)]]>
293 struct InvokeHelper<false, ReturnType, Runnable, 267 struct InvokeHelper<false, ReturnType, Runnable,
294 void($for ARG , [[A$(ARG)]])> { 268 void(Args...)> {
295 static ReturnType MakeItSo(Runnable runnable[[]] 269 static ReturnType MakeItSo(Runnable runnable, Args... args) {
296 $if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) { 270 return runnable.Run(CallbackForward(args)...);
297 return runnable.Run($for ARG , [[CallbackForward(a$(ARG))]]);
298 } 271 }
299 }; 272 };
300 273
301 template <typename Runnable[[]] 274 template <typename Runnable, typename... Args>
302 $if ARITY > 0 [[,]] $for ARG , [[typename A$(ARG)]]> 275 struct InvokeHelper<false, void, Runnable, void(Args...)> {
303 struct InvokeHelper<false, void, Runnable, 276 static void MakeItSo(Runnable runnable, Args... args) {
304 void($for ARG , [[A$(ARG)]])> { 277 runnable.Run(CallbackForward(args)...);
305 static void MakeItSo(Runnable runnable[[]]
306 $if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) {
307 runnable.Run($for ARG , [[CallbackForward(a$(ARG))]]);
308 } 278 }
309 }; 279 };
310 280
311 $if ARITY > 0 [[ 281 template <typename Runnable, typename BoundWeakPtr, typename... Args>
312 282 struct InvokeHelper<true, void, Runnable, void(BoundWeakPtr, Args...)> {
313 template <typename Runnable[[]], typename BoundWeakPtr 283 static void MakeItSo(Runnable runnable, BoundWeakPtr weak_ptr, Args... args) {
314 $if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[typename A$(WEAKCALL_ARG)]]>
315 struct InvokeHelper<true, void, Runnable,
316 void(BoundWeakPtr
317 $if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[A$(WEAKCALL_ARG)]])> {
318 static void MakeItSo(Runnable runnable, BoundWeakPtr weak_ptr
319 $if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[A$(WEAKCALL_ARG) a$(WEAKCALL_ARG)]]) {
320 if (!weak_ptr.get()) { 284 if (!weak_ptr.get()) {
321 return; 285 return;
322 } 286 }
323 runnable.Run(weak_ptr.get() 287 runnable.Run(weak_ptr.get(), CallbackForward(args)...);
324 $if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[CallbackForward(a$(WEAKCALL_ARG))]]);
325 } 288 }
326 }; 289 };
327 290
328 ]]
329
330 ]] $$ for ARITY
331
332 #if !defined(_MSC_VER) 291 #if !defined(_MSC_VER)
333 292
334 template <typename ReturnType, typename Runnable, typename ArgsType> 293 template <typename ReturnType, typename Runnable, typename ArgsType>
335 struct InvokeHelper<true, ReturnType, Runnable, ArgsType> { 294 struct InvokeHelper<true, ReturnType, Runnable, ArgsType> {
336 // WeakCalls are only supported for functions with a void return type. 295 // WeakCalls are only supported for functions with a void return type.
337 // Otherwise, the function result would be undefined if the the WeakPtr<> 296 // Otherwise, the function result would be undefined if the the WeakPtr<>
338 // is invalidated. 297 // is invalidated.
339 COMPILE_ASSERT(is_void<ReturnType>::value, 298 COMPILE_ASSERT(is_void<ReturnType>::value,
340 weak_ptrs_can_only_bind_to_methods_without_return_values); 299 weak_ptrs_can_only_bind_to_methods_without_return_values);
341 }; 300 };
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 450
492 ]] 451 ]]
493 }; 452 };
494 453
495 ]] $$ for ARITY 454 ]] $$ for ARITY
496 455
497 } // namespace internal 456 } // namespace internal
498 } // namespace base 457 } // namespace base
499 458
500 #endif // BASE_BIND_INTERNAL_H_ 459 #endif // BASE_BIND_INTERNAL_H_
OLDNEW
« no previous file with comments | « base/bind_internal.h ('k') | base/callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698