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