sequenced_task_runner_helpers.h 1.6 KB

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