| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // AutoTaskRunner, and its brother AutoCallbackRunner are the scoped_ptr | 7 // AutoTaskRunner, and its brother AutoCallbackRunner are the scoped_ptr |
| 8 // equivalents for callbacks. They are useful for ensuring a callback is | 8 // equivalents for callbacks. They are useful for ensuring a callback is |
| 9 // executed and delete in the face of multiple return points in a function. | 9 // executed and delete in the face of multiple return points in a function. |
| 10 // | 10 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace media { | 28 namespace media { |
| 29 | 29 |
| 30 class AutoTaskRunner { | 30 class AutoTaskRunner { |
| 31 public: | 31 public: |
| 32 // Takes ownership of the task. | 32 // Takes ownership of the task. |
| 33 explicit AutoTaskRunner(Task* task) | 33 explicit AutoTaskRunner(Task* task) |
| 34 : task_(task) { | 34 : task_(task) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 ~AutoTaskRunner() { | 37 ~AutoTaskRunner(); |
| 38 if (task_.get()) { | |
| 39 task_->Run(); | |
| 40 } | |
| 41 } | |
| 42 | 38 |
| 43 Task* release() { return task_.release(); } | 39 Task* release() { return task_.release(); } |
| 44 | 40 |
| 45 private: | 41 private: |
| 46 scoped_ptr<Task> task_; | 42 scoped_ptr<Task> task_; |
| 47 | 43 |
| 48 DISALLOW_COPY_AND_ASSIGN(AutoTaskRunner); | 44 DISALLOW_COPY_AND_ASSIGN(AutoTaskRunner); |
| 49 }; | 45 }; |
| 50 | 46 |
| 51 class AutoCallbackRunner { | 47 class AutoCallbackRunner { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 64 Callback0::Type* release() { return callback_.release(); } | 60 Callback0::Type* release() { return callback_.release(); } |
| 65 | 61 |
| 66 private: | 62 private: |
| 67 scoped_ptr<Callback0::Type> callback_; | 63 scoped_ptr<Callback0::Type> callback_; |
| 68 | 64 |
| 69 DISALLOW_COPY_AND_ASSIGN(AutoCallbackRunner); | 65 DISALLOW_COPY_AND_ASSIGN(AutoCallbackRunner); |
| 70 }; | 66 }; |
| 71 | 67 |
| 72 class TaskToCallbackAdapter : public Callback0::Type { | 68 class TaskToCallbackAdapter : public Callback0::Type { |
| 73 public: | 69 public: |
| 74 static Callback0::Type* NewCallback(Task* task) { | 70 static Callback0::Type* NewCallback(Task* task); |
| 75 return new TaskToCallbackAdapter(task); | |
| 76 } | |
| 77 | 71 |
| 78 virtual ~TaskToCallbackAdapter() {} | 72 virtual ~TaskToCallbackAdapter(); |
| 79 | 73 |
| 80 virtual void RunWithParams(const Tuple0& params) { task_->Run(); } | 74 virtual void RunWithParams(const Tuple0& params); |
| 81 | 75 |
| 82 private: | 76 private: |
| 83 TaskToCallbackAdapter(Task* task) : task_(task) {} | 77 TaskToCallbackAdapter(Task* task); |
| 84 | 78 |
| 85 scoped_ptr<Task> task_; | 79 scoped_ptr<Task> task_; |
| 86 | 80 |
| 87 DISALLOW_COPY_AND_ASSIGN(TaskToCallbackAdapter); | 81 DISALLOW_COPY_AND_ASSIGN(TaskToCallbackAdapter); |
| 88 }; | 82 }; |
| 89 | 83 |
| 90 template <typename CallbackType> | 84 template <typename CallbackType> |
| 91 class CleanupCallback : public CallbackType { | 85 class CleanupCallback : public CallbackType { |
| 92 public: | 86 public: |
| 93 explicit CleanupCallback(CallbackType* callback) : callback_(callback) {} | 87 explicit CleanupCallback(CallbackType* callback) : callback_(callback) {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 115 private: | 109 private: |
| 116 scoped_ptr<CallbackType> callback_; | 110 scoped_ptr<CallbackType> callback_; |
| 117 std::vector<Task*> run_when_done_; | 111 std::vector<Task*> run_when_done_; |
| 118 | 112 |
| 119 DISALLOW_COPY_AND_ASSIGN(CleanupCallback); | 113 DISALLOW_COPY_AND_ASSIGN(CleanupCallback); |
| 120 }; | 114 }; |
| 121 | 115 |
| 122 } // namespace media | 116 } // namespace media |
| 123 | 117 |
| 124 #endif // MEDIA_BASE_CALLBACK_ | 118 #endif // MEDIA_BASE_CALLBACK_ |
| OLD | NEW |