OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_CALLBACK_H_ | 5 #ifndef BASE_CALLBACK_H_ |
6 #define BASE_CALLBACK_H_ | 6 #define BASE_CALLBACK_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/callback_internal.h" | 9 #include "base/callback_internal.h" |
10 #include "base/template_util.h" | 10 #include "base/template_util.h" |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 // | 334 // |
335 // | 335 // |
336 // MISSING FUNCTIONALITY | 336 // MISSING FUNCTIONALITY |
337 // - Invoking the return of Bind. Bind(&foo).Run() does not work; | 337 // - Invoking the return of Bind. Bind(&foo).Run() does not work; |
338 // - Binding arrays to functions that take a non-const pointer. | 338 // - Binding arrays to functions that take a non-const pointer. |
339 // Example: | 339 // Example: |
340 // void Foo(const char* ptr); | 340 // void Foo(const char* ptr); |
341 // void Bar(char* ptr); | 341 // void Bar(char* ptr); |
342 // Bind(&Foo, "test"); | 342 // Bind(&Foo, "test"); |
343 // Bind(&Bar, "test"); // This fails because ptr is not const. | 343 // Bind(&Bar, "test"); // This fails because ptr is not const. |
344 | |
345 namespace base { | |
346 | |
347 // First, we forward declare the Callback class template. This informs the | |
348 // compiler that the template only has 1 type parameter which is the function | |
349 // signature that the Callback is representing. | |
350 // | |
351 // After this, create template specializations for 0-7 parameters. Note that | |
352 // even though the template typelist grows, the specialization still | |
353 // only has one type: the function signature. | |
354 // | 344 // |
355 // If you are thinking of forward declaring Callback in your own header file, | 345 // If you are thinking of forward declaring Callback in your own header file, |
356 // please include "base/callback_forward.h" instead. | 346 // please include "base/callback_forward.h" instead. |
357 | 347 |
| 348 namespace base { |
358 namespace internal { | 349 namespace internal { |
359 template <typename Runnable, typename RunType, typename... BoundArgsType> | 350 template <typename Runnable, typename RunType, typename... BoundArgsType> |
360 struct BindState; | 351 struct BindState; |
361 } // namespace internal | 352 } // namespace internal |
362 | 353 |
363 template <typename R, typename... Args> | 354 template <typename R, typename... Args, internal::CopyMode copy_mode> |
364 class Callback<R(Args...)> : public internal::CallbackBase { | 355 class Callback<R(Args...), copy_mode> |
| 356 : public internal::CallbackBase<copy_mode> { |
365 public: | 357 public: |
366 // MSVC 2013 doesn't support Type Alias of function types. | 358 // MSVC 2013 doesn't support Type Alias of function types. |
367 // Revisit this after we update it to newer version. | 359 // Revisit this after we update it to newer version. |
368 typedef R RunType(Args...); | 360 typedef R RunType(Args...); |
369 | 361 |
370 Callback() : CallbackBase(nullptr) { } | 362 Callback() : internal::CallbackBase<copy_mode>(nullptr) {} |
371 | 363 |
372 template <typename Runnable, typename BindRunType, typename... BoundArgsType> | 364 template <typename Runnable, typename BindRunType, typename... BoundArgs> |
373 explicit Callback( | 365 explicit Callback( |
374 internal::BindState<Runnable, BindRunType, BoundArgsType...>* bind_state) | 366 internal::BindState<Runnable, BindRunType, BoundArgs...>* bind_state) |
375 : CallbackBase(bind_state) { | 367 : internal::CallbackBase<copy_mode>(bind_state) { |
376 // Force the assignment to a local variable of PolymorphicInvoke | 368 // Force the assignment to a local variable of PolymorphicInvoke |
377 // so the compiler will typecheck that the passed in Run() method has | 369 // so the compiler will typecheck that the passed in Run() method has |
378 // the correct type. | 370 // the correct type. |
379 PolymorphicInvoke invoke_func = | 371 PolymorphicInvoke invoke_func = |
380 &internal::BindState<Runnable, BindRunType, BoundArgsType...> | 372 &internal::BindState<Runnable, BindRunType, BoundArgs...> |
381 ::InvokerType::Run; | 373 ::InvokerType::Run; |
382 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); | 374 using InvokeFuncStorage = |
| 375 typename internal::CallbackBase<copy_mode>::InvokeFuncStorage; |
| 376 this->polymorphic_invoke_ = |
| 377 reinterpret_cast<InvokeFuncStorage>(invoke_func); |
383 } | 378 } |
384 | 379 |
385 bool Equals(const Callback& other) const { | 380 bool Equals(const Callback& other) const { |
386 return CallbackBase::Equals(other); | 381 return this->EqualsInternal(other); |
387 } | 382 } |
388 | 383 |
389 // Run() makes an extra copy compared to directly calling the bound function | 384 // Run() makes an extra copy compared to directly calling the bound function |
390 // if an argument is passed-by-value and is copyable-but-not-movable: | 385 // if an argument is passed-by-value and is copyable-but-not-movable: |
391 // i.e. below copies CopyableNonMovableType twice. | 386 // i.e. below copies CopyableNonMovableType twice. |
392 // void F(CopyableNonMovableType) {} | 387 // void F(CopyableNonMovableType) {} |
393 // Bind(&F).Run(CopyableNonMovableType()); | 388 // Bind(&F).Run(CopyableNonMovableType()); |
394 // | 389 // |
395 // We can not fully apply Perfect Forwarding idiom to the callchain from | 390 // We can not fully apply Perfect Forwarding idiom to the callchain from |
396 // Callback::Run() to the target function. Perfect Forwarding requires | 391 // Callback::Run() to the target function. Perfect Forwarding requires |
397 // knowing how the caller will pass the arguments. However, the signature of | 392 // knowing how the caller will pass the arguments. However, the signature of |
398 // InvokerType::Run() needs to be fixed in the callback constructor, so Run() | 393 // InvokerType::Run() needs to be fixed in the callback constructor, so Run() |
399 // cannot template its arguments based on how it's called. | 394 // cannot template its arguments based on how it's called. |
400 R Run(Args... args) const { | 395 R Run(Args... args) const { |
401 PolymorphicInvoke f = | 396 PolymorphicInvoke f = |
402 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); | 397 reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke_); |
403 return f(bind_state_.get(), std::forward<Args>(args)...); | 398 return f(this->bind_state_.get(), std::forward<Args>(args)...); |
404 } | 399 } |
405 | 400 |
406 private: | 401 private: |
407 using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...); | 402 using PolymorphicInvoke = R (*)(internal::BindStateBase*, Args&&...); |
408 }; | 403 }; |
409 | 404 |
410 } // namespace base | 405 } // namespace base |
411 | 406 |
412 #endif // BASE_CALLBACK_H_ | 407 #endif // BASE_CALLBACK_H_ |
OLD | NEW |