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