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 
35  enum EARGoSTypes {
41  };
42 
43  public:
44 
51  static bool LoadScript(lua_State* pt_state,
52  const std::string& str_filename);
53 
60  static bool CallLuaFunction(lua_State* pt_state,
61  const std::string& str_function);
62 
71  static void PrintGlobals(CARGoSLog& c_log,
72  lua_State* pt_state);
73 
82  static void PrintStack(CARGoSLog& c_log,
83  lua_State* pt_state);
84 
94  static void RegisterLoggerWrapper(lua_State* pt_state);
95 
105  static void RegisterRNG(lua_State* pt_state,
106  CRandom::CRNG* pc_rng);
107 
117  static void OpenRobotStateTable(lua_State* pt_state,
118  const std::string& str_key);
119 
126  static void CloseRobotStateTable(lua_State* pt_state);
127 
136  static void StartTable(lua_State* pt_state,
137  const std::string& str_key);
138 
147  static void StartTable(lua_State* pt_state,
148  int n_key);
149 
157  static void EndTable(lua_State* pt_state);
158 
167  static void AddToTable(lua_State* pt_state,
168  const std::string& str_key,
169  void* pt_data);
170 
179  static void AddToTable(lua_State* pt_state,
180  const std::string& str_key,
181  lua_CFunction pt_data);
182 
191  static void AddToTable(lua_State* pt_state,
192  const std::string& str_key,
193  Real f_data);
202  static void AddToTable(lua_State* pt_state,
203  const std::string& str_key,
204  const std::string& str_data);
205 
214  static void AddToTable(lua_State* pt_state,
215  int n_key,
216  Real f_data);
217 
226  static void AddToTable(lua_State* pt_state,
227  const std::string& str_key,
228  const CRadians& c_data);
229 
238  static void AddToTable(lua_State* pt_state,
239  int n_key,
240  const CRadians& c_data);
241 
252  static void AddToTable(lua_State* pt_state,
253  const std::string& str_key,
254  const CVector2& c_data);
255 
266  static void AddToTable(lua_State* pt_state,
267  int n_key,
268  const CVector2& c_data);
269 
280  static void AddToTable(lua_State* pt_state,
281  const std::string& str_key,
282  const CVector3& c_data);
283 
294  static void AddToTable(lua_State* pt_state,
295  int n_key,
296  const CVector3& c_data);
297 
308  static void AddToTable(lua_State* pt_state,
309  const std::string& str_key,
310  const CQuaternion& c_data);
311 
322  static void AddToTable(lua_State* pt_state,
323  int n_key,
324  const CQuaternion& c_data);
325 
336  static void AddToTable(lua_State* pt_state,
337  const std::string& str_key,
338  const CColor& c_data);
339 
350  static void AddToTable(lua_State* pt_state,
351  int n_key,
352  const CColor& c_data);
353 
364  template<class T>
365  static T* GetDeviceInstance(lua_State* pt_state,
366  const std::string& str_key) {
367  lua_getglobal(pt_state, "robot");
368  lua_getfield(pt_state, -1, str_key.c_str());
369  lua_getfield(pt_state, -1, "_instance");
370  T* ptRetVal = reinterpret_cast<T*>(lua_touserdata(pt_state, -1));
371  lua_pop(pt_state, 3);
372  return ptRetVal;
373  }
374 
375  private:
376 
377  static int LOGWrapper(lua_State* pt_state);
378 
379  static int LOGERRWrapper(lua_State* pt_state);
380 
381  static int LoggerWrapper(CARGoSLog& c_log,
382  lua_State* pt_state);
383 
384  };
385 
386 }
387 
388 #endif
The RNG.
Definition: rng.h:90
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...
A 3D vector class.
Definition: vector3.h:29
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
static void RegisterRNG(lua_State *pt_state, CRandom::CRNG *pc_rng)
Registers the given random number generator in the Lua state.
static bool LoadScript(lua_State *pt_state, const std::string &str_filename)
Loads the given Lua script.
static bool CallLuaFunction(lua_State *pt_state, const std::string &str_function)
Calls a parameter-less function in the Lua script.
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
static void PrintGlobals(CARGoSLog &c_log, lua_State *pt_state)
Prints the global Lua symbols on the specified log.
static void PrintStack(CARGoSLog &c_log, lua_State *pt_state)
Prints the Lua stack on the specified log.
A 2D vector class.
Definition: vector2.h:25
static void RegisterLoggerWrapper(lua_State *pt_state)
Registers LOG and LOGERR in the Lua state.
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:365
The basic color type.
Definition: color.h:25
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 OpenRobotStateTable(lua_State *pt_state, const std::string &str_key)
Opens a table in the robot state, creating it if it does not exist.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
static void CloseRobotStateTable(lua_State *pt_state)
Closes a table in the robot state.
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.