Lua Utils
Biblioteca utilitária para facilitar a integração de Lua com C++
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
LuaSimpleProxy< T > Class Template Reference

#include <luaProxy.h>

Inheritance diagram for LuaSimpleProxy< T >:
Inheritance graph
[legend]
Collaboration diagram for LuaSimpleProxy< T >:
Collaboration graph
[legend]

Public Member Functions

 LuaSimpleProxy (const T &obj)
 
T & get ()
 
void * getClassMetatableID ()
 
void populateMetatable (lua_State *L, int index)
 

Static Private Member Functions

static int l___gc (lua_State *L)
 Método de coleta de lixo. Deleta o proxy.
 

Private Attributes

_obj
 Objeto encapsulado.
 

Detailed Description

template<typename T>
class LuaSimpleProxy< T >

Encapsulates a C++ object so it can be retrieved in Lua C functions. This is the simplest possible implementation of the LuaProxy::Base interface, and its use is equally simple. Let's say you have a C++ object, myObj of type MyClass, that you want to assign to a global Lua variable also called myObj. Here's what you do:

MyClass myObj;
lua_pushstring(L, "myObj");
lua_settable(L, LUA_GLOBALSINDEX);

Let's now say that you are in a Lua C function, and you want to check that the first parameter is a C++ object of type MyClass. Here's what you do:

LuaSimpleProxy<MyClass> * proxy = LuaProxy::toObjectOfClass<LuaSimpleProxy<MyClass> >(L, 1);
if (proxy == NULL) {
// Oops! Object is of the wrong type.
}
// OK, we can use the MyClass object now.
proxy->get().myMethod();

When a LuaSimpleProxy's garbage collection metamethod is invoked, it will be deleted. Therefore, it is not necessary for you to delete the LuaSimpleProxy if you push it onto a Lua state.

The proxy will actually create a copy of the supplied object; i.e., it follows pass-by-value semantics. If you need pass-by-reference semantics, declare the proxy as containing a pointer, e.g.:

MyClass myObj;
lua_pushstring(L, "myObj");
LuaProxy::pushObject(L, new LuaSimpleProxy<MyClass *>(&myObj)); // Using pointers now!
lua_settable(L, LUA_GLOBALSINDEX);

Constructor & Destructor Documentation

◆ LuaSimpleProxy()

template<typename T>
LuaSimpleProxy< T >::LuaSimpleProxy ( const T &  obj)
inline

Encapsulates the supplied object in a Lua proxy.

Member Function Documentation

◆ get()

template<typename T>
T& LuaSimpleProxy< T >::get ( )
inline
Returns
The object represented by this proxy.

◆ getClassMetatableID()

template<typename T>
void* LuaSimpleProxy< T >::getClassMetatableID ( )
inlinevirtual

A scriptable object should return here a unique ID for its class. This will be used to identify the class's metatable once it has been created.

Warning
Objects of the same most-derived class must return the same ID! Objects of different most-derived classes must return different IDs!

Implements LuaProxy::Base.

◆ populateMetatable()

template<typename T>
void LuaSimpleProxy< T >::populateMetatable ( lua_State *  L,
int  index 
)
inlinevirtual

The value at index in the Lua stack is guaranteed to be a table (which may or may not be empty; see below). This method must fill it with events/metamethods (set functions and tables in the table using strings as keys) so that userdata representing objects of this class will behave appropriately.

For convenience, the metatable will already have an "__index" event set to an empty table, so you can start filling that out immediately.

Hint: Design your classes so that the first thing in the method's body being an invocation of __super::populateMetatable() ensures proper inheritance.

Implements LuaProxy::Base.


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