• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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_TASK_SEQUENCED_TASK_RUNNER_HELPERS_H_
6 #define BASE_TASK_SEQUENCED_TASK_RUNNER_HELPERS_H_
7 
8 #include <memory>
9 
10 namespace base {
11 
12 class SequencedTaskRunner;
13 
14 // Template helpers which use function indirection to erase T from the
15 // function signature while still remembering it so we can call the
16 // correct destructor/release function.
17 //
18 // We use this trick so we don't need to include bind.h in a header
19 // file like sequenced_task_runner.h. We also wrap the helpers in a
20 // templated class to make it easier for users of DeleteSoon to
21 // declare the helper as a friend.
22 template <class T>
23 class DeleteHelper {
24  private:
DoDelete(const void * object)25   static void DoDelete(const void* object) {
26     delete static_cast<const T*>(object);
27   }
28 
29   friend class SequencedTaskRunner;
30 };
31 
32 template <class T>
33 class DeleteUniquePtrHelper {
34  private:
DoDelete(const void * object)35   static void DoDelete(const void* object) {
36     // Carefully unwrap `object`. T could have originally been const-qualified
37     // or not, and it is important to ensure that the constness matches in order
38     // to use the right specialization of std::default_delete<T>...
39     std::unique_ptr<T> destroyer(const_cast<T*>(static_cast<const T*>(object)));
40   }
41 
42   friend class SequencedTaskRunner;
43 };
44 
45 template <class T>
46 class ReleaseHelper {
47  private:
DoRelease(const void * object)48   static void DoRelease(const void* object) {
49     static_cast<const T*>(object)->Release();
50   }
51 
52   friend class SequencedTaskRunner;
53 };
54 
55 }  // namespace base
56 
57 #endif  // BASE_TASK_SEQUENCED_TASK_RUNNER_HELPERS_H_
58