Line data Source code
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 0 : class LambdaTask : public Runnable
25 : {
26 : public:
27 0 : explicit LambdaTask(OnRunType&& aOnRun)
28 : : Runnable("media::LambdaTask")
29 0 : , mOnRun(Move(aOnRun))
30 : {
31 0 : }
32 :
33 : private:
34 : NS_IMETHOD
35 0 : Run() override
36 : {
37 0 : mOnRun();
38 0 : return NS_OK;
39 : }
40 : OnRunType mOnRun;
41 : };
42 :
43 : template<typename OnRunType>
44 : already_AddRefed<LambdaTask<OnRunType>>
45 0 : NewTaskFrom(OnRunType&& aOnRun)
46 : {
47 : typedef LambdaTask<OnRunType> LambdaType;
48 0 : RefPtr<LambdaType> lambda = new LambdaType(Forward<OnRunType>(aOnRun));
49 0 : return lambda.forget();
50 : }
51 :
52 : } // namespace media
53 : } // namespace mozilla
54 :
55 : #endif // mozilla_MediaTaskUtils_h
|