Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: base/callback.h

Issue 6507029: Callback API Change: is_null, Reset, and Equals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Can we color it "is_null()"? Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // This file was GENERATED by command: 1 // This file was GENERATED by command:
2 // pump.py callback.h.pump 2 // pump.py callback.h.pump
3 // DO NOT EDIT BY HAND!!! 3 // DO NOT EDIT BY HAND!!!
4 4
5 5
6 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 6 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be 7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. 8 // found in the LICENSE file.
9 9
10 #ifndef BASE_CALLBACK_H_ 10 #ifndef BASE_CALLBACK_H_
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 // - Invoking the return of Bind. Bind(&foo).Run() does not work; 205 // - Invoking the return of Bind. Bind(&foo).Run() does not work;
206 // - Binding arrays to functions that take a non-const pointer. 206 // - Binding arrays to functions that take a non-const pointer.
207 // Example: 207 // Example:
208 // void Foo(const char* ptr); 208 // void Foo(const char* ptr);
209 // void Bar(char* ptr); 209 // void Bar(char* ptr);
210 // Bind(&Foo, "test"); 210 // Bind(&Foo, "test");
211 // Bind(&Bar, "test"); // This fails because ptr is not const. 211 // Bind(&Bar, "test"); // This fails because ptr is not const.
212 212
213 namespace base { 213 namespace base {
214 214
215 namespace internal {
216
217 // Holds the methods that don't require specialization to reduce template bloat.
218 class CallbackBase {
219 public:
220 // Returns true if Callback is null (doesn't refer to anything).
221 bool is_null() const {
222 return invoker_storage_.get() == NULL;
223 }
224
225 // Returns the Callback into an uninitalized state.
226 void Reset() {
227 invoker_storage_ = NULL;
228 polymorphic_invoke_ = NULL;
229 }
230
231 bool Equals(const CallbackBase& other) const {
232 return invoker_storage_.get() == other.invoker_storage_.get() &&
233 polymorphic_invoke_ == other.polymorphic_invoke_;
234 }
235
236 protected:
237 // In C++, it is safe to cast function pointers to function pointers of
238 // another type. It is not okay to use void*. We create a InvokeFuncStorage
239 // that that can store our function pointer, and then cast it back to
240 // the original type on usage.
241 typedef void(*InvokeFuncStorage)(void);
242
243 CallbackBase(InvokeFuncStorage polymorphic_invoke,
244 scoped_refptr<InvokerStorageBase>* invoker_storage)
245 : polymorphic_invoke_(polymorphic_invoke) {
246 if (invoker_storage) {
247 invoker_storage_.swap(*invoker_storage);
248 }
249 }
250
251 scoped_refptr<InvokerStorageBase> invoker_storage_;
252 InvokeFuncStorage polymorphic_invoke_;
253 };
254
255 } // namespace internal
256
257
215 // First, we forward declare the Callback class template. This informs the 258 // First, we forward declare the Callback class template. This informs the
216 // compiler that the template only has 1 type parameter which is the function 259 // compiler that the template only has 1 type parameter which is the function
217 // signature that the Callback is representing. 260 // signature that the Callback is representing.
218 // 261 //
219 // After this, create template specializations for 0-6 parameters. Note that 262 // After this, create template specializations for 0-6 parameters. Note that
220 // even though the template typelist grows, the specialization still 263 // even though the template typelist grows, the specialization still
221 // only has one type: the function signature. 264 // only has one type: the function signature.
222 template <typename Sig> 265 template <typename Sig>
223 class Callback; 266 class Callback;
224
225 template <typename R> 267 template <typename R>
226 class Callback<R(void)> { 268 class Callback<R(void)> : public internal::CallbackBase {
227 public: 269 public:
228 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*); 270 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*);
229 271
230 Callback() : polymorphic_invoke_(NULL) { } 272 Callback() : CallbackBase(NULL, NULL) { }
231 273
232 // We pass InvokerStorageHolder by const ref to avoid incurring an 274 // We pass InvokerStorageHolder by const ref to avoid incurring an
233 // unnecessary AddRef/Unref pair even though we will modify the object. 275 // unnecessary AddRef/Unref pair even though we will modify the object.
234 // We cannot use a normal reference because the compiler will warn 276 // We cannot use a normal reference because the compiler will warn
235 // since this is often used on a return value, which is a temporary. 277 // since this is often used on a return value, which is a temporary.
236 // 278 //
237 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 279 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
238 // return the exact Callback<> type. See base/bind.h for details. 280 // return the exact Callback<> type. See base/bind.h for details.
239 template <typename T> 281 template <typename T>
240 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 282 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
241 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 283 : CallbackBase(
242 invoker_storage_.swap(invoker_holder.invoker_storage_); 284 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
285 &invoker_holder.invoker_storage_) {
243 } 286 }
244 287
245 R Run(void) const { 288 R Run() const {
246 return polymorphic_invoke_(invoker_storage_.get()); 289 PolymorphicInvoke f =
290 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
291
292 return f(invoker_storage_.get());
247 } 293 }
248
249 private:
250 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
251 PolymorphicInvoke polymorphic_invoke_;
252 }; 294 };
253 295
254 template <typename R, typename A1> 296 template <typename R, typename A1>
255 class Callback<R(A1)> { 297 class Callback<R(A1)> : public internal::CallbackBase {
256 public: 298 public:
257 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&); 299 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&);
258 300
259 Callback() : polymorphic_invoke_(NULL) { } 301 Callback() : CallbackBase(NULL, NULL) { }
260 302
261 // We pass InvokerStorageHolder by const ref to avoid incurring an 303 // We pass InvokerStorageHolder by const ref to avoid incurring an
262 // unnecessary AddRef/Unref pair even though we will modify the object. 304 // unnecessary AddRef/Unref pair even though we will modify the object.
263 // We cannot use a normal reference because the compiler will warn 305 // We cannot use a normal reference because the compiler will warn
264 // since this is often used on a return value, which is a temporary. 306 // since this is often used on a return value, which is a temporary.
265 // 307 //
266 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 308 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
267 // return the exact Callback<> type. See base/bind.h for details. 309 // return the exact Callback<> type. See base/bind.h for details.
268 template <typename T> 310 template <typename T>
269 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 311 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
270 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 312 : CallbackBase(
271 invoker_storage_.swap(invoker_holder.invoker_storage_); 313 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
314 &invoker_holder.invoker_storage_) {
272 } 315 }
273 316
274 R Run(const A1& a1) const { 317 R Run(const A1& a1) const {
275 return polymorphic_invoke_(invoker_storage_.get(), a1); 318 PolymorphicInvoke f =
319 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
320
321 return f(invoker_storage_.get(), a1);
276 } 322 }
277
278 private:
279 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
280 PolymorphicInvoke polymorphic_invoke_;
281 }; 323 };
282 324
283 template <typename R, typename A1, typename A2> 325 template <typename R, typename A1, typename A2>
284 class Callback<R(A1, A2)> { 326 class Callback<R(A1, A2)> : public internal::CallbackBase {
285 public: 327 public:
286 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, 328 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&,
287 const A2&); 329 const A2&);
288 330
289 Callback() : polymorphic_invoke_(NULL) { } 331 Callback() : CallbackBase(NULL, NULL) { }
290 332
291 // We pass InvokerStorageHolder by const ref to avoid incurring an 333 // We pass InvokerStorageHolder by const ref to avoid incurring an
292 // unnecessary AddRef/Unref pair even though we will modify the object. 334 // unnecessary AddRef/Unref pair even though we will modify the object.
293 // We cannot use a normal reference because the compiler will warn 335 // We cannot use a normal reference because the compiler will warn
294 // since this is often used on a return value, which is a temporary. 336 // since this is often used on a return value, which is a temporary.
295 // 337 //
296 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 338 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
297 // return the exact Callback<> type. See base/bind.h for details. 339 // return the exact Callback<> type. See base/bind.h for details.
298 template <typename T> 340 template <typename T>
299 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 341 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
300 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 342 : CallbackBase(
301 invoker_storage_.swap(invoker_holder.invoker_storage_); 343 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
344 &invoker_holder.invoker_storage_) {
302 } 345 }
303 346
304 R Run(const A1& a1, 347 R Run(const A1& a1,
305 const A2& a2) const { 348 const A2& a2) const {
306 return polymorphic_invoke_(invoker_storage_.get(), a1, 349 PolymorphicInvoke f =
307 a2); 350 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
351
352 return f(invoker_storage_.get(), a1,
353 a2);
308 } 354 }
309
310 private:
311 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
312 PolymorphicInvoke polymorphic_invoke_;
313 }; 355 };
314 356
315 template <typename R, typename A1, typename A2, typename A3> 357 template <typename R, typename A1, typename A2, typename A3>
316 class Callback<R(A1, A2, A3)> { 358 class Callback<R(A1, A2, A3)> : public internal::CallbackBase {
317 public: 359 public:
318 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, 360 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&,
319 const A2&, 361 const A2&,
320 const A3&); 362 const A3&);
321 363
322 Callback() : polymorphic_invoke_(NULL) { } 364 Callback() : CallbackBase(NULL, NULL) { }
323 365
324 // We pass InvokerStorageHolder by const ref to avoid incurring an 366 // We pass InvokerStorageHolder by const ref to avoid incurring an
325 // unnecessary AddRef/Unref pair even though we will modify the object. 367 // unnecessary AddRef/Unref pair even though we will modify the object.
326 // We cannot use a normal reference because the compiler will warn 368 // We cannot use a normal reference because the compiler will warn
327 // since this is often used on a return value, which is a temporary. 369 // since this is often used on a return value, which is a temporary.
328 // 370 //
329 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 371 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
330 // return the exact Callback<> type. See base/bind.h for details. 372 // return the exact Callback<> type. See base/bind.h for details.
331 template <typename T> 373 template <typename T>
332 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 374 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
333 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 375 : CallbackBase(
334 invoker_storage_.swap(invoker_holder.invoker_storage_); 376 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
377 &invoker_holder.invoker_storage_) {
335 } 378 }
336 379
337 R Run(const A1& a1, 380 R Run(const A1& a1,
338 const A2& a2, 381 const A2& a2,
339 const A3& a3) const { 382 const A3& a3) const {
340 return polymorphic_invoke_(invoker_storage_.get(), a1, 383 PolymorphicInvoke f =
341 a2, 384 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
342 a3); 385
386 return f(invoker_storage_.get(), a1,
387 a2,
388 a3);
343 } 389 }
344
345 private:
346 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
347 PolymorphicInvoke polymorphic_invoke_;
348 }; 390 };
349 391
350 template <typename R, typename A1, typename A2, typename A3, typename A4> 392 template <typename R, typename A1, typename A2, typename A3, typename A4>
351 class Callback<R(A1, A2, A3, A4)> { 393 class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase {
352 public: 394 public:
353 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, 395 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&,
354 const A2&, 396 const A2&,
355 const A3&, 397 const A3&,
356 const A4&); 398 const A4&);
357 399
358 Callback() : polymorphic_invoke_(NULL) { } 400 Callback() : CallbackBase(NULL, NULL) { }
359 401
360 // We pass InvokerStorageHolder by const ref to avoid incurring an 402 // We pass InvokerStorageHolder by const ref to avoid incurring an
361 // unnecessary AddRef/Unref pair even though we will modify the object. 403 // unnecessary AddRef/Unref pair even though we will modify the object.
362 // We cannot use a normal reference because the compiler will warn 404 // We cannot use a normal reference because the compiler will warn
363 // since this is often used on a return value, which is a temporary. 405 // since this is often used on a return value, which is a temporary.
364 // 406 //
365 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 407 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
366 // return the exact Callback<> type. See base/bind.h for details. 408 // return the exact Callback<> type. See base/bind.h for details.
367 template <typename T> 409 template <typename T>
368 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 410 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
369 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 411 : CallbackBase(
370 invoker_storage_.swap(invoker_holder.invoker_storage_); 412 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
413 &invoker_holder.invoker_storage_) {
371 } 414 }
372 415
373 R Run(const A1& a1, 416 R Run(const A1& a1,
374 const A2& a2, 417 const A2& a2,
375 const A3& a3, 418 const A3& a3,
376 const A4& a4) const { 419 const A4& a4) const {
377 return polymorphic_invoke_(invoker_storage_.get(), a1, 420 PolymorphicInvoke f =
378 a2, 421 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
379 a3, 422
380 a4); 423 return f(invoker_storage_.get(), a1,
424 a2,
425 a3,
426 a4);
381 } 427 }
382
383 private:
384 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
385 PolymorphicInvoke polymorphic_invoke_;
386 }; 428 };
387 429
388 template <typename R, typename A1, typename A2, typename A3, typename A4, 430 template <typename R, typename A1, typename A2, typename A3, typename A4,
389 typename A5> 431 typename A5>
390 class Callback<R(A1, A2, A3, A4, A5)> { 432 class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase {
391 public: 433 public:
392 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, 434 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&,
393 const A2&, 435 const A2&,
394 const A3&, 436 const A3&,
395 const A4&, 437 const A4&,
396 const A5&); 438 const A5&);
397 439
398 Callback() : polymorphic_invoke_(NULL) { } 440 Callback() : CallbackBase(NULL, NULL) { }
399 441
400 // We pass InvokerStorageHolder by const ref to avoid incurring an 442 // We pass InvokerStorageHolder by const ref to avoid incurring an
401 // unnecessary AddRef/Unref pair even though we will modify the object. 443 // unnecessary AddRef/Unref pair even though we will modify the object.
402 // We cannot use a normal reference because the compiler will warn 444 // We cannot use a normal reference because the compiler will warn
403 // since this is often used on a return value, which is a temporary. 445 // since this is often used on a return value, which is a temporary.
404 // 446 //
405 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 447 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
406 // return the exact Callback<> type. See base/bind.h for details. 448 // return the exact Callback<> type. See base/bind.h for details.
407 template <typename T> 449 template <typename T>
408 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 450 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
409 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 451 : CallbackBase(
410 invoker_storage_.swap(invoker_holder.invoker_storage_); 452 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
453 &invoker_holder.invoker_storage_) {
411 } 454 }
412 455
413 R Run(const A1& a1, 456 R Run(const A1& a1,
414 const A2& a2, 457 const A2& a2,
415 const A3& a3, 458 const A3& a3,
416 const A4& a4, 459 const A4& a4,
417 const A5& a5) const { 460 const A5& a5) const {
418 return polymorphic_invoke_(invoker_storage_.get(), a1, 461 PolymorphicInvoke f =
419 a2, 462 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
420 a3, 463
421 a4, 464 return f(invoker_storage_.get(), a1,
422 a5); 465 a2,
466 a3,
467 a4,
468 a5);
423 } 469 }
424
425 private:
426 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
427 PolymorphicInvoke polymorphic_invoke_;
428 }; 470 };
429 471
430 template <typename R, typename A1, typename A2, typename A3, typename A4, 472 template <typename R, typename A1, typename A2, typename A3, typename A4,
431 typename A5, typename A6> 473 typename A5, typename A6>
432 class Callback<R(A1, A2, A3, A4, A5, A6)> { 474 class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase {
433 public: 475 public:
434 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, 476 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&,
435 const A2&, 477 const A2&,
436 const A3&, 478 const A3&,
437 const A4&, 479 const A4&,
438 const A5&, 480 const A5&,
439 const A6&); 481 const A6&);
440 482
441 Callback() : polymorphic_invoke_(NULL) { } 483 Callback() : CallbackBase(NULL, NULL) { }
442 484
443 // We pass InvokerStorageHolder by const ref to avoid incurring an 485 // We pass InvokerStorageHolder by const ref to avoid incurring an
444 // unnecessary AddRef/Unref pair even though we will modify the object. 486 // unnecessary AddRef/Unref pair even though we will modify the object.
445 // We cannot use a normal reference because the compiler will warn 487 // We cannot use a normal reference because the compiler will warn
446 // since this is often used on a return value, which is a temporary. 488 // since this is often used on a return value, which is a temporary.
447 // 489 //
448 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 490 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
449 // return the exact Callback<> type. See base/bind.h for details. 491 // return the exact Callback<> type. See base/bind.h for details.
450 template <typename T> 492 template <typename T>
451 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 493 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
452 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 494 : CallbackBase(
453 invoker_storage_.swap(invoker_holder.invoker_storage_); 495 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke),
496 &invoker_holder.invoker_storage_) {
454 } 497 }
455 498
456 R Run(const A1& a1, 499 R Run(const A1& a1,
457 const A2& a2, 500 const A2& a2,
458 const A3& a3, 501 const A3& a3,
459 const A4& a4, 502 const A4& a4,
460 const A5& a5, 503 const A5& a5,
461 const A6& a6) const { 504 const A6& a6) const {
462 return polymorphic_invoke_(invoker_storage_.get(), a1, 505 PolymorphicInvoke f =
463 a2, 506 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
464 a3, 507
465 a4, 508 return f(invoker_storage_.get(), a1,
466 a5, 509 a2,
467 a6); 510 a3,
511 a4,
512 a5,
513 a6);
468 } 514 }
469
470 private:
471 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
472 PolymorphicInvoke polymorphic_invoke_;
473 }; 515 };
474 516
475 517
476 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it 518 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
477 // will be used in a lot of APIs with delayed execution. 519 // will be used in a lot of APIs with delayed execution.
478 typedef Callback<void(void)> Closure; 520 typedef Callback<void(void)> Closure;
479 521
480 } // namespace base 522 } // namespace base
481 523
482 #endif // BASE_CALLBACK_H 524 #endif // BASE_CALLBACK_H
OLDNEW
« no previous file with comments | « base/bind_unittest.cc ('k') | base/callback.h.pump » ('j') | base/callback_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698