lua_custom_function.cpp
Go to the documentation of this file.
1 #include "lua_custom_function.h"
2 
4  : CustomFunction(snippet)
5 {
6  initEngine();
7 }
8 
10 {
11  _lua_engine = std::unique_ptr<sol::state>(new sol::state());
12  _lua_engine->open_libraries();
13  _lua_engine->script(_snippet.globalVars.toStdString());
14 
15  auto calcMethodStr = QString("function calc(time, value");
16 
17  for(int index = 1; index <= _snippet.additionalSources.size(); index++ )
18  {
19  calcMethodStr += QString(", v%1").arg(index);
20  }
21 
22  calcMethodStr += QString(")\n%1\nend").arg(snippet().function);
23  _lua_engine->safe_script(calcMethodStr.toStdString());
24 
25  _lua_function = (*_lua_engine)["calc"];
26 }
27 
29  const std::vector<const PlotData*>& channels_data,
30  size_t point_index)
31 {
32  _chan_values.resize(channels_data.size());
33 
34  const PlotData::Point& old_point = src_data.at(point_index);
35 
36  for (size_t chan_index = 0; chan_index < channels_data.size(); chan_index++)
37  {
38  double value;
39  const auto& chan_data = channels_data[chan_index];
40  int index = chan_data->getIndexFromX(old_point.x);
41  if (index != -1)
42  {
43  value = chan_data->at(index).y;
44  }
45  else
46  {
47  value = std::numeric_limits<double>::quiet_NaN();
48  }
49  _chan_values[chan_index] = value;
50  }
51 
52  PlotData::Point new_point;
53  new_point.x = old_point.x;
54 
56  const auto& v = _chan_values;
57  // ugly code, sorry
58  switch( _snippet.additionalSources.size() )
59  {
60  case 0: result = _lua_function(old_point.x, old_point.y);
61  break;
62  case 1: result = _lua_function(old_point.x, old_point.y,
63  v[0]);
64  break;
65  case 2: result = _lua_function(old_point.x, old_point.y,
66  v[0], v[1]);
67  break;
68  case 3: result = _lua_function(old_point.x, old_point.y,
69  v[0], v[1], v[2]);
70  break;
71  case 4: result = _lua_function(old_point.x, old_point.y,
72  v[0], v[1], v[2], v[3]);
73  break;
74  case 5: result = _lua_function(old_point.x, old_point.y,
75  v[0], v[1], v[2], v[3], v[4]);
76  break;
77  case 6: result = _lua_function(old_point.x, old_point.y,
78  v[0], v[1], v[2], v[3], v[4], v[5]);
79  break;
80  case 7: result = _lua_function(old_point.x, old_point.y,
81  v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
82  break;
83  case 8: result = _lua_function(old_point.x, old_point.y,
84  v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
85  break;
86  default:
87  throw std::runtime_error("Lua Engine : maximum number of additional source is 8");
88  }
89 
90  if( !result.valid() )
91  {
92  throw std::runtime_error("Lua Engine : invalid function (missing variable?)");
93  }
94 
95  if (result.return_count() == 2)
96  {
97  new_point.x = result.get<double>(0);
98  new_point.y = result.get<double>(1);
99  }
100  else if (result.return_count() == 1)
101  {
102  new_point.y = result.get<double>(0);
103  }
104  else
105  {
106  throw std::runtime_error("Lua Engine : return either a single value "
107  "or an array with size 2 (time, value)");
108  }
109  return new_point;
110 }
enum MQTTPropertyCodes value
static heap_info state
Definition: Heap.c:58
LuaCustomFunction(const SnippetData &snippet)
const Point & at(size_t index) const
Definition: plotdata.h:97
std::vector< double > _chan_values
QString globalVars
sol::protected_function _lua_function
void initEngine() override
bool valid() const noexcept
Definition: sol.hpp:15805
PlotData::Point calculatePoint(const PlotData &src_data, const std::vector< const PlotData * > &channels_data, size_t point_index) override
int return_count() const noexcept
Definition: sol.hpp:15902
const SnippetData _snippet
std::unique_ptr< sol::state > _lua_engine
const SnippetData & snippet() const
decltype(auto) get(int index_offset=0) const
Definition: sol.hpp:15810
QStringList additionalSources
typename PlotDataBase< Value >::Point Point
Definition: plotdata.h:290


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09