AGX Dynamics  2.35.0.1
LuaCall.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_LUACALL_H
18 #define AGXLUA_LUACALL_H
19 
20 #include <agx/config/AGX_USE_LUA.h>
21 #include <agx/config.h>
22 
23 #if AGX_USE_LUA()
24 #include <sstream>
25 #include <cassert>
26 #include <agxLua/export.h>
27 #include <agxLua/luaUtils.h>
28 #include <agxStream/Serializable.h>
29 #include <agxUnit/UnitMacros.h>
31 
32 
33 namespace agxLua
34 {
35 
78  class AGXLUA_EXPORT LuaCall
79  {
80  public:
81 
89  LuaCall( lua_State *lua, const std::string& type, void *this_ptr, const std::string& method);
90 
91  LuaCall( lua_State *lua, const std::string& functionName );
92 
93  LuaCall( lua_State *lua, const agxStream::ClassInformation &classInfo, const void *this_ptr);
94 
95  // Pop the stack of this methodcall, but only if it was a valid call!
96  ~LuaCall();
97 
99  bool isValid() {
100  return m_valid;
101  }
102 
104  void pushInteger ( int value ) {
105  m_num_args++;
106  lua_pushinteger( m_lua, value );
107  }
108 
110  void pushValue ( int lo ) {
111  m_num_args++;
112  lua_pushvalue ( m_lua, lo );
113  }
114 
115 
117  void pushBoolean ( bool value ) {
118  m_num_args++;
119  lua_pushboolean ( m_lua, value );
120  }
121 
123  void pushNumber ( double value ) {
124  m_num_args++;
125  lua_pushnumber ( m_lua, value );
126  }
127 
128 
130  void pushString ( const std::string& value ) {
131  m_num_args++;
132  agxLua::pushstring ( m_lua, value.c_str() );
133  }
134 
135 
137  bool pushUsertype ( void* value, const char *type ) {
138  m_num_args++;
139  return agxLua::pushusertype( m_lua, value, type );
140  }
141 
142 
144 // void pushUsertype ( void* value, const std::string& type) {
145 // m_num_args++;
146 // tolua_pushusertype (m_lua, value, type.c_str());
147 // }
148 
156  bool call( int num_return_values );
157 
159  const std::string& getLastError() const;
160 
162  std::string getStackTrace() const;
163 
164 
165  private:
166 
167  lua_State *m_lua;
168  unsigned int m_num_args;
169  bool m_valid;
170  bool m_isFunction;
171  std::string m_errorMessage;
172  };
173 
174 
175 
176 } // namespace agxLua
177 
178 #define AGXLUA_LUACALL_CLASS_DECLARE( LC ) agxLua::LuaCall LC( m_lua, agxStream::ClassInformation( AGX_FUNCTION ), this );
179 
180 
181 #define AGXLUA_REPORT_ERROR(errorMessage) \
182  if (g_luaScriptManager->getTreatInvalidAsErrors() || agxUnit::isUnittestingEnabled()) \
183  { \
184  LOGGER_ERROR() << (errorMessage) << std::endl << LOGGER_END(); \
185  } \
186  else \
187  { \
188  LOGGER_WARNING() << (errorMessage) << std::endl << LOGGER_END(); \
189  g_luaScriptManager->setLastErrorMessage(errorMessage); \
190  }
191 
192 
193 #define AGXLUA_LUACALL_CLASS_CALL( N ) \
194  if (!lc.call(N)) \
195  { \
196  AGXLUA_REPORT_ERROR(lc.getLastError()) \
197  }
198 
199 
200 inline std::ostream& operator <<( std::ostream& ostr, const agxStream::ClassInformation &c )
201 {
202  ostr << c.getClassType() << "::" << c.method;
203  return ostr;
204 
205 }
206 #endif
207 
208 #endif
209 
std::ostream & operator<<(std::ostream &o, const agx::Vec6 &v)
Definition: Vec6.h:230
#define AGXLUA_EXPORT
Definition: agxLua/export.h:42
Class that extracts namespace, class name and method name when called with the AGX_FUNCTION macro.
std::string getClassType() const