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