OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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_MESSAGE_LOOP_HELPERS_H_ |
| 6 #define BASE_MESSAGE_LOOP_HELPERS_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 |
| 11 namespace tracked_objects { |
| 12 class Location; |
| 13 } |
| 14 |
| 15 namespace base { |
| 16 |
| 17 namespace subtle { |
| 18 template <class T, class R> class DeleteHelperInternal; |
| 19 } |
| 20 |
| 21 // This is a template helper which uses a function indirection to erase T from |
| 22 // the function signature while still remembering it so we can call the correct |
| 23 // destructor. We use this trick so we don't need to include bind.h in a |
| 24 // header file. We also embed the function in a class to make it easier for |
| 25 // users of DeleteSoon to declare the helper as a friend. |
| 26 template <class T> |
| 27 class DeleteHelper { |
| 28 private: |
| 29 // TODO(dcheng): Move the return type back to a function template parameter. |
| 30 template <class T2, class R> friend class subtle::DeleteHelperInternal; |
| 31 |
| 32 static void DoDelete(const void* object) { |
| 33 delete reinterpret_cast<const T*>(object); |
| 34 } |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(DeleteHelper); |
| 37 }; |
| 38 |
| 39 namespace subtle { |
| 40 |
| 41 // An internal MessageLoop-like class helper for DeleteHelper. We don't want to |
| 42 // expose DoDelete directly since the void* argument makes it possible to pass |
| 43 // an object of the wrong type to delete. Instead, we force callers to go |
| 44 // through DeleteHelperInternal for type safety. MessageLoop-like classes which |
| 45 // expose a DeleteSoon method should friend this class and implement a |
| 46 // DeleteSoonInternal method with the following signature: |
| 47 // bool(const tracked_objects::Location&, |
| 48 // void(*deleter)(const void*), |
| 49 // void* object) |
| 50 // An implementation of DeleteSoonInternal should simply create a base::Closure |
| 51 // from (deleter, object) and return the result of posting the task. |
| 52 template <class T, class ReturnType> |
| 53 class DeleteHelperInternal { |
| 54 public: |
| 55 template <class MessageLoopType> |
| 56 static ReturnType DeleteOnMessageLoop( |
| 57 MessageLoopType* message_loop, |
| 58 const tracked_objects::Location& from_here, |
| 59 const T* object) { |
| 60 return message_loop->DeleteSoonInternal(from_here, |
| 61 &DeleteHelper<T>::DoDelete, |
| 62 object); |
| 63 } |
| 64 |
| 65 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal); |
| 67 }; |
| 68 |
| 69 } // namespace subtle |
| 70 |
| 71 } // namespace base |
| 72 |
| 73 #endif // BASE_MESSAGE_LOOP_HELPERS_H_ |
OLD | NEW |