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. |
11 // Use of this source code is governed by a BSD-style license that can be | 11 // Use of this source code is governed by a BSD-style license that can be |
12 // found in the LICENSE file. | 12 // found in the LICENSE file. |
13 | 13 |
14 #ifndef BASE_CALLBACK_H_ | 14 #ifndef BASE_CALLBACK_H_ |
15 #define BASE_CALLBACK_H_ | 15 #define BASE_CALLBACK_H_ |
16 #pragma once | 16 #pragma once |
17 | 17 |
18 #include "base/callback_helpers.h" | 18 #include "base/callback_internal.h" |
19 #include "base/callback_old.h" | 19 #include "base/callback_old.h" |
20 | 20 |
21 // New, super-duper, unified Callback system. This will eventually replace | 21 // New, super-duper, unified Callback system. This will eventually replace |
22 // NewRunnableMethod, NewRunnableFunction, CreateFunctor, and CreateCallback | 22 // NewRunnableMethod, NewRunnableFunction, CreateFunctor, and CreateCallback |
23 // systems currently in the Chromium code base. | 23 // systems currently in the Chromium code base. |
24 // | 24 // |
25 // WHAT IS THIS: | 25 // WHAT IS THIS: |
26 // | 26 // |
27 // The templated Callback class is a generalized function object. Together | 27 // The templated Callback class is a generalized function object. Together |
28 // with the Bind() function in bind.h, they provide a type-safe method for | 28 // with the Bind() function in bind.h, they provide a type-safe method for |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // - Invoking the return of Bind. Bind(&foo).Run() does not work; | 209 // - Invoking the return of Bind. Bind(&foo).Run() does not work; |
210 // - Binding arrays to functions that take a non-const pointer. | 210 // - Binding arrays to functions that take a non-const pointer. |
211 // Example: | 211 // Example: |
212 // void Foo(const char* ptr); | 212 // void Foo(const char* ptr); |
213 // void Bar(char* ptr); | 213 // void Bar(char* ptr); |
214 // Bind(&Foo, "test"); | 214 // Bind(&Foo, "test"); |
215 // Bind(&Bar, "test"); // This fails because ptr is not const. | 215 // Bind(&Bar, "test"); // This fails because ptr is not const. |
216 | 216 |
217 namespace base { | 217 namespace base { |
218 | 218 |
219 namespace internal { | |
220 | |
221 // Holds the methods that don't require specialization to reduce template bloat. | |
222 class CallbackBase { | |
223 public: | |
224 // Returns true if Callback is null (doesn't refer to anything). | |
225 bool is_null() const { | |
226 return invoker_storage_.get() == NULL; | |
227 } | |
228 | |
229 // Returns the Callback into an uninitalized state. | |
230 void Reset() { | |
231 invoker_storage_ = NULL; | |
232 polymorphic_invoke_ = NULL; | |
233 } | |
234 | |
235 bool Equals(const CallbackBase& other) const { | |
236 return invoker_storage_.get() == other.invoker_storage_.get() && | |
237 polymorphic_invoke_ == other.polymorphic_invoke_; | |
238 } | |
239 | |
240 protected: | |
241 // In C++, it is safe to cast function pointers to function pointers of | |
242 // another type. It is not okay to use void*. We create a InvokeFuncStorage | |
243 // that that can store our function pointer, and then cast it back to | |
244 // the original type on usage. | |
245 typedef void(*InvokeFuncStorage)(void); | |
246 | |
247 CallbackBase(InvokeFuncStorage polymorphic_invoke, | |
248 scoped_refptr<InvokerStorageBase>* invoker_storage) | |
249 : polymorphic_invoke_(polymorphic_invoke) { | |
250 if (invoker_storage) { | |
251 invoker_storage_.swap(*invoker_storage); | |
252 } | |
253 } | |
254 | |
255 scoped_refptr<InvokerStorageBase> invoker_storage_; | |
256 InvokeFuncStorage polymorphic_invoke_; | |
257 }; | |
258 | |
259 } // namespace internal | |
260 | |
261 | |
262 // First, we forward declare the Callback class template. This informs the | 219 // First, we forward declare the Callback class template. This informs the |
263 // compiler that the template only has 1 type parameter which is the function | 220 // compiler that the template only has 1 type parameter which is the function |
264 // signature that the Callback is representing. | 221 // signature that the Callback is representing. |
265 // | 222 // |
266 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No
te that | 223 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No
te that |
267 // even though the template typelist grows, the specialization still | 224 // even though the template typelist grows, the specialization still |
268 // only has one type: the function signature. | 225 // only has one type: the function signature. |
269 template <typename Sig> | 226 template <typename Sig> |
270 class Callback; | 227 class Callback; |
271 | 228 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 275 |
319 ]] $$ for ARITY | 276 ]] $$ for ARITY |
320 | 277 |
321 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it | 278 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it |
322 // will be used in a lot of APIs with delayed execution. | 279 // will be used in a lot of APIs with delayed execution. |
323 typedef Callback<void(void)> Closure; | 280 typedef Callback<void(void)> Closure; |
324 | 281 |
325 } // namespace base | 282 } // namespace base |
326 | 283 |
327 #endif // BASE_CALLBACK_H | 284 #endif // BASE_CALLBACK_H |
OLD | NEW |