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
3 // found in the LICENSE file.
5 #ifndef BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_
6 #define BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_
8 #include "base/basictypes.h"
10 // TODO(akalin): Investigate whether it's possible to just have
11 // SequencedTaskRunner use these helpers (instead of MessageLoop).
12 // Then we can just move these to sequenced_task_runner.h.
14 namespace tracked_objects
{
21 template <class T
, class R
> class DeleteHelperInternal
;
22 template <class T
, class R
> class ReleaseHelperInternal
;
25 // Template helpers which use function indirection to erase T from the
26 // function signature while still remembering it so we can call the
27 // correct destructor/release function.
29 // We use this trick so we don't need to include bind.h in a header
30 // file like sequenced_task_runner.h. We also wrap the helpers in a
31 // templated class to make it easier for users of DeleteSoon to
32 // declare the helper as a friend.
36 template <class T2
, class R
> friend class subtle::DeleteHelperInternal
;
38 static void DoDelete(const void* object
) {
39 delete reinterpret_cast<const T
*>(object
);
42 DISALLOW_COPY_AND_ASSIGN(DeleteHelper
);
48 template <class T2
, class R
> friend class subtle::ReleaseHelperInternal
;
50 static void DoRelease(const void* object
) {
51 reinterpret_cast<const T
*>(object
)->Release();
54 DISALLOW_COPY_AND_ASSIGN(ReleaseHelper
);
59 // An internal SequencedTaskRunner-like class helper for DeleteHelper
60 // and ReleaseHelper. We don't want to expose the Do*() functions
61 // directly directly since the void* argument makes it possible to
62 // pass/ an object of the wrong type to delete. Instead, we force
63 // callers to go through these internal helpers for type
64 // safety. SequencedTaskRunner-like classes which expose DeleteSoon or
65 // ReleaseSoon methods should friend the appropriate helper and
66 // implement a corresponding *Internal method with the following
69 // bool(const tracked_objects::Location&,
70 // void(*function)(const void*),
73 // An implementation of this function should simply create a
74 // base::Closure from (function, object) and return the result of
76 template <class T
, class ReturnType
>
77 class DeleteHelperInternal
{
79 template <class SequencedTaskRunnerType
>
80 static ReturnType
DeleteViaSequencedTaskRunner(
81 SequencedTaskRunnerType
* sequenced_task_runner
,
82 const tracked_objects::Location
& from_here
,
84 return sequenced_task_runner
->DeleteSoonInternal(
85 from_here
, &DeleteHelper
<T
>::DoDelete
, object
);
89 DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal
);
92 template <class T
, class ReturnType
>
93 class ReleaseHelperInternal
{
95 template <class SequencedTaskRunnerType
>
96 static ReturnType
ReleaseViaSequencedTaskRunner(
97 SequencedTaskRunnerType
* sequenced_task_runner
,
98 const tracked_objects::Location
& from_here
,
100 return sequenced_task_runner
->ReleaseSoonInternal(
101 from_here
, &ReleaseHelper
<T
>::DoRelease
, object
);
105 DISALLOW_COPY_AND_ASSIGN(ReleaseHelperInternal
);
108 } // namespace subtle
112 #endif // BASE_SEQUENCED_TASK_RUNNER_HELPERS_H_