Shaka Player Embedded
js_wrapper.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_JS_WRAPPER_H_
16 #define SHAKA_EMBEDDED_JS_WRAPPER_H_
17 
18 #include <glog/logging.h>
19 
20 #include <utility>
21 
23 #include "src/core/ref_ptr.h"
24 #include "src/core/task_runner.h"
25 #include "src/util/macros.h"
26 
27 namespace shaka {
28 namespace util {
29 
30 template <typename T>
31 class JSWrapper {
32  public:
34 
35  JSWrapper() {}
36 
38 
39  template <typename Func, typename... Args>
40  auto CallInnerMethod(Func member, Args... args)
41  -> decltype((inner->*member)(args...)) {
42  DCHECK(inner) << "Must call Initialize.";
44  ->MainThread()
46  std::bind(member, inner, args...))
47  ->GetValue();
48  }
49 
50  template <typename Var, typename Val>
51  void SetMemberVariable(Var member, Val val) {
52  DCHECK(inner) << "Must call Initialize.";
53  auto callback = [this, member, val]() { inner->*member = val; };
55  ->MainThread()
56  ->AddInternalTask(TaskPriority::Internal, "", std::move(callback))
57  ->GetValue();
58  }
59 
60  template <typename Var>
61  auto GetMemberVariable(Var member) -> decltype(inner->*member) {
62  DCHECK(inner) << "Must call Initialize.";
64  ->MainThread()
65  ->AddInternalTask(TaskPriority::Internal, "", std::bind(member, inner))
66  ->GetValue();
67  }
68 };
69 
70 } // namespace util
71 } // namespace shaka
72 
73 #endif // SHAKA_EMBEDDED_JS_WRAPPER_H_
SHAKA_NON_COPYABLE_OR_MOVABLE_TYPE(JSWrapper)
auto GetMemberVariable(Var member) -> decltype(inner-> *member)
Definition: js_wrapper.h:61
std::shared_ptr< ThreadEvent< impl::RetOf< Func > > > AddInternalTask(TaskPriority priority, const std::string &name, Func &&callback)
Definition: task_runner.h:219
void SetMemberVariable(Var member, Val val)
Definition: js_wrapper.h:51
auto CallInnerMethod(Func member, Args... args) -> decltype((inner-> *member)(args...))
Definition: js_wrapper.h:40
TaskRunner * MainThread()