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