utils.h
1 #pragma once
2 
3 #include <memory>
4 #include <stdexcept>
5 #include <tuple>
6 #include <vector>
7 
8 #include <momemta/any.h>
9 #include <lua.hpp>
10 
11 class ILuaCallback;
12 class ParameterSet;
13 
21 namespace lua {
22 
24  class invalid_configuration_file: public std::runtime_error {
25  using std::runtime_error::runtime_error;
26  };
27 
28  class invalid_array_error: public std::runtime_error {
29  using std::runtime_error::runtime_error;
30  };
31 
32  class unsupported_type_error: public std::runtime_error {
33  using std::runtime_error::runtime_error;
34  };
35 
39  enum Type {
44  REAL,
47  };
48 
54  struct Lazy {
55  lua_State* L;
56 
57  Lazy(lua_State* L);
58 
64  virtual momemta::any operator() () const = 0;
65  };
66 
72  struct LazyFunction: public Lazy {
73  int ref_index;
74 
80  virtual momemta::any operator() () const override;
81 
88  LazyFunction(lua_State* L, int index);
89  };
90 
99  Type type(lua_State* L, int index);
100 
109  size_t get_index(lua_State* L, int index);
110 
116  int lua_is_array(lua_State* L, int index);
117 
125  std::pair<momemta::any, bool> to_any(lua_State* L, int index);
126 
138  void push_any(lua_State* L, const momemta::any& value);
139 
140  template<typename T> T special_any_cast(const momemta::any& value) {
141  return momemta::any_cast<T>(value);
142  }
143 
149  template<typename T>
150  momemta::any to_vectorT(lua_State* L, int index) {
151 
152  std::vector<T> result;
153  size_t absolute_index = get_index(L, index);
154 
155  if (lua_type(L, absolute_index) != LUA_TTABLE)
156  return result;
157 
158  lua_pushnil(L);
159  while (lua_next(L, absolute_index) != 0) {
160  momemta::any value;
161  bool lazy = false;
162  std::tie(value, lazy) = to_any(L, -1);
163  result.push_back(special_any_cast<T>(value));
164  lua_pop(L, 1);
165  }
166 
167  return momemta::any(result);
168  }
169 
175  momemta::any to_vector(lua_State* L, int index, Type type);
176 
184  void setup_hooks(lua_State* L, void* ptr);
185 
199  int module_table_newindex(lua_State* L);
200 
217  void register_modules(lua_State* L, void* ptr);
218 
228  int load_modules(lua_State* L);
229 
239  int parameter(lua_State* L);
240 
249  std::shared_ptr<lua_State> init_runtime(ILuaCallback* callback);
250 
260  int generate_cuba_inputtag(lua_State* L);
261 
270  void inject_parameters(lua_State* L, const ParameterSet& parameters);
271 
272  namespace debug {
273  std::vector<std::string> dump_stack(lua_State* L);
274  void print_stack(lua_State* L);
275  }
276 }
Notification callback used for communication between the lua file and MoMEMta.
Definition: ILuaCallback.h:30
size_t get_index(lua_State *L, int index)
Convert a negative lua stack index to an absolute index.
Definition: utils.cc:116
int generate_cuba_inputtag(lua_State *L)
Define Lua function to generate Cuba phase-space point input-tags.
void setup_hooks(lua_State *L, void *ptr)
Register all C function in the lua userspace.
Definition: utils.cc:524
std::shared_ptr< lua_State > init_runtime(ILuaCallback *callback)
Initialize the lua runtime.
Definition: utils.cc:554
Type type(lua_State *L, int index)
Extract the type of a lua value.
Definition: utils.cc:81
momemta::any to_vectorT(lua_State *L, int index)
Convert a lua array to a typed vector, encapsulated into a momemta::any.
Definition: utils.h:150
int load_modules(lua_State *L)
Hook for the load_modules lua function. The stack must have one element:
Definition: utils.cc:422
int lua_is_array(lua_State *L, int index)
Check if a lua table is an array.
Definition: utils.cc:125
Type
List of all supported lua types.
Definition: utils.h:39
int ref_index
The reference index where the anonymous function is stored.
Definition: utils.h:73
lua_State * L
The global lua state. This state must be valid for as long as this instance.
Definition: utils.h:55
momemta::any to_vector(lua_State *L, int index, Type type)
Convert a lua array to a typed vector, encapsulated into a momemta::any.
Definition: utils.cc:297
void register_modules(lua_State *L, void *ptr)
Register modules in lua userspace.
Definition: utils.cc:376
void push_any(lua_State *L, const momemta::any &value)
Convert a momemta::any to a lua type, and push it to the top of the stack.
Definition: utils.cc:271
Lazy value in lua (delayed evaluation)
Definition: utils.h:54
A class encapsulating a lua table.
Definition: ParameterSet.h:82
int module_table_newindex(lua_State *L)
Hook for the metatable __newindex of the module&#39;s table.
Definition: utils.cc:332
< Thrown if the configuration file is not valid
Definition: utils.h:24
int parameter(lua_State *L)
Hook for the parameter lua function. This function accepts one argument:
Definition: utils.cc:438
std::pair< momemta::any, bool > to_any(lua_State *L, int index)
Convert a lua type to momemta::any.
Definition: utils.cc:191
void inject_parameters(lua_State *L, const ParameterSet &parameters)
Inject parameters into the current lua state.
Definition: utils.cc:572
Lazy function in lua (delayed function evaluation)
Definition: utils.h:72
Utility functions related to lua configuration file parsing.
Definition: Path.h:31