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