lua_utility.h
Go to the documentation of this file.
1 #ifndef LUA_UTILITY_H
2 #define LUA_UTILITY_H
3 
10 namespace argos {
11  class CRadians;
12  class CVector2;
13  class CVector3;
14  class CQuaternion;
15  class CColor;
16 }
17 
18 #include <argos3/core/utility/logging/argos_log.h>
19 #include <argos3/core/utility/math/rng.h>
20 
21 extern "C" {
22 #include <lua.h>
23 #include <lualib.h>
24 #include <lauxlib.h>
25 }
26 
27 #include <string>
28 
29 namespace argos {
30 
31  class CLuaUtility {
32 
33  public:
34 
41  static bool LoadScript(lua_State* pt_state,
42  const std::string& str_filename);
43 
50  static bool CallLuaFunction(lua_State* pt_state,
51  const std::string& str_function);
52 
61  static void PrintGlobals(CARGoSLog& c_log,
62  lua_State* pt_state);
63 
72  static void PrintStack(CARGoSLog& c_log,
73  lua_State* pt_state);
74 
84  static void RegisterLoggerWrapper(lua_State* pt_state);
85 
95  static void RegisterRNG(lua_State* pt_state,
96  CRandom::CRNG* pc_rng);
97 
107  static void OpenRobotStateTable(lua_State* pt_state,
108  const std::string& str_key);
109 
116  static void CloseRobotStateTable(lua_State* pt_state);
117 
126  static void StartTable(lua_State* pt_state,
127  const std::string& str_key);
128 
137  static void StartTable(lua_State* pt_state,
138  int n_key);
139 
147  static void EndTable(lua_State* pt_state);
148 
149 
157  static void SetMetatable(lua_State* pt_state,
158  const std::string& str_key);
159 
160 
169  static void AddToTable(lua_State* pt_state,
170  const std::string& str_key,
171  void* pt_data);
172 
181  static void AddToTable(lua_State* pt_state,
182  const std::string& str_key,
183  lua_CFunction pt_data);
184 
193  static void AddToTable(lua_State* pt_state,
194  const std::string& str_key,
195  const std::string& str_data);
196 
205  static void AddToTable(lua_State* pt_state,
206  int n_key,
207  const std::string& str_data);
208 
217  static void AddToTable(lua_State* pt_state,
218  const std::string& str_key,
219  Real f_data);
220 
229  static void AddToTable(lua_State* pt_state,
230  int n_key,
231  Real f_data);
232 
241  static void AddToTable(lua_State* pt_state,
242  const std::string& str_key,
243  const CRadians& c_data);
244 
253  static void AddToTable(lua_State* pt_state,
254  int n_key,
255  const CRadians& c_data);
256 
267  static void AddToTable(lua_State* pt_state,
268  const std::string& str_key,
269  const CVector2& c_data);
270 
281  static void AddToTable(lua_State* pt_state,
282  int n_key,
283  const CVector2& c_data);
284 
295  static void AddToTable(lua_State* pt_state,
296  const std::string& str_key,
297  const CVector3& c_data);
298 
309  static void AddToTable(lua_State* pt_state,
310  int n_key,
311  const CVector3& c_data);
312 
323  static void AddToTable(lua_State* pt_state,
324  const std::string& str_key,
325  const CQuaternion& c_data);
326 
337  static void AddToTable(lua_State* pt_state,
338  int n_key,
339  const CQuaternion& c_data);
340 
351  static void AddToTable(lua_State* pt_state,
352  const std::string& str_key,
353  const CColor& c_data);
354 
365  static void AddToTable(lua_State* pt_state,
366  int n_key,
367  const CColor& c_data);
368 
379  template<class T>
380  static T* GetDeviceInstance(lua_State* pt_state,
381  const std::string& str_key) {
382  lua_getglobal(pt_state, "robot");
383  lua_getfield(pt_state, -1, str_key.c_str());
384  lua_getfield(pt_state, -1, "_instance");
385  T* ptRetVal = reinterpret_cast<T*>(lua_touserdata(pt_state, -1));
386  lua_pop(pt_state, 3);
387  return ptRetVal;
388  }
389 
390  private:
391 
392  static int LOGWrapper(lua_State* pt_state);
393 
394  static int LOGERRWrapper(lua_State* pt_state);
395 
396  static int LoggerWrapper(CARGoSLog& c_log,
397  lua_State* pt_state);
398 
399  };
400 
401 }
402 
403 #endif
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
The basic color type.
Definition: color.h:25
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
The RNG.
Definition: rng.h:90
A 2D vector class.
Definition: vector2.h:27
A 3D vector class.
Definition: vector3.h:31
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.
static T * GetDeviceInstance(lua_State *pt_state, const std::string &str_key)
Returns a pointer to the instance to the wanted device.
Definition: lua_utility.h:380
static void AddToTable(lua_State *pt_state, const std::string &str_key, void *pt_data)
Adds a pointer to a chunk of data with the given string key to the table located at the top of the st...
static bool CallLuaFunction(lua_State *pt_state, const std::string &str_function)
Calls a parameter-less function in the Lua script.
static void SetMetatable(lua_State *pt_state, const std::string &str_key)
Sets the metatable with the given string key to the table located at the top of the stack.
static bool LoadScript(lua_State *pt_state, const std::string &str_filename)
Loads the given Lua script.
static void StartTable(lua_State *pt_state, const std::string &str_key)
Adds a table with the given string key to the table located at the top of the stack.
static void RegisterLoggerWrapper(lua_State *pt_state)
Registers LOG and LOGERR in the Lua state.
static void OpenRobotStateTable(lua_State *pt_state, const std::string &str_key)
Opens a table in the robot state, creating it if it does not exist.
static void CloseRobotStateTable(lua_State *pt_state)
Closes a table in the robot state.
static void PrintStack(CARGoSLog &c_log, lua_State *pt_state)
Prints the Lua stack on the specified log.
static void RegisterRNG(lua_State *pt_state, CRandom::CRNG *pc_rng)
Registers the given random number generator in the Lua state.
static void PrintGlobals(CARGoSLog &c_log, lua_State *pt_state)
Prints the global Lua symbols on the specified log.