Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: base/message_loop_helpers.h

Issue 9022034: Convert various ReleaseSoon methods to use base::Bind() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix test Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/message_loop.cc ('k') | base/message_loop_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 #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 } 20 }
20 21
21 // This is a template helper which uses a function indirection to erase T from 22 // Template helpers which use a function indirection to erase T from the
22 // the 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
23 // destructor. We use this trick so we don't need to include bind.h in a 24 // destructor/release function.
24 // header file. We also embed the function in a class to make it easier for 25 // We use this trick so we don't need to include bind.h in a header file like
25 // users of DeleteSoon to declare the helper as a friend. 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.
26 template <class T> 28 template <class T>
27 class DeleteHelper { 29 class DeleteHelper {
28 private: 30 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 template <class T2, class R> friend class subtle::DeleteHelperInternal;
31 32
32 static void DoDelete(const void* object) { 33 static void DoDelete(const void* object) {
33 delete reinterpret_cast<const T*>(object); 34 delete reinterpret_cast<const T*>(object);
34 } 35 }
35 36
36 DISALLOW_COPY_AND_ASSIGN(DeleteHelper); 37 DISALLOW_COPY_AND_ASSIGN(DeleteHelper);
37 }; 38 };
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
39 namespace subtle { 52 namespace subtle {
40 53
41 // An internal MessageLoop-like class helper for DeleteHelper. We don't want to 54 // An internal MessageLoop-like class helper for DeleteHelper and ReleaseHelper.
42 // expose DoDelete directly since the void* argument makes it possible to pass 55 // We don't want to expose the Do*() functions directly directly since the void*
43 // an object of the wrong type to delete. Instead, we force callers to go 56 // argument makes it possible to pass/ an object of the wrong type to delete.
44 // through DeleteHelperInternal for type safety. MessageLoop-like classes which 57 // Instead, we force callers to go through these internal helpers for type
45 // expose a DeleteSoon method should friend this class and implement a 58 // safety. MessageLoop-like classes which expose DeleteSoon or ReleaseSoon
46 // DeleteSoonInternal method with the following signature: 59 // methods should friend the appropriate helper and implement a corresponding
60 // *Internal method with the following signature:
47 // bool(const tracked_objects::Location&, 61 // bool(const tracked_objects::Location&,
48 // void(*deleter)(const void*), 62 // void(*function)(const void*),
49 // void* object) 63 // void* object)
50 // An implementation of DeleteSoonInternal should simply create a base::Closure 64 // An implementation of this function should simply create a base::Closure
51 // from (deleter, object) and return the result of posting the task. 65 // from (function, object) and return the result of posting the task.
52 template <class T, class ReturnType> 66 template <class T, class ReturnType>
53 class DeleteHelperInternal { 67 class DeleteHelperInternal {
54 public: 68 public:
55 template <class MessageLoopType> 69 template <class MessageLoopType>
56 static ReturnType DeleteOnMessageLoop( 70 static ReturnType DeleteOnMessageLoop(
57 MessageLoopType* message_loop, 71 MessageLoopType* message_loop,
58 const tracked_objects::Location& from_here, 72 const tracked_objects::Location& from_here,
59 const T* object) { 73 const T* object) {
60 return message_loop->DeleteSoonInternal(from_here, 74 return message_loop->DeleteSoonInternal(from_here,
61 &DeleteHelper<T>::DoDelete, 75 &DeleteHelper<T>::DoDelete,
62 object); 76 object);
63 } 77 }
64 78
65 private: 79 private:
66 DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal); 80 DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal);
67 }; 81 };
68 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
69 } // namespace subtle 100 } // namespace subtle
70 101
71 } // namespace base 102 } // namespace base
72 103
73 #endif // BASE_MESSAGE_LOOP_HELPERS_H_ 104 #endif // BASE_MESSAGE_LOOP_HELPERS_H_
OLDNEW
« no previous file with comments | « base/message_loop.cc ('k') | base/message_loop_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698