ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 8 | $$ See comment for MAX_ARITY in base/bind.h.pump. |
| 9 | $var MAX_ARITY = 7 |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 10 | $range ARITY 0..MAX_ARITY |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 11 | |
| 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. |
| 15 | |
| 16 | #ifndef BASE_BIND_INTERNAL_H_ |
| 17 | #define BASE_BIND_INTERNAL_H_ |
| 18 | #pragma once |
| 19 | |
| 20 | #include "base/bind_helpers.h" |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 21 | #include "base/callback_internal.h" |
ajwong@chromium.org | 27e5685 | 2011-10-01 15:31:41 +0900 | [diff] [blame] | 22 | #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 23 | #include "base/memory/weak_ptr.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 24 | #include "base/template_util.h" |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 25 | #include "build/build_config.h" |
| 26 | |
| 27 | #if defined(OS_WIN) |
| 28 | #include "base/bind_internal_win.h" |
| 29 | #endif |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 30 | |
| 31 | namespace base { |
| 32 | namespace internal { |
| 33 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 34 | // CONCEPTS: |
| 35 | // Runnable -- A type (really a type class) that has a single Run() method |
| 36 | // and a RunType typedef that corresponds to the type of Run(). |
| 37 | // A Runnable can declare that it should treated like a method |
| 38 | // call by including a typedef named IsMethod. The value of |
| 39 | // this typedef is NOT inspected, only the existence. When a |
| 40 | // Runnable declares itself a method, Bind() will enforce special |
| 41 | // refcounting + WeakPtr handling semantics for the first |
| 42 | // parameter which is expected to be an object. |
| 43 | // Functor -- A copyable type representing something that should be called. |
| 44 | // All function pointers, Callback<>, and Runnables are functors |
| 45 | // even if the invocation syntax differs. |
| 46 | // RunType -- A function type (as opposed to function _pointer_ type) for |
| 47 | // a Run() function. Usually just a convenience typedef. |
| 48 | // (Bound)ArgsType -- A function type that is being (ab)used to store the |
| 49 | // types of set of arguments. The "return" type is always |
| 50 | // void here. We use this hack so that we do not need |
| 51 | // a new type name for each arity of type. (eg., |
| 52 | // BindState1, BindState2). This makes forward |
| 53 | // declarations and friending much much easier. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 54 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 55 | // Types: |
| 56 | // RunnableAdapter<> -- Wraps the various "function" pointer types into an |
| 57 | // object that adheres to the Runnable interface. |
| 58 | // There are |3*ARITY| RunnableAdapter types. |
| 59 | // FunctionTraits<> -- Type traits that unwrap a function signature into a |
| 60 | // a set of easier to use typedefs. Used mainly for |
| 61 | // compile time asserts. |
| 62 | // There are |ARITY| FunctionTraits types. |
| 63 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 64 | // equivalent forms with a "void" return type. |
| 65 | // There are |ARITY| ForceVoidReturn types. |
| 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 | // There are |ARITY| ForceVoidReturn types. |
| 70 | // MakeRunnable<> -- Takes a Functor and returns an object in the Runnable |
| 71 | // type class that represents the underlying Functor. |
| 72 | // There are |O(1)| MakeRunnable types. |
| 73 | // InvokeHelper<> -- Take a Runnable + arguments and actully invokes it. |
| 74 | // Handle the differing syntaxes needed for WeakPtr<> support, |
| 75 | // and for ignoring return values. This is separate from |
| 76 | // Invoker to avoid creating multiple version of Invoker<> |
| 77 | // which grows at O(n^2) with the arity. |
| 78 | // There are |k*ARITY| InvokeHelper types. |
| 79 | // Invoker<> -- Unwraps the curried parameters and executes the Runnable. |
| 80 | // There are |(ARITY^2 + ARITY)/2| Invoketypes. |
| 81 | // BindState<> -- Stores the curried parameters, and is the main entry point |
| 82 | // into the Bind() system, doing most of the type resolution. |
| 83 | // There are ARITY BindState types. |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 84 | |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 85 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 86 | // RunnableAdapter<> |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 87 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 88 | // The RunnableAdapter<> templates provide a uniform interface for invoking |
| 89 | // a function pointer, method pointer, or const method pointer. The adapter |
| 90 | // exposes a Run() method with an appropriate signature. Using this wrapper |
| 91 | // allows for writing code that supports all three pointer types without |
| 92 | // undue repetition. Without it, a lot of code would need to be repeated 3 |
| 93 | // times. |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 94 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 95 | // For method pointers and const method pointers the first argument to Run() |
| 96 | // is considered to be the received of the method. This is similar to STL's |
| 97 | // mem_fun(). |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 98 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 99 | // This class also exposes a RunType typedef that is the function type of the |
| 100 | // Run() function. |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 101 | // |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 102 | // If and only if the wrapper contains a method or const method pointer, an |
| 103 | // IsMethod typedef is exposed. The existence of this typedef (NOT the value) |
| 104 | // marks that the wrapper should be considered a method wrapper. |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 105 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 106 | template <typename Functor> |
| 107 | class RunnableAdapter; |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 108 | |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 109 | $for ARITY [[ |
| 110 | $range ARG 1..ARITY |
| 111 | |
| 112 | // Function: Arity $(ARITY). |
| 113 | template <typename R[[]] |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 114 | $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> |
| 115 | class RunnableAdapter<R(*)($for ARG , [[A$(ARG)]])> { |
| 116 | public: |
| 117 | typedef R (RunType)($for ARG , [[A$(ARG)]]); |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 118 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 119 | explicit RunnableAdapter(R(*function)($for ARG , [[A$(ARG)]])) |
| 120 | : function_(function) { |
| 121 | } |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 122 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 123 | R Run($for ARG , [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 124 | return function_($for ARG , [[a$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 125 | } |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 126 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 127 | private: |
| 128 | R (*function_)($for ARG , [[A$(ARG)]]); |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // Method: Arity $(ARITY). |
| 132 | template <typename R, typename T[[]] |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 133 | $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> |
| 134 | class RunnableAdapter<R(T::*)($for ARG , [[A$(ARG)]])> { |
| 135 | public: |
| 136 | typedef R (RunType)(T*[[]] |
| 137 | $if ARITY > 0[[, ]] $for ARG , [[A$(ARG)]]); |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 138 | typedef true_type IsMethod; |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 139 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 140 | explicit RunnableAdapter(R(T::*method)($for ARG , [[A$(ARG)]])) |
| 141 | : method_(method) { |
| 142 | } |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 143 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 144 | R Run(T* object[[]] |
| 145 | $if ARITY > 0[[, ]] $for ARG, [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 146 | return (object->*method_)($for ARG , [[a$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 147 | } |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 148 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 149 | private: |
| 150 | R (T::*method_)($for ARG , [[A$(ARG)]]); |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | // Const Method: Arity $(ARITY). |
| 154 | template <typename R, typename T[[]] |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 155 | $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> |
| 156 | class RunnableAdapter<R(T::*)($for ARG , [[A$(ARG)]]) const> { |
| 157 | public: |
| 158 | typedef R (RunType)(const T*[[]] |
| 159 | $if ARITY > 0[[, ]] $for ARG , [[A$(ARG)]]); |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 160 | typedef true_type IsMethod; |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 161 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 162 | explicit RunnableAdapter(R(T::*method)($for ARG , [[A$(ARG)]]) const) |
| 163 | : method_(method) { |
| 164 | } |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 165 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 166 | R Run(const T* object[[]] |
| 167 | $if ARITY > 0[[, ]] $for ARG, [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 168 | return (object->*method_)($for ARG , [[a$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 169 | } |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 170 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 171 | private: |
| 172 | R (T::*method_)($for ARG , [[A$(ARG)]]) const; |
| 173 | }; |
| 174 | |
| 175 | ]] $$ for ARITY |
| 176 | |
| 177 | |
| 178 | // FunctionTraits<> |
| 179 | // |
| 180 | // Breaks a function signature apart into typedefs for easier introspection. |
| 181 | template <typename Sig> |
| 182 | struct FunctionTraits; |
| 183 | |
| 184 | $for ARITY [[ |
| 185 | $range ARG 1..ARITY |
| 186 | |
| 187 | template <typename R[[]] |
| 188 | $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> |
| 189 | struct FunctionTraits<R($for ARG , [[A$(ARG)]])> { |
| 190 | typedef R ReturnType; |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 191 | $for ARG [[ |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 192 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 193 | typedef A$(ARG) A$(ARG)Type; |
| 194 | ]] |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 195 | |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 196 | }; |
| 197 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 198 | ]] |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 199 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 200 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 201 | // ForceVoidReturn<> |
| 202 | // |
| 203 | // Set of templates that support forcing the function return type to void. |
| 204 | template <typename Sig> |
| 205 | struct ForceVoidReturn; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 206 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 207 | $for ARITY [[ |
| 208 | $range ARG 1..ARITY |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 209 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 210 | template <typename R[[]] |
| 211 | $if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> |
| 212 | struct ForceVoidReturn<R($for ARG , [[A$(ARG)]])> { |
| 213 | typedef void(RunType)($for ARG , [[A$(ARG)]]); |
| 214 | }; |
| 215 | |
| 216 | ]] $$ for ARITY |
| 217 | |
| 218 | |
| 219 | // FunctorTraits<> |
| 220 | // |
| 221 | // See description at top of file. |
| 222 | template <typename T> |
| 223 | struct FunctorTraits { |
| 224 | typedef RunnableAdapter<T> RunnableType; |
| 225 | typedef typename RunnableType::RunType RunType; |
| 226 | }; |
| 227 | |
| 228 | template <typename T> |
| 229 | struct FunctorTraits<IgnoreResultHelper<T> > { |
| 230 | typedef typename FunctorTraits<T>::RunnableType RunnableType; |
| 231 | typedef typename ForceVoidReturn< |
| 232 | typename RunnableType::RunType>::RunType RunType; |
| 233 | }; |
| 234 | |
| 235 | template <typename T> |
| 236 | struct FunctorTraits<Callback<T> > { |
| 237 | typedef Callback<T> RunnableType; |
| 238 | typedef typename Callback<T>::RunType RunType; |
| 239 | }; |
| 240 | |
| 241 | |
| 242 | // MakeRunnable<> |
| 243 | // |
| 244 | // Converts a passed in functor to a RunnableType using type inference. |
| 245 | |
| 246 | template <typename T> |
| 247 | typename FunctorTraits<T>::RunnableType MakeRunnable(const T& t) { |
| 248 | return RunnableAdapter<T>(t); |
| 249 | } |
| 250 | |
| 251 | template <typename T> |
| 252 | typename FunctorTraits<T>::RunnableType |
| 253 | MakeRunnable(const IgnoreResultHelper<T>& t) { |
| 254 | return MakeRunnable(t.functor_); |
| 255 | } |
| 256 | |
| 257 | template <typename T> |
| 258 | const typename FunctorTraits<Callback<T> >::RunnableType& |
| 259 | MakeRunnable(const Callback<T>& t) { |
| 260 | return t; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | // InvokeHelper<> |
| 265 | // |
| 266 | // There are 3 logical InvokeHelper<> specializations: normal, void-return, |
| 267 | // WeakCalls. |
| 268 | // |
| 269 | // The normal type just calls the underlying runnable. |
| 270 | // |
| 271 | // We need a InvokeHelper to handle void return types in order to support |
| 272 | // IgnoreResult(). Normally, if the Runnable's RunType had a void return, |
| 273 | // the template system would just accept "return functor.Run()" ignoring |
| 274 | // the fact that a void function is being used with return. This piece of |
| 275 | // sugar breaks though when the Runnable's RunType is not void. Thus, we |
| 276 | // need a partial specialization to change the syntax to drop the "return" |
| 277 | // from the invocation call. |
| 278 | // |
| 279 | // WeakCalls similarly need special syntax that is applied to the first |
| 280 | // argument to check if they should no-op themselves. |
| 281 | template <bool IsWeakCall, typename ReturnType, typename Runnable, |
| 282 | typename ArgsType> |
| 283 | struct InvokeHelper; |
| 284 | |
| 285 | $for ARITY [[ |
| 286 | $range ARG 1..ARITY |
| 287 | |
| 288 | template <typename ReturnType, typename Runnable[[]] |
| 289 | $if ARITY > 0 [[,]] $for ARG , [[typename A$(ARG)]]> |
| 290 | struct InvokeHelper<false, ReturnType, Runnable, |
| 291 | void($for ARG , [[A$(ARG)]])> { |
| 292 | static ReturnType MakeItSo(Runnable runnable[[]] |
| 293 | $if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) { |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 294 | return runnable.Run($for ARG , [[a$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 295 | } |
| 296 | }; |
| 297 | |
| 298 | template <typename Runnable[[]] |
| 299 | $if ARITY > 0 [[,]] $for ARG , [[typename A$(ARG)]]> |
| 300 | struct InvokeHelper<false, void, Runnable, |
| 301 | void($for ARG , [[A$(ARG)]])> { |
| 302 | static void MakeItSo(Runnable runnable[[]] |
| 303 | $if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) { |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 304 | runnable.Run($for ARG , [[a$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 305 | } |
| 306 | }; |
| 307 | |
| 308 | $if ARITY > 0 [[ |
| 309 | |
| 310 | template <typename Runnable[[]], $for ARG , [[typename A$(ARG)]]> |
| 311 | struct InvokeHelper<true, void, Runnable, |
| 312 | void($for ARG , [[A$(ARG)]])> { |
| 313 | static void MakeItSo(Runnable runnable[[]] |
| 314 | $if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) { |
| 315 | if (!a1.get()) { |
| 316 | return; |
| 317 | } |
| 318 | |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 319 | runnable.Run($for ARG , [[a$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 320 | } |
| 321 | }; |
| 322 | |
| 323 | ]] |
| 324 | |
| 325 | ]] $$ for ARITY |
| 326 | |
| 327 | #if !defined(_MSC_VER) |
| 328 | |
| 329 | template <typename ReturnType, typename Runnable, typename ArgsType> |
| 330 | struct InvokeHelper<true, ReturnType, Runnable, ArgsType> { |
| 331 | // WeakCalls are only supported for functions with a void return type. |
| 332 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 333 | // is invalidated. |
| 334 | COMPILE_ASSERT(is_void<ReturnType>::value, |
| 335 | weak_ptrs_can_only_bind_to_methods_without_return_values); |
| 336 | }; |
| 337 | |
| 338 | #endif |
| 339 | |
| 340 | // Invoker<> |
| 341 | // |
| 342 | // See description at the top of the file. |
| 343 | template <int NumBound, typename Storage, typename RunType> |
| 344 | struct Invoker; |
| 345 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 346 | $for ARITY [[ |
| 347 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 348 | $$ Number of bound arguments. |
| 349 | $range BOUND 0..ARITY |
| 350 | $for BOUND [[ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 351 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 352 | $var UNBOUND = ARITY - BOUND |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 353 | $range ARG 1..ARITY |
| 354 | $range BOUND_ARG 1..BOUND |
| 355 | $range UNBOUND_ARG (ARITY - UNBOUND + 1)..ARITY |
| 356 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 357 | // Arity $(ARITY) -> $(UNBOUND). |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 358 | template <typename StorageType, typename R[[]] |
| 359 | $if ARITY > 0 [[,]][[]] |
| 360 | $for ARG , [[typename X$(ARG)]]> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 361 | struct Invoker<$(BOUND), StorageType, R($for ARG , [[X$(ARG)]])> { |
| 362 | typedef R(RunType)(BindStateBase*[[]] |
ajwong@chromium.org | ec1750a | 2011-06-27 01:22:50 +0900 | [diff] [blame] | 363 | $if UNBOUND != 0 [[, ]] |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 364 | $for UNBOUND_ARG , [[typename CallbackParamTraits<X$(UNBOUND_ARG)>::ForwardType]]); |
ajwong@chromium.org | ec1750a | 2011-06-27 01:22:50 +0900 | [diff] [blame] | 365 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 366 | typedef R(UnboundRunType)($for UNBOUND_ARG , [[X$(UNBOUND_ARG)]]); |
| 367 | |
| 368 | static R Run(BindStateBase* base[[]] |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 369 | $if UNBOUND != 0 [[, ]][[]] |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 370 | $for UNBOUND_ARG , [[ |
| 371 | typename CallbackParamTraits<X$(UNBOUND_ARG)>::ForwardType x$(UNBOUND_ARG) |
| 372 | ]][[]] |
| 373 | ) { |
| 374 | StorageType* storage = static_cast<StorageType*>(base); |
| 375 | |
| 376 | // Local references to make debugger stepping easier. If in a debugger, |
| 377 | // you really want to warp ahead and step through the |
| 378 | // InvokeHelper<>::MakeItSo() call below. |
| 379 | $for BOUND_ARG |
| 380 | [[ |
| 381 | |
| 382 | typedef typename StorageType::Bound$(BOUND_ARG)UnwrapTraits Bound$(BOUND_ARG)UnwrapTraits; |
| 383 | ]] |
| 384 | |
| 385 | |
| 386 | $for BOUND_ARG |
| 387 | [[ |
| 388 | |
| 389 | typename Bound$(BOUND_ARG)UnwrapTraits::ForwardType x$(BOUND_ARG) = |
| 390 | Bound$(BOUND_ARG)UnwrapTraits::Unwrap(storage->p$(BOUND_ARG)_); |
| 391 | ]] |
| 392 | |
| 393 | return InvokeHelper<StorageType::IsWeakCall::value, R, |
| 394 | typename StorageType::RunnableType, |
| 395 | void( |
| 396 | $for BOUND_ARG , [[ |
| 397 | typename Bound$(BOUND_ARG)UnwrapTraits::ForwardType |
| 398 | ]] |
| 399 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 400 | $if UNBOUND > 0 [[$if BOUND > 0 [[, ]]]][[]] |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 401 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 402 | $for UNBOUND_ARG , [[ |
| 403 | typename CallbackParamTraits<X$(UNBOUND_ARG)>::ForwardType x$(UNBOUND_ARG) |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 404 | ]] |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 405 | )> |
| 406 | ::MakeItSo(storage->runnable_ |
jeremya@chromium.org | d71e8fe | 2011-12-22 08:37:57 +0900 | [diff] [blame^] | 407 | $if ARITY > 0[[, ]] $for ARG , [[x$(ARG)]]); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 408 | } |
| 409 | }; |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 410 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 411 | ]] $$ for BOUND |
| 412 | ]] $$ for ARITY |
| 413 | |
| 414 | |
| 415 | // BindState<> |
| 416 | // |
| 417 | // This stores all the state passed into Bind() and is also where most |
| 418 | // of the template resolution magic occurs. |
| 419 | // |
| 420 | // Runnable is the functor we are binding arguments to. |
| 421 | // RunType is type of the Run() function that the Invoker<> should use. |
| 422 | // Normally, this is the same as the RunType of the Runnable, but it can |
| 423 | // be different if an adapter like IgnoreResult() has been used. |
| 424 | // |
| 425 | // BoundArgsType contains the storage type for all the bound arguments by |
| 426 | // (ab)using a function type. |
| 427 | template <typename Runnable, typename RunType, typename BoundArgsType> |
| 428 | struct BindState; |
| 429 | |
| 430 | $for ARITY [[ |
| 431 | $range ARG 1..ARITY |
| 432 | |
| 433 | template <typename Runnable, typename RunType[[]] |
| 434 | $if ARITY > 0[[, ]] $for ARG , [[typename P$(ARG)]]> |
| 435 | struct BindState<Runnable, RunType, void($for ARG , [[P$(ARG)]])> : public BindStateBase { |
| 436 | typedef Runnable RunnableType; |
| 437 | |
| 438 | $if ARITY > 0 [[ |
| 439 | typedef IsWeakMethod<HasIsMethodTag<Runnable>::value, P1> IsWeakCall; |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 440 | ]] $else [[ |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 441 | typedef false_type IsWeakCall; |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 442 | ]] |
| 443 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 444 | typedef Invoker<$(ARITY), BindState, RunType> InvokerType; |
| 445 | typedef typename InvokerType::UnboundRunType UnboundRunType; |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 446 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 447 | $if ARITY > 0 [[ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 448 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 449 | // Convenience typedefs for bound argument types. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 450 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 451 | $for ARG [[ |
| 452 | typedef UnwrapTraits<P$(ARG)> Bound$(ARG)UnwrapTraits; |
| 453 | |
| 454 | ]] $$ for ARG |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 455 | |
| 456 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 457 | ]] $$ if ARITY > 0 |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 458 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 459 | $$ The extra [[ ]] is needed to massage spacing. Silly pump.py. |
| 460 | [[ ]]$if ARITY == 0 [[explicit ]]BindState(const Runnable& runnable |
| 461 | $if ARITY > 0 [[, ]] $for ARG , [[const P$(ARG)& p$(ARG)]]) |
| 462 | : runnable_(runnable)[[]] |
| 463 | $if ARITY == 0 [[ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 464 | { |
| 465 | |
| 466 | ]] $else [[ |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 467 | , $for ARG , [[ |
| 468 | |
| 469 | p$(ARG)_(p$(ARG)) |
| 470 | ]] { |
| 471 | MaybeRefcount<HasIsMethodTag<Runnable>::value, P1>::AddRef(p1_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 472 | |
| 473 | ]] |
| 474 | } |
| 475 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 476 | virtual ~BindState() { |
| 477 | $if ARITY > 0 [[ |
| 478 | MaybeRefcount<HasIsMethodTag<Runnable>::value, P1>::Release(p1_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 479 | ]] |
| 480 | } |
| 481 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 482 | RunnableType runnable_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 483 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 484 | $for ARG [[ |
| 485 | P$(ARG) p$(ARG)_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 486 | |
| 487 | ]] |
| 488 | }; |
| 489 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 490 | ]] $$ for ARITY |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 491 | |
| 492 | } // namespace internal |
| 493 | } // namespace base |
| 494 | |
| 495 | #endif // BASE_BIND_INTERNAL_H_ |