| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_ | |
| 6 #define BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 | |
| 10 // TODO(akalin): Investigate whether it's possible to just have | |
| 11 // SequencedTaskRunner use these helpers (instead of MessageLoop). | |
| 12 // Then we can just move these to sequenced_task_runner.h. | |
| 13 | |
| 14 namespace tracked_objects { | |
| 15 class Location; | |
| 16 } | |
| 17 | |
| 18 namespace base { | |
| 19 | |
| 20 namespace subtle { | |
| 21 template <class T, class R> class DeleteHelperInternal; | |
| 22 template <class T, class R> class ReleaseHelperInternal; | |
| 23 } | |
| 24 | |
| 25 // Template helpers which use function indirection to erase T from the | |
| 26 // function signature while still remembering it so we can call the | |
| 27 // correct destructor/release function. | |
| 28 // | |
| 29 // We use this trick so we don't need to include bind.h in a header | |
| 30 // file like sequenced_task_runner.h. We also wrap the helpers in a | |
| 31 // templated class to make it easier for users of DeleteSoon to | |
| 32 // declare the helper as a friend. | |
| 33 template <class T> | |
| 34 class DeleteHelper { | |
| 35 private: | |
| 36 template <class T2, class R> friend class subtle::DeleteHelperInternal; | |
| 37 | |
| 38 static void DoDelete(const void* object) { | |
| 39 delete reinterpret_cast<const T*>(object); | |
| 40 } | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(DeleteHelper); | |
| 43 }; | |
| 44 | |
| 45 template <class T> | |
| 46 class ReleaseHelper { | |
| 47 private: | |
| 48 template <class T2, class R> friend class subtle::ReleaseHelperInternal; | |
| 49 | |
| 50 static void DoRelease(const void* object) { | |
| 51 reinterpret_cast<const T*>(object)->Release(); | |
| 52 } | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(ReleaseHelper); | |
| 55 }; | |
| 56 | |
| 57 namespace subtle { | |
| 58 | |
| 59 // An internal SequencedTaskRunner-like class helper for DeleteHelper | |
| 60 // and ReleaseHelper. We don't want to expose the Do*() functions | |
| 61 // directly directly since the void* argument makes it possible to | |
| 62 // pass/ an object of the wrong type to delete. Instead, we force | |
| 63 // callers to go through these internal helpers for type | |
| 64 // safety. SequencedTaskRunner-like classes which expose DeleteSoon or | |
| 65 // ReleaseSoon methods should friend the appropriate helper and | |
| 66 // implement a corresponding *Internal method with the following | |
| 67 // signature: | |
| 68 // | |
| 69 // bool(const tracked_objects::Location&, | |
| 70 // void(*function)(const void*), | |
| 71 // void* object) | |
| 72 // | |
| 73 // An implementation of this function should simply create a | |
| 74 // base::Closure from (function, object) and return the result of | |
| 75 // posting the task. | |
| 76 template <class T, class ReturnType> | |
| 77 class DeleteHelperInternal { | |
| 78 public: | |
| 79 template <class SequencedTaskRunnerType> | |
| 80 static ReturnType DeleteViaSequencedTaskRunner( | |
| 81 SequencedTaskRunnerType* sequenced_task_runner, | |
| 82 const tracked_objects::Location& from_here, | |
| 83 const T* object) { | |
| 84 return sequenced_task_runner->DeleteSoonInternal( | |
| 85 from_here, &DeleteHelper<T>::DoDelete, object); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal); | |
| 90 }; | |
| 91 | |
| 92 template <class T, class ReturnType> | |
| 93 class ReleaseHelperInternal { | |
| 94 public: | |
| 95 template <class SequencedTaskRunnerType> | |
| 96 static ReturnType ReleaseViaSequencedTaskRunner( | |
| 97 SequencedTaskRunnerType* sequenced_task_runner, | |
| 98 const tracked_objects::Location& from_here, | |
| 99 const T* object) { | |
| 100 return sequenced_task_runner->ReleaseSoonInternal( | |
| 101 from_here, &ReleaseHelper<T>::DoRelease, object); | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 DISALLOW_COPY_AND_ASSIGN(ReleaseHelperInternal); | |
| 106 }; | |
| 107 | |
| 108 } // namespace subtle | |
| 109 | |
| 110 } // namespace base | |
| 111 | |
| 112 #endif // BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_ | |
| OLD | NEW |