MediaTaskUtils.h (1512B)
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 * but ignore the return value from the lambda. 22 * 23 * Prefer NS_NewRunnableFunction(), which provides a specific name, unless the 24 * lambda really must have a non-void return value that is to be ignored. 25 */ 26 27 template <typename OnRunType> 28 class LambdaTask : public Runnable { 29 public: 30 explicit LambdaTask(OnRunType&& aOnRun) 31 : Runnable("media::LambdaTask"), mOnRun(std::move(aOnRun)) {} 32 33 private: 34 NS_IMETHOD 35 Run() override { 36 mOnRun(); 37 return NS_OK; 38 } 39 OnRunType mOnRun; 40 }; 41 42 template <typename OnRunType> 43 already_AddRefed<LambdaTask<OnRunType>> NewTaskFrom(OnRunType&& aOnRun) { 44 typedef LambdaTask<OnRunType> LambdaType; 45 RefPtr<LambdaType> lambda = new LambdaType(std::forward<OnRunType>(aOnRun)); 46 return lambda.forget(); 47 } 48 49 } // namespace media 50 } // namespace mozilla 51 52 #endif // mozilla_MediaTaskUtils_h