$darkmode
lua_action.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
25 #pragma once
26 
27 #include <sol/state_view.hpp>
28 #include <sol/function.hpp>
29 
30 #include <cloe/core.hpp> // for Logger, Json, Conf, ...
31 #include <cloe/trigger.hpp> // for Action, ActionFactory, ...
32 
33 namespace engine {
34 namespace actions {
35 
36 class LuaFunction : public cloe::Action {
37  public:
38  LuaFunction(const std::string& name, sol::function fun) : Action(name), func_(fun) {}
39 
40  cloe::ActionPtr clone() const override {
41  return std::make_unique<LuaFunction>(this->name(), func_);
42  }
43 
45 
46  void to_json(cloe::Json& j) const override { j = cloe::Json{}; }
47 
48  private:
49  sol::protected_function func_;
50 };
51 
52 class Lua : public cloe::Action {
53  public:
54  Lua(const std::string& name, const std::string& script, sol::state_view lua)
55  : Action(name), script_(script), lua_(lua) {}
56 
57  cloe::ActionPtr clone() const override { return std::make_unique<Lua>(name(), script_, lua_); }
58 
60 
61  protected:
62  void to_json(cloe::Json& j) const override;
63 
64  private:
65  std::string script_;
66  sol::state_view lua_;
67 };
68 
70  public:
71  using ActionType = Lua;
72  explicit LuaFactory(sol::state_view lua)
73  : cloe::ActionFactory("lua", "run a lua script"), lua_(lua) {
74  }
75  cloe::TriggerSchema schema() const override;
76  cloe::ActionPtr make(const cloe::Conf& c) const override;
77  cloe::ActionPtr make(const std::string& s) const override;
78 
79  private:
80  sol::state_view lua_;
81 };
82 
83 } // namespace actions
84 } // namespace engine
Definition: trigger.hpp:619
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:437
Definition: lua_action.hpp:69
cloe::ActionPtr make(const cloe::Conf &c) const override
Definition: lua_action.cpp:77
cloe::TriggerSchema schema() const override
Definition: lua_action.cpp:68
Definition: lua_action.hpp:36
cloe::ActionPtr clone() const override
Definition: lua_action.hpp:40
cloe::CallbackResult operator()(const cloe::Sync &sync, cloe::TriggerRegistrar &) override
Definition: lua_action.cpp:36
Definition: lua_action.hpp:52
cloe::CallbackResult operator()(const cloe::Sync &, cloe::TriggerRegistrar &) override
Definition: lua_action.cpp:49
cloe::ActionPtr clone() const override
Definition: lua_action.hpp:57
Definition: conf.hpp:81
Definition: trigger.hpp:207
CallbackResult
Definition: trigger.hpp:514