Overte C++ Documentation
HelperScriptEngine.h
1 //
2 // HelperScriptEngine.h
3 // libraries/script-engine/src/HelperScriptEngine.h
4 //
5 // Created by dr Karol Suprynowicz on 2024/04/28.
6 // Copyright 2024 Overte e.V.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef overte_HelperScriptEngine_h
13 #define overte_HelperScriptEngine_h
14 
15 #include <mutex>
16 #include "QThread"
17 
18 #include "ScriptEngine.h"
19 
32 public:
35 
36  template <typename F>
37  inline void run(F&& f) {
38  std::lock_guard<std::mutex> guard(_scriptEngineLock);
39  auto scopeGuard = _scriptEngine->getScopeGuard();
40  f();
41  }
42 
43  template <typename T, typename F>
44  inline T runWithResult(F&& f) {
45  T result;
46  {
47  std::lock_guard<std::mutex> guard(_scriptEngineLock);
48  result = f();
49  }
50  return result;
51  }
52 
58  ScriptEngine* get() { return _scriptEngine.get(); };
59  ScriptEnginePointer getShared() { return _scriptEngine; };
60 private:
61  std::mutex _scriptEngineLock;
62  ScriptEnginePointer _scriptEngine { nullptr };
63  std::shared_ptr<QThread> _scriptEngineThread { nullptr };
64 };
65 
66 #endif //overte_HelperScriptEngine_h
Provides a wrapper around script engine that does not have ScriptManager.
Definition: HelperScriptEngine.h:31
ScriptEngine * get()
Returns pointer to the script engine.
Definition: HelperScriptEngine.h:58
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93