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 // Some basic utilities for aiding in the management of Tasks and Callbacks. | 5 // Some basic utilities for aiding in the management of Tasks and Callbacks. |
6 // | 6 // |
7 // AutoCallbackRunner is akin to scoped_ptr for callbacks. It is useful for | 7 // AutoTaskRunner, and its brother AutoCallbackRunner are the scoped_ptr |
8 // ensuring a callback is executed and delete in the face of multiple return | 8 // equivalents for callbacks. They are useful for ensuring a callback is |
9 // points in a function. | 9 // executed and delete in the face of multiple return points in a function. |
10 // | 10 // |
11 // TaskToCallbackAdapter converts a Task to a Callback0::Type since the two type | 11 // TaskToCallbackAdapter converts a Task to a Callback0::Type since the two type |
12 // hierarchies are strangely separate. | 12 // hierarchies are strangely separate. |
13 // | 13 // |
14 // CleanupCallback wraps another Callback and provides the ability to register | 14 // CleanupCallback wraps another Callback and provides the ability to register |
15 // objects for deletion as well as cleanup tasks that will be run on the | 15 // objects for deletion as well as cleanup tasks that will be run on the |
16 // callback's destruction. The deletion and cleanup tasks will be run on | 16 // callback's destruction. The deletion and cleanup tasks will be run on |
17 // whatever thread the CleanupCallback is destroyed in. | 17 // whatever thread the CleanupCallback is destroyed in. |
18 | 18 |
19 #ifndef MEDIA_BASE_CALLBACK_ | 19 #ifndef MEDIA_BASE_CALLBACK_ |
20 #define MEDIA_BASE_CALLBACK_ | 20 #define MEDIA_BASE_CALLBACK_ |
21 | 21 |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "base/callback_old.h" | 24 #include "base/callback_old.h" |
25 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
26 #include "base/task.h" | 26 #include "base/task.h" |
27 | 27 |
28 namespace media { | 28 namespace media { |
29 | 29 |
| 30 class AutoTaskRunner { |
| 31 public: |
| 32 // Takes ownership of the task. |
| 33 explicit AutoTaskRunner(Task* task) |
| 34 : task_(task) { |
| 35 } |
| 36 |
| 37 ~AutoTaskRunner(); |
| 38 |
| 39 Task* release() { return task_.release(); } |
| 40 |
| 41 private: |
| 42 scoped_ptr<Task> task_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(AutoTaskRunner); |
| 45 }; |
| 46 |
30 class AutoCallbackRunner { | 47 class AutoCallbackRunner { |
31 public: | 48 public: |
32 // Takes ownership of the callback. | 49 // Takes ownership of the callback. |
33 explicit AutoCallbackRunner(Callback0::Type* callback) | 50 explicit AutoCallbackRunner(Callback0::Type* callback) |
34 : callback_(callback) { | 51 : callback_(callback) { |
35 } | 52 } |
36 | 53 |
37 ~AutoCallbackRunner(); | 54 ~AutoCallbackRunner(); |
38 | 55 |
39 Callback0::Type* release() { return callback_.release(); } | 56 Callback0::Type* release() { return callback_.release(); } |
(...skipping 13 matching lines...) Expand all Loading... |
53 virtual void RunWithParams(const Tuple0& params); | 70 virtual void RunWithParams(const Tuple0& params); |
54 | 71 |
55 private: | 72 private: |
56 TaskToCallbackAdapter(Task* task); | 73 TaskToCallbackAdapter(Task* task); |
57 | 74 |
58 scoped_ptr<Task> task_; | 75 scoped_ptr<Task> task_; |
59 | 76 |
60 DISALLOW_COPY_AND_ASSIGN(TaskToCallbackAdapter); | 77 DISALLOW_COPY_AND_ASSIGN(TaskToCallbackAdapter); |
61 }; | 78 }; |
62 | 79 |
| 80 template <typename CallbackType> |
| 81 class CleanupCallback : public CallbackType { |
| 82 public: |
| 83 explicit CleanupCallback(CallbackType* callback) : callback_(callback) {} |
| 84 |
| 85 virtual ~CleanupCallback() { |
| 86 for (size_t i = 0; i < run_when_done_.size(); i++) { |
| 87 run_when_done_[i]->Run(); |
| 88 delete run_when_done_[i]; |
| 89 } |
| 90 } |
| 91 |
| 92 virtual void RunWithParams(const typename CallbackType::TupleType& params) { |
| 93 callback_->RunWithParams(params); |
| 94 } |
| 95 |
| 96 template <typename T> |
| 97 void DeleteWhenDone(T* ptr) { |
| 98 RunWhenDone(new DeleteTask<T>(ptr)); |
| 99 } |
| 100 |
| 101 void RunWhenDone(Task* ptr) { |
| 102 run_when_done_.push_back(ptr); |
| 103 } |
| 104 |
| 105 private: |
| 106 scoped_ptr<CallbackType> callback_; |
| 107 std::vector<Task*> run_when_done_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(CleanupCallback); |
| 110 }; |
| 111 |
63 } // namespace media | 112 } // namespace media |
64 | 113 |
65 #endif // MEDIA_BASE_CALLBACK_ | 114 #endif // MEDIA_BASE_CALLBACK_ |
OLD | NEW |