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