AGX Dynamics  2.35.0.1
luaUtils.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXLUA_LUAUTILS_H
18 #define AGXLUA_LUAUTILS_H
19 
20 #include <agx/config/AGX_USE_LUA.h>
21 #include <agx/config.h>
22 #if AGX_USE_LUA()
23 
24 extern "C" {
25  #include <lua.h>
26  #include <lualib.h>
27  #include <lauxlib.h>
28  struct tolua_Error;
29 }
30 
31 #include <agx/Referenced.h>
32 #include <agxLua/export.h>
33 #include <stdexcept>
34 
35 
36 
40 namespace agxLua
41 {
42 
43  class LuaError : public std::runtime_error
44  {
45  public:
46  explicit LuaError( lua_State *L ) : std::runtime_error(""), m_msg("")
47  {
48  if (lua_isstring(L, -1)) {
49  m_msg = lua_tostring(L, -1);
50  }
51  }
52 
53  const char *what() const throw() { return m_msg; }
54 
55  explicit LuaError( const char *msg ) : std::runtime_error(msg), m_msg(msg)
56  {
57  }
58 
59  private:
60  const char *m_msg;
61  };
62 
63  AGXLUA_EXPORT bool pushusertype (lua_State *L, void *p, const char *name);
64 
65  AGXLUA_EXPORT void ensurepointer (lua_State *L, const void *p);
66 
67  AGXLUA_EXPORT void getpointertable (lua_State *L);
68 
69  AGXLUA_EXPORT void pushstring (lua_State* L, const char* value);
70 
71  AGXLUA_EXPORT const char* tostring (lua_State* L, int narg, const char* def="");
72 
73  AGXLUA_EXPORT bool testuserdata (lua_State *L, int index, const char *name);
74 
75  AGXLUA_EXPORT int pcall (lua_State *L, int nargs, int nresults, int errfunc);
76 
77  AGXLUA_EXPORT void error( lua_State * L);
78 
79  AGXLUA_EXPORT int requestPlugin(lua_State *L);
80 
81  AGXLUA_EXPORT int doFile(lua_State *L);
82  AGXLUA_EXPORT int doString(lua_State *L);
83 
84 
85 // template<typename T>
86 // T *touserdata (lua_State *L, int index, const char *name) {
87 // void *ret = 0;
88 // if (!agxLua::testuserdata(L, index, name)) return 0;
89 // void **pp = static_cast<void**>(lua_touserdata(L, index));
90 // ret = *pp;
91 // lua_getfield(L, index, name);
92 // ret = (void*)(lua_tointeger(L, -1) + (char*)ret);
93 // lua_pop(L, 1);
94 // return static_cast<T *>(ret);
95 // }
96 
97 
107  AGXLUA_EXPORT void *_tousertype(lua_State *L, int index, const char *type);
108 
109  template<typename T>
110  T *tousertype (lua_State *L, int index, const char *type) {
111  return static_cast<T *>(_tousertype(L, index, type));
112  }
113 
114 
115 
125  template<typename T>
126  T *tousertype (lua_State *L, const char *variableName, const char *type) {
127  lua_getglobal(L, variableName);
128  if (lua_isnil(L,-1))
129  return nullptr;
130 
131  return tousertype<T>(L, -1, type);
132  }
133 
134  AGXLUA_EXPORT std::string getStackTrace( lua_State *L );
135  AGXLUA_EXPORT void error(lua_State* L, const char* msg, tolua_Error* err);
136 
137 
147  AGXLUA_EXPORT int tableToRealPairVector(lua_State* L);
148 
149 
159  AGXLUA_EXPORT int tableToRealVector(lua_State* L);
160 
161 }
162 #endif
163 
164 #endif
#define AGXLUA_EXPORT
Definition: agxLua/export.h:42