| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // ============================================================================ | 5 // ============================================================================ |
| 6 // **************************************************************************** | 6 // **************************************************************************** |
| 7 // * THIS HEADER IS DEPRECATED, SEE base/callback.h FOR NEW IMPLEMENTATION * | 7 // * THIS HEADER IS DEPRECATED, SEE base/callback.h FOR NEW IMPLEMENTATION * |
| 8 // **************************************************************************** | 8 // **************************************************************************** |
| 9 // ============================================================================ | 9 // ============================================================================ |
| 10 // ============================================================================ | 10 // ============================================================================ |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 // | 320 // |
| 321 // This is different from DISALLOW_COPY_AND_ASSIGN which is declared inside the | 321 // This is different from DISALLOW_COPY_AND_ASSIGN which is declared inside the |
| 322 // class. | 322 // class. |
| 323 #define DISABLE_RUNNABLE_METHOD_REFCOUNT(TypeName) \ | 323 #define DISABLE_RUNNABLE_METHOD_REFCOUNT(TypeName) \ |
| 324 template <> \ | 324 template <> \ |
| 325 struct RunnableMethodTraits<TypeName> { \ | 325 struct RunnableMethodTraits<TypeName> { \ |
| 326 void RetainCallee(TypeName* manager) {} \ | 326 void RetainCallee(TypeName* manager) {} \ |
| 327 void ReleaseCallee(TypeName* manager) {} \ | 327 void ReleaseCallee(TypeName* manager) {} \ |
| 328 } | 328 } |
| 329 | 329 |
| 330 // RunnableMethod and RunnableFunction ----------------------------------------- | 330 // RunnableMethod -------------------------------------------------------------- |
| 331 // | 331 // |
| 332 // Runnable methods are a type of task that call a function on an object when | 332 // Runnable methods are a type of task that call a function on an object when |
| 333 // they are run. We implement both an object and a set of NewRunnableMethod and | 333 // they are run. We implement both an object and a set of NewRunnableMethod |
| 334 // NewRunnableFunction functions for convenience. These functions are | 334 // functions for convenience. These functions are overloaded and will infer the |
| 335 // overloaded and will infer the template types, simplifying calling code. | 335 // template types, simplifying calling code. |
| 336 // | 336 // |
| 337 // The template definitions all use the following names: | 337 // The template definitions all use the following names: |
| 338 // T - the class type of the object you're supplying | 338 // T - the class type of the object you're supplying |
| 339 // this is not needed for the Static version of the call | 339 // this is not needed for the Static version of the call |
| 340 // Method/Function - the signature of a pointer to the method or function you | 340 // Method/Function - the signature of a pointer to the method or function you |
| 341 // want to call | 341 // want to call |
| 342 // Param - the parameter(s) to the method, possibly packed as a Tuple | 342 // Param - the parameter(s) to the method, possibly packed as a Tuple |
| 343 // A - the first parameter (if any) to the method | 343 // A - the first parameter (if any) to the method |
| 344 // B - the second parameter (if any) to the method | 344 // B - the second parameter (if any) to the method |
| 345 // | 345 // |
| 346 // Put these all together and you get an object that can call a method whose | 346 // Put these all together and you get an object that can call a method whose |
| 347 // signature is: | 347 // signature is: |
| 348 // R T::MyFunction([A[, B]]) | 348 // R T::MyFunction([A[, B]]) |
| 349 // | 349 // |
| 350 // Usage: | 350 // Usage: |
| 351 // PostTask(FROM_HERE, NewRunnableMethod(object, &Object::method[, a[, b]]) | 351 // PostTask(FROM_HERE, NewRunnableMethod(object, &Object::method[, a[, b]]) |
| 352 // PostTask(FROM_HERE, NewRunnableFunction(&function[, a[, b]]) | |
| 353 | 352 |
| 354 // RunnableMethod and NewRunnableMethod implementation ------------------------- | 353 // RunnableMethod and NewRunnableMethod implementation ------------------------- |
| 355 | 354 |
| 356 template <class T, class Method, class Params> | 355 template <class T, class Method, class Params> |
| 357 class RunnableMethod : public CancelableTask { | 356 class RunnableMethod : public CancelableTask { |
| 358 public: | 357 public: |
| 359 RunnableMethod(T* obj, Method meth, const Params& params) | 358 RunnableMethod(T* obj, Method meth, const Params& params) |
| 360 : obj_(obj), meth_(meth), params_(params) { | 359 : obj_(obj), meth_(meth), params_(params) { |
| 361 traits_.RetainCallee(obj_); | 360 traits_.RetainCallee(obj_); |
| 362 COMPILE_ASSERT( | 361 COMPILE_ASSERT( |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 const F& f, const G& g, const H& h) { | 473 const F& f, const G& g, const H& h) { |
| 475 return new RunnableMethod<T, | 474 return new RunnableMethod<T, |
| 476 Method, | 475 Method, |
| 477 Tuple8<A, B, C, D, E, F, G, H> >(object, | 476 Tuple8<A, B, C, D, E, F, G, H> >(object, |
| 478 method, | 477 method, |
| 479 MakeTuple(a, b, c, | 478 MakeTuple(a, b, c, |
| 480 d, e, f, | 479 d, e, f, |
| 481 g, h)); | 480 g, h)); |
| 482 } | 481 } |
| 483 | 482 |
| 484 // RunnableFunction and NewRunnableFunction implementation --------------------- | |
| 485 | |
| 486 template <class Function, class Params> | |
| 487 class RunnableFunction : public Task { | |
| 488 public: | |
| 489 RunnableFunction(Function function, const Params& params) | |
| 490 : function_(function), params_(params) { | |
| 491 COMPILE_ASSERT( | |
| 492 (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), | |
| 493 badrunnablefunctionparams); | |
| 494 } | |
| 495 | |
| 496 ~RunnableFunction() { | |
| 497 function_ = reinterpret_cast<Function>(base::kDeadTask); | |
| 498 } | |
| 499 | |
| 500 virtual void Run() { | |
| 501 if (function_) | |
| 502 DispatchToFunction(function_, params_); | |
| 503 } | |
| 504 | |
| 505 private: | |
| 506 Function function_; | |
| 507 Params params_; | |
| 508 }; | |
| 509 | |
| 510 template <class Function> | |
| 511 inline Task* NewRunnableFunction(Function function) { | |
| 512 return new RunnableFunction<Function, Tuple0>(function, MakeTuple()); | |
| 513 } | |
| 514 | |
| 515 template <class Function, class A> | |
| 516 inline Task* NewRunnableFunction(Function function, const A& a) { | |
| 517 return new RunnableFunction<Function, Tuple1<A> >(function, MakeTuple(a)); | |
| 518 } | |
| 519 | |
| 520 template <class Function, class A, class B> | |
| 521 inline Task* NewRunnableFunction(Function function, const A& a, const B& b) { | |
| 522 return new RunnableFunction<Function, Tuple2<A, B> >(function, | |
| 523 MakeTuple(a, b)); | |
| 524 } | |
| 525 | |
| 526 template <class Function, class A, class B, class C> | |
| 527 inline Task* NewRunnableFunction(Function function, const A& a, const B& b, | |
| 528 const C& c) { | |
| 529 return new RunnableFunction<Function, Tuple3<A, B, C> >(function, | |
| 530 MakeTuple(a, b, c)); | |
| 531 } | |
| 532 | |
| 533 template <class Function, class A, class B, class C, class D> | |
| 534 inline Task* NewRunnableFunction(Function function, const A& a, const B& b, | |
| 535 const C& c, const D& d) { | |
| 536 return new RunnableFunction<Function, Tuple4<A, B, C, D> >(function, | |
| 537 MakeTuple(a, b, | |
| 538 c, d)); | |
| 539 } | |
| 540 | |
| 541 template <class Function, class A, class B, class C, class D, class E> | |
| 542 inline Task* NewRunnableFunction(Function function, const A& a, const B& b, | |
| 543 const C& c, const D& d, const E& e) { | |
| 544 return new RunnableFunction<Function, Tuple5<A, B, C, D, E> >(function, | |
| 545 MakeTuple(a, b, | |
| 546 c, d, | |
| 547 e)); | |
| 548 } | |
| 549 | |
| 550 template <class Function, class A, class B, class C, class D, class E, | |
| 551 class F> | |
| 552 inline Task* NewRunnableFunction(Function function, const A& a, const B& b, | |
| 553 const C& c, const D& d, const E& e, | |
| 554 const F& f) { | |
| 555 return new RunnableFunction<Function, Tuple6<A, B, C, D, E, F> >(function, | |
| 556 MakeTuple(a, b, c, d, e, f)); | |
| 557 } | |
| 558 | |
| 559 template <class Function, class A, class B, class C, class D, class E, | |
| 560 class F, class G> | |
| 561 inline Task* NewRunnableFunction(Function function, const A& a, const B& b, | |
| 562 const C& c, const D& d, const E& e, const F& f, | |
| 563 const G& g) { | |
| 564 return new RunnableFunction<Function, Tuple7<A, B, C, D, E, F, G> >(function, | |
| 565 MakeTuple(a, b, c, d, e, f, g)); | |
| 566 } | |
| 567 | |
| 568 template <class Function, class A, class B, class C, class D, class E, | |
| 569 class F, class G, class H> | |
| 570 inline Task* NewRunnableFunction(Function function, const A& a, const B& b, | |
| 571 const C& c, const D& d, const E& e, const F& f, | |
| 572 const G& g, const H& h) { | |
| 573 return new RunnableFunction<Function, Tuple8<A, B, C, D, E, F, G, H> >( | |
| 574 function, MakeTuple(a, b, c, d, e, f, g, h)); | |
| 575 } | |
| 576 | |
| 577 namespace base { | 483 namespace base { |
| 578 | 484 |
| 579 // ScopedTaskRunner is akin to scoped_ptr for Tasks. It ensures that the Task | 485 // ScopedTaskRunner is akin to scoped_ptr for Tasks. It ensures that the Task |
| 580 // is executed and deleted no matter how the current scope exits. | 486 // is executed and deleted no matter how the current scope exits. |
| 581 class BASE_EXPORT ScopedTaskRunner { | 487 class BASE_EXPORT ScopedTaskRunner { |
| 582 public: | 488 public: |
| 583 // Takes ownership of the task. | 489 // Takes ownership of the task. |
| 584 explicit ScopedTaskRunner(Task* task); | 490 explicit ScopedTaskRunner(Task* task); |
| 585 ~ScopedTaskRunner(); | 491 ~ScopedTaskRunner(); |
| 586 | 492 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 Task* task_; | 545 Task* task_; |
| 640 bool* should_leak_task_; | 546 bool* should_leak_task_; |
| 641 static bool kTaskLeakingDefault; | 547 static bool kTaskLeakingDefault; |
| 642 }; | 548 }; |
| 643 | 549 |
| 644 } // namespace subtle | 550 } // namespace subtle |
| 645 | 551 |
| 646 } // namespace base | 552 } // namespace base |
| 647 | 553 |
| 648 #endif // BASE_TASK_H_ | 554 #endif // BASE_TASK_H_ |
| OLD | NEW |