AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
agxLua::LuaCall Class Reference

The class encapsulates method calls from C++ to lua. More...

#include <LuaCall.h>

Public Member Functions

 LuaCall (lua_State *lua, const std::string &type, void *this_ptr, const std::string &method)
 Constructor. More...
 
 LuaCall (lua_State *lua, const std::string &functionName)
 
 LuaCall (lua_State *lua, const agxStream::ClassInformation &classInfo, const void *this_ptr)
 
 ~LuaCall ()
 
bool call (int num_return_values)
 Push a pointer to a known usertype. More...
 
const std::string & getLastError () const
 Return message string of last error message (from previous call to call() method). "" if no error occurred. More...
 
std::string getStackTrace () const
 Return the current stack trace. More...
 
bool isValid ()
 Returns true if a call to the method can safely be done ( the method exists in lua) More...
 
void pushBoolean (bool value)
 Push a boolean as an argument. More...
 
void pushInteger (int value)
 Push an integer as an argument. More...
 
void pushNumber (double value)
 Push a number as an argument. More...
 
void pushString (const std::string &value)
 Push a string as an argument. More...
 
bool pushUsertype (void *value, const char *type)
 Push a pointer of userdata as an argument. More...
 
void pushValue (int lo)
 Push a value as an argument. More...
 

Detailed Description

The class encapsulates method calls from C++ to lua.

The usage of this class enables a call such as:

return_value = this->method(args...);

The constructor takes a pointer to an object, this is the "this" pointer which is of a known lua user type.
The name of the method is given as a string parameter.

By default the constructor will Throw an exception if the function does not exist.

IMPORTANT: Make sure you make a call to LuaCall:call() if isValid() returns true
after the constructor have been called. Otherwise the lua stack would most probably
be corrupted.

Example usage:
 MyClass::callLua(MyArgument& arg) {

    LuaCall lc(luaVM, this, "MyClass", "luaMethod", false);
    if (!lc.valid()) {
      cerr << "Invalid call to non existing method";
      return;
    }

The argument to the method lc.pushUsertype(&arg, "MyArgument"); lc.call(0); //Make the call, return 0 arguments }

The lua code for this setup would then be:

mc= MyClass:new() function mc:luaMethod(arg) print("The call succeeded") end

ma = MyArgument:new() mc:callLua(ma) // Called from luaClass that encapsulates method calls from C++ to lua

Definition at line 78 of file LuaCall.h.

Constructor & Destructor Documentation

agxLua::LuaCall::LuaCall ( lua_State *  lua,
const std::string &  type,
void *  this_ptr,
const std::string &  method 
)

Constructor.

Parameters
lua- A pointer to the VM of lua
type- A string that is the type of the this pointer (must be known by lua as usertype)
this_ptr- A void pointer to this->
method- A string containing the name of the method to be called
agxLua::LuaCall::LuaCall ( lua_State *  lua,
const std::string &  functionName 
)
agxLua::LuaCall::LuaCall ( lua_State *  lua,
const agxStream::ClassInformation &  classInfo,
const void *  this_ptr 
)
agxLua::LuaCall::~LuaCall ( )

Member Function Documentation

bool agxLua::LuaCall::call ( int  num_return_values)

Push a pointer to a known usertype.

Perform the actual call. If the function call is valid (the method exists in lua) a call will be made with the pushed arguments.

Parameters
num_return_values- Specifies the number of arguments that will be returned by the lua method
Returns
true - If call was successful, false if failure occurred. getLastError() contain error message.
const std::string& agxLua::LuaCall::getLastError ( ) const

Return message string of last error message (from previous call to call() method). "" if no error occurred.

std::string agxLua::LuaCall::getStackTrace ( ) const

Return the current stack trace.

bool agxLua::LuaCall::isValid ( )
inline

Returns true if a call to the method can safely be done ( the method exists in lua)

Definition at line 99 of file LuaCall.h.

void agxLua::LuaCall::pushBoolean ( bool  value)
inline

Push a boolean as an argument.

Definition at line 117 of file LuaCall.h.

void agxLua::LuaCall::pushInteger ( int  value)
inline

Push an integer as an argument.

Definition at line 104 of file LuaCall.h.

void agxLua::LuaCall::pushNumber ( double  value)
inline

Push a number as an argument.

Definition at line 123 of file LuaCall.h.

void agxLua::LuaCall::pushString ( const std::string &  value)
inline

Push a string as an argument.

Definition at line 130 of file LuaCall.h.

References agxLua::pushstring().

bool agxLua::LuaCall::pushUsertype ( void *  value,
const char *  type 
)
inline

Push a pointer of userdata as an argument.

Definition at line 137 of file LuaCall.h.

References agxLua::pushusertype().

void agxLua::LuaCall::pushValue ( int  lo)
inline

Push a value as an argument.

Definition at line 110 of file LuaCall.h.


The documentation for this class was generated from the following file: