| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 // The object will be deleted when the callback is destroyed, even if it's | 158 // The object will be deleted when the callback is destroyed, even if it's |
| 159 // not run (like if you post a task during shutdown). Potentially useful for | 159 // not run (like if you post a task during shutdown). Potentially useful for |
| 160 // "fire and forget" cases. | 160 // "fire and forget" cases. |
| 161 // | 161 // |
| 162 // IGNORING RETURN VALUES | 162 // IGNORING RETURN VALUES |
| 163 // | 163 // |
| 164 // Sometimes you want to call a function that returns a value in a callback | 164 // Sometimes you want to call a function that returns a value in a callback |
| 165 // that doesn't expect a return value. | 165 // that doesn't expect a return value. |
| 166 // | 166 // |
| 167 // int DoSomething(int arg) { cout << arg << endl; } | 167 // int DoSomething(int arg) { cout << arg << endl; } |
| 168 // base::Callback<void<int>) cb = | 168 // base::Callback<void(int)> cb = |
| 169 // base::Bind(base::IgnoreResult(&DoSomething)); | 169 // base::Bind(base::IgnoreResult(&DoSomething)); |
| 170 // | 170 // |
| 171 // | 171 // |
| 172 // ----------------------------------------------------------------------------- | 172 // ----------------------------------------------------------------------------- |
| 173 // Quick reference for binding parameters to Bind() | 173 // Quick reference for binding parameters to Bind() |
| 174 // ----------------------------------------------------------------------------- | 174 // ----------------------------------------------------------------------------- |
| 175 // | 175 // |
| 176 // Bound parameters are specified as arguments to Bind() and are passed to the | 176 // Bound parameters are specified as arguments to Bind() and are passed to the |
| 177 // function. A callback with no parameters or no unbound parameters is called a | 177 // function. A callback with no parameters or no unbound parameters is called a |
| 178 // Closure (base::Callback<void(void)> and base::Closure are the same thing). | 178 // Closure (base::Callback<void(void)> and base::Closure are the same thing). |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 // First, we forward declare the Callback class template. This informs the | 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 | 348 // compiler that the template only has 1 type parameter which is the function |
| 349 // signature that the Callback is representing. | 349 // signature that the Callback is representing. |
| 350 // | 350 // |
| 351 // After this, create template specializations for 0-7 parameters. Note that | 351 // After this, create template specializations for 0-7 parameters. Note that |
| 352 // even though the template typelist grows, the specialization still | 352 // even though the template typelist grows, the specialization still |
| 353 // only has one type: the function signature. | 353 // only has one type: the function signature. |
| 354 // | 354 // |
| 355 // If you are thinking of forward declaring Callback in your own header file, | 355 // If you are thinking of forward declaring Callback in your own header file, |
| 356 // please include "base/callback_forward.h" instead. | 356 // please include "base/callback_forward.h" instead. |
| 357 template <typename Sig> | |
| 358 class Callback; | |
| 359 | 357 |
| 360 namespace internal { | 358 namespace internal { |
| 361 template <typename Runnable, typename RunType, typename BoundArgsType> | 359 template <typename Runnable, typename RunType, typename... BoundArgsType> |
| 362 struct BindState; | 360 struct BindState; |
| 363 } // namespace internal | 361 } // namespace internal |
| 364 | 362 |
| 365 template <typename R, typename... Args> | 363 template <typename R, typename... Args> |
| 366 class Callback<R(Args...)> : public internal::CallbackBase { | 364 class Callback<R(Args...)> : public internal::CallbackBase { |
| 367 public: | 365 public: |
| 368 typedef R(RunType)(Args...); | 366 typedef R(RunType)(Args...); |
| 369 | 367 |
| 370 Callback() : CallbackBase(NULL) { } | 368 Callback() : CallbackBase(nullptr) { } |
| 371 | 369 |
| 372 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT | 370 template <typename Runnable, typename BindRunType, typename... BoundArgsType> |
| 373 // return the exact Callback<> type. See base/bind.h for details. | 371 explicit Callback( |
| 374 template <typename Runnable, typename BindRunType, typename BoundArgsType> | 372 internal::BindState<Runnable, BindRunType, BoundArgsType...>* bind_state) |
| 375 Callback(internal::BindState<Runnable, BindRunType, | |
| 376 BoundArgsType>* bind_state) | |
| 377 : CallbackBase(bind_state) { | 373 : CallbackBase(bind_state) { |
| 378 // Force the assignment to a local variable of PolymorphicInvoke | 374 // Force the assignment to a local variable of PolymorphicInvoke |
| 379 // so the compiler will typecheck that the passed in Run() method has | 375 // so the compiler will typecheck that the passed in Run() method has |
| 380 // the correct type. | 376 // the correct type. |
| 381 PolymorphicInvoke invoke_func = | 377 PolymorphicInvoke invoke_func = |
| 382 &internal::BindState<Runnable, BindRunType, BoundArgsType> | 378 &internal::BindState<Runnable, BindRunType, BoundArgsType...> |
| 383 ::InvokerType::Run; | 379 ::InvokerType::Run; |
| 384 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); | 380 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
| 385 } | 381 } |
| 386 | 382 |
| 387 bool Equals(const Callback& other) const { | 383 bool Equals(const Callback& other) const { |
| 388 return CallbackBase::Equals(other); | 384 return CallbackBase::Equals(other); |
| 389 } | 385 } |
| 390 | 386 |
| 391 R Run(typename internal::CallbackParamTraits<Args>::ForwardType... args) | 387 R Run(typename internal::CallbackParamTraits<Args>::ForwardType... args) |
| 392 const { | 388 const { |
| 393 PolymorphicInvoke f = | 389 PolymorphicInvoke f = |
| 394 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); | 390 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
| 395 | 391 |
| 396 return f(bind_state_.get(), internal::CallbackForward(args)...); | 392 return f(bind_state_.get(), internal::CallbackForward(args)...); |
| 397 } | 393 } |
| 398 | 394 |
| 399 private: | 395 private: |
| 400 typedef R(*PolymorphicInvoke)( | 396 typedef R(*PolymorphicInvoke)( |
| 401 internal::BindStateBase*, | 397 internal::BindStateBase*, |
| 402 typename internal::CallbackParamTraits<Args>::ForwardType...); | 398 typename internal::CallbackParamTraits<Args>::ForwardType...); |
| 403 }; | 399 }; |
| 404 | 400 |
| 405 // Syntactic sugar to make Callback<void(void)> easier to declare since it | |
| 406 // will be used in a lot of APIs with delayed execution. | |
| 407 typedef Callback<void(void)> Closure; | |
| 408 | |
| 409 } // namespace base | 401 } // namespace base |
| 410 | 402 |
| 411 #endif // BASE_CALLBACK_H_ | 403 #endif // BASE_CALLBACK_H_ |
| OLD | NEW |