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