| 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 $var MAX_ARITY = 6 | 8 $var MAX_ARITY = 6 |
| 9 | 9 |
| 10 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 10 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // HOW THE IMPLEMENTATION WORKS: | 122 // HOW THE IMPLEMENTATION WORKS: |
| 123 // | 123 // |
| 124 // There are three main components to the system: | 124 // There are three main components to the system: |
| 125 // 1) The Callback classes. | 125 // 1) The Callback classes. |
| 126 // 2) The Bind() functions. | 126 // 2) The Bind() functions. |
| 127 // 3) The arguments wrappers (eg., Unretained() and ConstRef()). | 127 // 3) The arguments wrappers (eg., Unretained() and ConstRef()). |
| 128 // | 128 // |
| 129 // The Callback classes represent a generic function pointer. Internally, | 129 // The Callback classes represent a generic function pointer. Internally, |
| 130 // it stores a refcounted piece of state that represents the target function | 130 // it stores a refcounted piece of state that represents the target function |
| 131 // and all its bound parameters. Each Callback specialization has a templated | 131 // and all its bound parameters. Each Callback specialization has a templated |
| 132 // constructor that takes an InvokerStorageHolder<> object. In the context of | 132 // constructor that takes an BindStateHolder<> object. In the context of |
| 133 // the constructor, the static type of this InvokerStorageHolder<> object | 133 // the constructor, the static type of this BindStateHolder<> object |
| 134 // uniquely identifies the function it is representing, all its bound | 134 // uniquely identifies the function it is representing, all its bound |
| 135 // parameters, and a DoInvoke() that is capable of invoking the target. | 135 // parameters, and a DoInvoke() that is capable of invoking the target. |
| 136 // | 136 // |
| 137 // Callback's constructor is takes the InvokerStorageHolder<> that has the | 137 // Callback's constructor is takes the BindStateHolder<> that has the |
| 138 // full static type and erases the target function type, and the bound | 138 // full static type and erases the target function type, and the bound |
| 139 // parameters. It does this by storing a pointer to the specific DoInvoke() | 139 // parameters. It does this by storing a pointer to the specific DoInvoke() |
| 140 // function, and upcasting the state of InvokerStorageHolder<> to a | 140 // function, and upcasting the state of BindStateHolder<> to a |
| 141 // InvokerStorageBase. This is safe as long as this InvokerStorageBase pointer | 141 // BindStateBase. This is safe as long as this BindStateBase pointer |
| 142 // is only used with the stored DoInvoke() pointer. | 142 // is only used with the stored DoInvoke() pointer. |
| 143 // | 143 // |
| 144 // To create InvokerStorageHolder<> objects, we use the Bind() functions. | 144 // To create BindStateHolder<> objects, we use the Bind() functions. |
| 145 // These functions, along with a set of internal templates, are reponsible for | 145 // These functions, along with a set of internal templates, are reponsible for |
| 146 // | 146 // |
| 147 // - Unwrapping the function signature into return type, and parameters | 147 // - Unwrapping the function signature into return type, and parameters |
| 148 // - Determining the number of parameters that are bound | 148 // - Determining the number of parameters that are bound |
| 149 // - Creating the storage for the bound parameters | 149 // - Creating the storage for the bound parameters |
| 150 // - Performing compile-time asserts to avoid error-prone behavior | 150 // - Performing compile-time asserts to avoid error-prone behavior |
| 151 // - Returning an InvokerStorageHolder<> with an DoInvoke() that has an arity | 151 // - Returning an BindStateHolder<> with an DoInvoke() that has an arity |
| 152 // matching the number of unbound parameters, and knows the correct | 152 // matching the number of unbound parameters, and knows the correct |
| 153 // refcounting semantics for the target object if we are binding a class | 153 // refcounting semantics for the target object if we are binding a class |
| 154 // method. | 154 // method. |
| 155 // | 155 // |
| 156 // The Bind functions do the above using type-inference, and template | 156 // The Bind functions do the above using type-inference, and template |
| 157 // specializations. | 157 // specializations. |
| 158 // | 158 // |
| 159 // By default Bind() will store copies of all bound parameters, and attempt | 159 // By default Bind() will store copies of all bound parameters, and attempt |
| 160 // to refcount a target object if the function being bound is a class method. | 160 // to refcount a target object if the function being bound is a class method. |
| 161 // | 161 // |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 243 |
| 244 $if ARITY == 0 [[ | 244 $if ARITY == 0 [[ |
| 245 template <typename R> | 245 template <typename R> |
| 246 class Callback<R(void)> : public internal::CallbackBase { | 246 class Callback<R(void)> : public internal::CallbackBase { |
| 247 ]] $else [[ | 247 ]] $else [[ |
| 248 template <typename R, $for ARG , [[typename A$(ARG)]]> | 248 template <typename R, $for ARG , [[typename A$(ARG)]]> |
| 249 class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase { | 249 class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase { |
| 250 ]] | 250 ]] |
| 251 | 251 |
| 252 public: | 252 public: |
| 253 typedef R(*PolymorphicInvoke)( | 253 typedef R(RunType)($for ARG , [[A$(ARG)]]); |
| 254 internal::InvokerStorageBase*[[]] | |
| 255 $if ARITY != 0 [[, ]] | |
| 256 $for ARG , [[typename internal::ParamTraits<A$(ARG)>::ForwardType]]); | |
| 257 | 254 |
| 258 Callback() : CallbackBase(NULL, NULL) { } | 255 Callback() : CallbackBase(NULL, NULL) { } |
| 259 | 256 |
| 260 // We pass InvokerStorageHolder by const ref to avoid incurring an | 257 // We pass BindStateHolder by const ref to avoid incurring an |
| 261 // unnecessary AddRef/Unref pair even though we will modify the object. | 258 // unnecessary AddRef/Unref pair even though we will modify the object. |
| 262 // We cannot use a normal reference because the compiler will warn | 259 // We cannot use a normal reference because the compiler will warn |
| 263 // since this is often used on a return value, which is a temporary. | 260 // since this is often used on a return value, which is a temporary. |
| 264 // | 261 // |
| 265 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT | 262 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 266 // return the exact Callback<> type. See base/bind.h for details. | 263 // return the exact Callback<> type. See base/bind.h for details. |
| 267 template <typename T> | 264 template <typename T> |
| 268 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) | 265 Callback(const internal::BindStateHolder<T>& bind_state_holder) |
| 269 : CallbackBase( | 266 : CallbackBase(NULL, &bind_state_holder.bind_state_) { |
| 270 reinterpret_cast<InvokeFuncStorage>(&T::Invoker::DoInvoke), | 267 // Force the assignment to a location variable of PolymorphicInvoke |
| 271 &invoker_holder.invoker_storage_) { | 268 // so the compiler will typecheck that the passed in Run() method has |
| 272 COMPILE_ASSERT((is_same<PolymorphicInvoke, | 269 // the correct type. |
| 273 typename T::Invoker::DoInvokeType>::value), | 270 PolymorphicInvoke invoke_func = &T::InvokerType::Run; |
| 274 callback_type_does_not_match_bind_result); | 271 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
| 275 } | 272 } |
| 276 | 273 |
| 277 bool Equals(const Callback& other) const { | 274 bool Equals(const Callback& other) const { |
| 278 return CallbackBase::Equals(other); | 275 return CallbackBase::Equals(other); |
| 279 } | 276 } |
| 280 | 277 |
| 281 R Run($for ARG , | 278 R Run($for ARG , |
| 282 [[typename internal::ParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) const
{ | 279 [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]
) const { |
| 283 PolymorphicInvoke f = | 280 PolymorphicInvoke f = |
| 284 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); | 281 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
| 285 | 282 |
| 286 return f(invoker_storage_.get()[[]] | 283 return f(bind_state_.get()[[]] |
| 287 $if ARITY != 0 [[, ]] | 284 $if ARITY != 0 [[, ]] |
| 288 $for ARG , | 285 $for ARG , |
| 289 [[a$(ARG)]]); | 286 [[a$(ARG)]]); |
| 290 } | 287 } |
| 288 |
| 289 private: |
| 290 typedef R(*PolymorphicInvoke)( |
| 291 internal::BindStateBase*[[]] |
| 292 $if ARITY != 0 [[, ]] |
| 293 $for ARG , [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType]]); |
| 294 |
| 291 }; | 295 }; |
| 292 | 296 |
| 293 | 297 |
| 294 ]] $$ for ARITY | 298 ]] $$ for ARITY |
| 295 | 299 |
| 296 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it | 300 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it |
| 297 // will be used in a lot of APIs with delayed execution. | 301 // will be used in a lot of APIs with delayed execution. |
| 298 typedef Callback<void(void)> Closure; | 302 typedef Callback<void(void)> Closure; |
| 299 | 303 |
| 300 } // namespace base | 304 } // namespace base |
| 301 | 305 |
| 302 #endif // BASE_CALLBACK_H | 306 #endif // BASE_CALLBACK_H |
| OLD | NEW |