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
5 $$ http://code.google.com/p/googletest/wiki/PumpManual
8 $$ See comment for MAX_ARITY in base/bind.h.pump.
10 $range ARITY 0..MAX_ARITY
12 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
13 // Use of this source code is governed by a BSD-style license that can be
14 // found in the LICENSE file.
16 #ifndef BASE_BIND_INTERNAL_H_
17 #define BASE_BIND_INTERNAL_H_
19 #include "base/bind_helpers.h"
20 #include "base/callback_internal.h"
21 #include "base/memory/raw_scoped_refptr_mismatch_checker.h"
22 #include "base/memory/weak_ptr.h"
23 #include "base/template_util.h"
24 #include "build/build_config.h"
27 #include "base/bind_internal_win.h"
33 // See base/callback.h for user documentation.
37 // Runnable -- A type (really a type class) that has a single Run() method
38 // and a RunType typedef that corresponds to the type of Run().
39 // A Runnable can declare that it should treated like a method
40 // call by including a typedef named IsMethod. The value of
41 // this typedef is NOT inspected, only the existence. When a
42 // Runnable declares itself a method, Bind() will enforce special
43 // refcounting + WeakPtr handling semantics for the first
44 // parameter which is expected to be an object.
45 // Functor -- A copyable type representing something that should be called.
46 // All function pointers, Callback<>, and Runnables are functors
47 // even if the invocation syntax differs.
48 // RunType -- A function type (as opposed to function _pointer_ type) for
49 // a Run() function. Usually just a convenience typedef.
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
52 // void here. We use this hack so that we do not need
53 // a new type name for each arity of type. (eg.,
54 // BindState1, BindState2). This makes forward
55 // declarations and friending much much easier.
58 // RunnableAdapter<> -- Wraps the various "function" pointer types into an
59 // object that adheres to the Runnable interface.
60 // FunctionTraits<> -- Type traits that unwrap a function signature into a
61 // a set of easier to use typedefs. Used mainly for
62 // compile time asserts.
63 // There are |ARITY| FunctionTraits types.
64 // ForceVoidReturn<> -- Helper class for translating function signatures to
65 // equivalent forms with a "void" return type.
66 // FunctorTraits<> -- Type traits used determine the correct RunType and
67 // RunnableType for a Functor. This is where function
68 // signature adapters are applied.
69 // MakeRunnable<> -- Takes a Functor and returns an object in the Runnable
70 // type class that represents the underlying Functor.
71 // There are |O(1)| MakeRunnable types.
72 // InvokeHelper<> -- Take a Runnable + arguments and actully invokes it.
73 // Handle the differing syntaxes needed for WeakPtr<> support,
74 // and for ignoring return values. This is separate from
75 // Invoker to avoid creating multiple version of Invoker<>
76 // which grows at O(n^2) with the arity.
77 // Invoker<> -- Unwraps the curried parameters and executes the Runnable.
78 // There are |(ARITY^2 + ARITY)/2| Invoketypes.
79 // BindState<> -- Stores the curried parameters, and is the main entry point
80 // into the Bind() system, doing most of the type resolution.
81 // There are ARITY BindState types.
85 // The RunnableAdapter<> templates provide a uniform interface for invoking
86 // a function pointer, method pointer, or const method pointer. The adapter
87 // exposes a Run() method with an appropriate signature. Using this wrapper
88 // allows for writing code that supports all three pointer types without
89 // undue repetition. Without it, a lot of code would need to be repeated 3
92 // For method pointers and const method pointers the first argument to Run()
93 // is considered to be the received of the method. This is similar to STL's
96 // This class also exposes a RunType typedef that is the function type of the
99 // If and only if the wrapper contains a method or const method pointer, an
100 // IsMethod typedef is exposed. The existence of this typedef (NOT the value)
101 // marks that the wrapper should be considered a method wrapper.
103 template <typename Functor>
104 class RunnableAdapter;
107 template <typename R, typename... Args>
108 class RunnableAdapter<R(*)(Args...)> {
110 typedef R (RunType)(Args...);
112 explicit RunnableAdapter(R(*function)(Args...))
113 : function_(function) {
116 R Run(typename CallbackParamTraits<Args>::ForwardType... args) {
117 return function_(CallbackForward(args)...);
121 R (*function_)(Args...);
125 template <typename R, typename T, typename... Args>
126 class RunnableAdapter<R(T::*)(Args...)> {
128 typedef R (RunType)(T*, Args...);
129 typedef true_type IsMethod;
131 explicit RunnableAdapter(R(T::*method)(Args...))
135 R Run(T* object, typename CallbackParamTraits<Args>::ForwardType... args) {
136 return (object->*method_)(CallbackForward(args)...);
140 R (T::*method_)(Args...);
144 template <typename R, typename T, typename... Args>
145 class RunnableAdapter<R(T::*)(Args...) const> {
147 typedef R (RunType)(const T*, Args...);
148 typedef true_type IsMethod;
150 explicit RunnableAdapter(R(T::*method)(Args...) const)
154 R Run(const T* object,
155 typename CallbackParamTraits<Args>::ForwardType... args) {
156 return (object->*method_)(CallbackForward(args)...);
160 R (T::*method_)(Args...) const;
163 // TODO(tzik): Remove FunctionTraits after we finish removing bind.pump.
166 // Breaks a function signature apart into typedefs for easier introspection.
167 template <typename Sig>
168 struct FunctionTraits;
173 template <typename R[[]]
174 $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]>
175 struct FunctionTraits<R($for ARG , [[A$(ARG)]])> {
176 typedef R ReturnType;
179 typedef A$(ARG) A$(ARG)Type;
189 // Set of templates that support forcing the function return type to void.
190 template <typename Sig>
191 struct ForceVoidReturn;
193 template <typename R, typename... Args>
194 struct ForceVoidReturn<R(Args...)> {
195 typedef void(RunType)(Args...);
201 // See description at top of file.
202 template <typename T>
203 struct FunctorTraits {
204 typedef RunnableAdapter<T> RunnableType;
205 typedef typename RunnableType::RunType RunType;
208 template <typename T>
209 struct FunctorTraits<IgnoreResultHelper<T> > {
210 typedef typename FunctorTraits<T>::RunnableType RunnableType;
211 typedef typename ForceVoidReturn<
212 typename RunnableType::RunType>::RunType RunType;
215 template <typename T>
216 struct FunctorTraits<Callback<T> > {
217 typedef Callback<T> RunnableType;
218 typedef typename Callback<T>::RunType RunType;
224 // Converts a passed in functor to a RunnableType using type inference.
226 template <typename T>
227 typename FunctorTraits<T>::RunnableType MakeRunnable(const T& t) {
228 return RunnableAdapter<T>(t);
231 template <typename T>
232 typename FunctorTraits<T>::RunnableType
233 MakeRunnable(const IgnoreResultHelper<T>& t) {
234 return MakeRunnable(t.functor_);
237 template <typename T>
238 const typename FunctorTraits<Callback<T> >::RunnableType&
239 MakeRunnable(const Callback<T>& t) {
240 DCHECK(!t.is_null());
247 // There are 3 logical InvokeHelper<> specializations: normal, void-return,
250 // The normal type just calls the underlying runnable.
252 // We need a InvokeHelper to handle void return types in order to support
253 // IgnoreResult(). Normally, if the Runnable's RunType had a void return,
254 // the template system would just accept "return functor.Run()" ignoring
255 // the fact that a void function is being used with return. This piece of
256 // sugar breaks though when the Runnable's RunType is not void. Thus, we
257 // need a partial specialization to change the syntax to drop the "return"
258 // from the invocation call.
260 // WeakCalls similarly need special syntax that is applied to the first
261 // argument to check if they should no-op themselves.
262 template <bool IsWeakCall, typename ReturnType, typename Runnable,
266 template <typename ReturnType, typename Runnable, typename... Args>
267 struct InvokeHelper<false, ReturnType, Runnable,
269 static ReturnType MakeItSo(Runnable runnable, Args... args) {
270 return runnable.Run(CallbackForward(args)...);
274 template <typename Runnable, typename... Args>
275 struct InvokeHelper<false, void, Runnable, void(Args...)> {
276 static void MakeItSo(Runnable runnable, Args... args) {
277 runnable.Run(CallbackForward(args)...);
281 template <typename Runnable, typename BoundWeakPtr, typename... Args>
282 struct InvokeHelper<true, void, Runnable, void(BoundWeakPtr, Args...)> {
283 static void MakeItSo(Runnable runnable, BoundWeakPtr weak_ptr, Args... args) {
284 if (!weak_ptr.get()) {
287 runnable.Run(weak_ptr.get(), CallbackForward(args)...);
291 #if !defined(_MSC_VER)
293 template <typename ReturnType, typename Runnable, typename ArgsType>
294 struct InvokeHelper<true, ReturnType, Runnable, ArgsType> {
295 // WeakCalls are only supported for functions with a void return type.
296 // Otherwise, the function result would be undefined if the the WeakPtr<>
298 COMPILE_ASSERT(is_void<ReturnType>::value,
299 weak_ptrs_can_only_bind_to_methods_without_return_values);
306 // See description at the top of the file.
307 template <int NumBound, typename Storage, typename RunType>
312 $$ Number of bound arguments.
313 $range BOUND 0..ARITY
316 $var UNBOUND = ARITY - BOUND
318 $range BOUND_ARG 1..BOUND
319 $range UNBOUND_ARG (ARITY - UNBOUND + 1)..ARITY
321 // Arity $(ARITY) -> $(UNBOUND).
322 template <typename StorageType, typename R[[]]
323 $if ARITY > 0 [[,]][[]]
324 $for ARG , [[typename X$(ARG)]]>
325 struct Invoker<$(BOUND), StorageType, R($for ARG , [[X$(ARG)]])> {
326 typedef R(RunType)(BindStateBase*[[]]
327 $if UNBOUND != 0 [[, ]]
328 $for UNBOUND_ARG , [[typename CallbackParamTraits<X$(UNBOUND_ARG)>::ForwardType]]);
330 typedef R(UnboundRunType)($for UNBOUND_ARG , [[X$(UNBOUND_ARG)]]);
332 static R Run(BindStateBase* base[[]]
333 $if UNBOUND != 0 [[, ]][[]]
334 $for UNBOUND_ARG , [[
335 typename CallbackParamTraits<X$(UNBOUND_ARG)>::ForwardType x$(UNBOUND_ARG)
338 StorageType* storage = static_cast<StorageType*>(base);
340 // Local references to make debugger stepping easier. If in a debugger,
341 // you really want to warp ahead and step through the
342 // InvokeHelper<>::MakeItSo() call below.
346 typedef typename StorageType::Bound$(BOUND_ARG)UnwrapTraits Bound$(BOUND_ARG)UnwrapTraits;
353 typename Bound$(BOUND_ARG)UnwrapTraits::ForwardType x$(BOUND_ARG) =
354 Bound$(BOUND_ARG)UnwrapTraits::Unwrap(storage->p$(BOUND_ARG)_);
357 return InvokeHelper<StorageType::IsWeakCall::value, R,
358 typename StorageType::RunnableType,
361 typename Bound$(BOUND_ARG)UnwrapTraits::ForwardType
364 $if UNBOUND > 0 [[$if BOUND > 0 [[, ]]]][[]]
366 $for UNBOUND_ARG , [[
367 typename CallbackParamTraits<X$(UNBOUND_ARG)>::ForwardType x$(UNBOUND_ARG)
370 ::MakeItSo(storage->runnable_
371 $if ARITY > 0[[, ]] $for ARG , [[CallbackForward(x$(ARG))]]);
381 // This stores all the state passed into Bind() and is also where most
382 // of the template resolution magic occurs.
384 // Runnable is the functor we are binding arguments to.
385 // RunType is type of the Run() function that the Invoker<> should use.
386 // Normally, this is the same as the RunType of the Runnable, but it can
387 // be different if an adapter like IgnoreResult() has been used.
389 // BoundArgsType contains the storage type for all the bound arguments by
390 // (ab)using a function type.
391 template <typename Runnable, typename RunType, typename BoundArgsType>
397 template <typename Runnable, typename RunType[[]]
398 $if ARITY > 0[[, ]] $for ARG , [[typename P$(ARG)]]>
399 struct BindState<Runnable, RunType, void($for ARG , [[P$(ARG)]])> : public BindStateBase {
400 typedef Runnable RunnableType;
403 typedef IsWeakMethod<HasIsMethodTag<Runnable>::value, P1> IsWeakCall;
405 typedef false_type IsWeakCall;
408 typedef Invoker<$(ARITY), BindState, RunType> InvokerType;
409 typedef typename InvokerType::UnboundRunType UnboundRunType;
413 // Convenience typedefs for bound argument types.
416 typedef UnwrapTraits<P$(ARG)> Bound$(ARG)UnwrapTraits;
423 $$ The extra [[ ]] is needed to massage spacing. Silly pump.py.
424 [[ ]]$if ARITY == 0 [[explicit ]]BindState(const Runnable& runnable
425 $if ARITY > 0 [[, ]] $for ARG , [[const P$(ARG)& p$(ARG)]])
426 : runnable_(runnable)[[]]
435 MaybeRefcount<HasIsMethodTag<Runnable>::value, P1>::AddRef(p1_);
440 virtual ~BindState() {
442 MaybeRefcount<HasIsMethodTag<Runnable>::value, P1>::Release(p1_);
446 RunnableType runnable_;
456 } // namespace internal
459 #endif // BASE_BIND_INTERNAL_H_