OLD | NEW |
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 | 10 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // HOW THE IMPLEMENTATION WORKS: | 127 // HOW THE IMPLEMENTATION WORKS: |
128 // | 128 // |
129 // There are three main components to the system: | 129 // There are three main components to the system: |
130 // 1) The Callback classes. | 130 // 1) The Callback classes. |
131 // 2) The Bind() functions. | 131 // 2) The Bind() functions. |
132 // 3) The arguments wrappers (e.g., Unretained() and ConstRef()). | 132 // 3) The arguments wrappers (e.g., Unretained() and ConstRef()). |
133 // | 133 // |
134 // The Callback classes represent a generic function pointer. Internally, | 134 // The Callback classes represent a generic function pointer. Internally, |
135 // it stores a refcounted piece of state that represents the target function | 135 // it stores a refcounted piece of state that represents the target function |
136 // and all its bound parameters. Each Callback specialization has a templated | 136 // and all its bound parameters. Each Callback specialization has a templated |
137 // constructor that takes an BindState<>*. In the context of the constructor, | 137 // constructor that takes an BindStateHolder<> object. In the context of |
138 // the static type of this BindState<> pointer uniquely identifies the | 138 // the constructor, the static type of this BindStateHolder<> object |
139 // function it is representing, all its bound parameters, and a Run() method | 139 // uniquely identifies the function it is representing, all its bound |
140 // that is capable of invoking the target. | 140 // parameters, and a DoInvoke() that is capable of invoking the target. |
141 // | 141 // |
142 // Callback's constructor takes the BindState<>* that has the full static type | 142 // Callback's constructor is takes the BindStateHolder<> that has the |
143 // and erases the target function type as well as the types of the bound | 143 // full static type and erases the target function type, and the bound |
144 // parameters. It does this by storing a pointer to the specific Run() | 144 // parameters. It does this by storing a pointer to the specific DoInvoke() |
145 // function, and upcasting the state of BindState<>* to a | 145 // function, and upcasting the state of BindStateHolder<> to a |
146 // BindStateBase*. This is safe as long as this BindStateBase pointer | 146 // BindStateBase. This is safe as long as this BindStateBase pointer |
147 // is only used with the stored Run() pointer. | 147 // is only used with the stored DoInvoke() pointer. |
148 // | 148 // |
149 // To BindState<> objects are created inside the Bind() functions. | 149 // To create BindStateHolder<> objects, we use the Bind() functions. |
150 // These functions, along with a set of internal templates, are responsible for | 150 // These functions, along with a set of internal templates, are reponsible for |
151 // | 151 // |
152 // - Unwrapping the function signature into return type, and parameters | 152 // - Unwrapping the function signature into return type, and parameters |
153 // - Determining the number of parameters that are bound | 153 // - Determining the number of parameters that are bound |
154 // - Creating the BindState storing the bound parameters | 154 // - Creating the storage for the bound parameters |
155 // - Performing compile-time asserts to avoid error-prone behavior | 155 // - Performing compile-time asserts to avoid error-prone behavior |
156 // - Returning an Callback<> with an arity matching the number of unbound | 156 // - Returning an BindStateHolder<> with an DoInvoke() that has an arity |
157 // parameters and that knows the correct refcounting semantics for the | 157 // matching the number of unbound parameters, and knows the correct |
158 // target object if we are binding a method. | 158 // refcounting semantics for the target object if we are binding a class |
| 159 // method. |
159 // | 160 // |
160 // The Bind functions do the above using type-inference, and template | 161 // The Bind functions do the above using type-inference, and template |
161 // specializations. | 162 // specializations. |
162 // | 163 // |
163 // By default Bind() will store copies of all bound parameters, and attempt | 164 // By default Bind() will store copies of all bound parameters, and attempt |
164 // to refcount a target object if the function being bound is a class method. | 165 // to refcount a target object if the function being bound is a class method. |
165 // | 166 // |
166 // To change this behavior, we introduce a set of argument wrappers | 167 // To change this behavior, we introduce a set of argument wrappers |
167 // (e.g., Unretained(), and ConstRef()). These are simple container templates | 168 // (e.g., Unretained(), and ConstRef()). These are simple container templates |
168 // that are passed by value, and wrap a pointer to argument. See the | 169 // that are passed by value, and wrap a pointer to argument. See the |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // | 237 // |
237 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No
te that | 238 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No
te that |
238 // even though the template typelist grows, the specialization still | 239 // even though the template typelist grows, the specialization still |
239 // only has one type: the function signature. | 240 // only has one type: the function signature. |
240 // | 241 // |
241 // If you are thinking of forward declaring Callback in your own header file, | 242 // If you are thinking of forward declaring Callback in your own header file, |
242 // please include "base/callback_forward.h" instead. | 243 // please include "base/callback_forward.h" instead. |
243 template <typename Sig> | 244 template <typename Sig> |
244 class Callback; | 245 class Callback; |
245 | 246 |
246 namespace internal { | |
247 template <typename Runnable, typename RunType, typename BoundArgsType> | |
248 struct BindState; | |
249 } // namespace internal | |
250 | |
251 | 247 |
252 $range ARITY 0..MAX_ARITY | 248 $range ARITY 0..MAX_ARITY |
253 $for ARITY [[ | 249 $for ARITY [[ |
254 $range ARG 1..ARITY | 250 $range ARG 1..ARITY |
255 | 251 |
256 $if ARITY == 0 [[ | 252 $if ARITY == 0 [[ |
257 template <typename R> | 253 template <typename R> |
258 class Callback<R(void)> : public internal::CallbackBase { | 254 class Callback<R(void)> : public internal::CallbackBase { |
259 ]] $else [[ | 255 ]] $else [[ |
260 template <typename R, $for ARG , [[typename A$(ARG)]]> | 256 template <typename R, $for ARG , [[typename A$(ARG)]]> |
261 class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase { | 257 class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase { |
262 ]] | 258 ]] |
263 | 259 |
264 public: | 260 public: |
265 typedef R(RunType)($for ARG , [[A$(ARG)]]); | 261 typedef R(RunType)($for ARG , [[A$(ARG)]]); |
266 | 262 |
267 Callback() : CallbackBase(NULL) { } | 263 Callback() : CallbackBase(NULL, NULL) { } |
268 | 264 |
| 265 // We pass BindStateHolder by const ref to avoid incurring an |
| 266 // unnecessary AddRef/Unref pair even though we will modify the object. |
| 267 // We cannot use a normal reference because the compiler will warn |
| 268 // since this is often used on a return value, which is a temporary. |
| 269 // |
269 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT | 270 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
270 // return the exact Callback<> type. See base/bind.h for details. | 271 // return the exact Callback<> type. See base/bind.h for details. |
271 template <typename Runnable, typename RunType, typename BoundArgsType> | 272 template <typename T> |
272 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state) | 273 Callback(const internal::BindStateHolder<T>& bind_state_holder) |
273 : CallbackBase(bind_state) { | 274 : CallbackBase(NULL, &bind_state_holder.bind_state_) { |
274 | 275 // Force the assignment to a location variable of PolymorphicInvoke |
275 // Force the assignment to a local variable of PolymorphicInvoke | |
276 // so the compiler will typecheck that the passed in Run() method has | 276 // so the compiler will typecheck that the passed in Run() method has |
277 // the correct type. | 277 // the correct type. |
278 PolymorphicInvoke invoke_func = | 278 PolymorphicInvoke invoke_func = &T::InvokerType::Run; |
279 &internal::BindState<Runnable, RunType, BoundArgsType> | |
280 ::InvokerType::Run; | |
281 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); | 279 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
282 } | 280 } |
283 | 281 |
284 bool Equals(const Callback& other) const { | 282 bool Equals(const Callback& other) const { |
285 return CallbackBase::Equals(other); | 283 return CallbackBase::Equals(other); |
286 } | 284 } |
287 | 285 |
288 R Run($for ARG , | 286 R Run($for ARG , |
289 [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]
) const { | 287 [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]
) const { |
290 PolymorphicInvoke f = | 288 PolymorphicInvoke f = |
(...skipping 16 matching lines...) Expand all Loading... |
307 | 305 |
308 ]] $$ for ARITY | 306 ]] $$ for ARITY |
309 | 307 |
310 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it | 308 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it |
311 // will be used in a lot of APIs with delayed execution. | 309 // will be used in a lot of APIs with delayed execution. |
312 typedef Callback<void(void)> Closure; | 310 typedef Callback<void(void)> Closure; |
313 | 311 |
314 } // namespace base | 312 } // namespace base |
315 | 313 |
316 #endif // BASE_CALLBACK_H | 314 #endif // BASE_CALLBACK_H |
OLD | NEW |