MediaTaskUtils.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_MediaTaskUtils_h
  6. #define mozilla_MediaTaskUtils_h
  7. #include "nsThreadUtils.h"
  8. // The main reason this file is separate from MediaUtils.h
  9. #include "base/task.h"
  10. namespace mozilla {
  11. namespace media {
  12. /* media::NewTaskFrom() - Create a Task from a lambda.
  13. *
  14. * Similar to media::NewRunnableFrom() - Create an nsRunnable from a lambda.
  15. */
  16. template<typename OnRunType>
  17. class LambdaTask : public Runnable
  18. {
  19. public:
  20. explicit LambdaTask(OnRunType&& aOnRun) : mOnRun(Move(aOnRun)) {}
  21. private:
  22. NS_IMETHOD
  23. Run() override
  24. {
  25. mOnRun();
  26. return NS_OK;
  27. }
  28. OnRunType mOnRun;
  29. };
  30. template<typename OnRunType>
  31. already_AddRefed<LambdaTask<OnRunType>>
  32. NewTaskFrom(OnRunType&& aOnRun)
  33. {
  34. typedef LambdaTask<OnRunType> LambdaType;
  35. RefPtr<LambdaType> lambda = new LambdaType(Forward<OnRunType>(aOnRun));
  36. return lambda.forget();
  37. }
  38. } // namespace media
  39. } // namespace mozilla
  40. #endif // mozilla_MediaTaskUtils_h