[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: metatables [newbie]
- From: "anderson" <anderson@...>
- Date: Wed, 18 Jun 2003 01:24:49 +0800
I'm having trouble understanding metatables for Object Orientated C++ like
classes (note I'm looking at this from the C++ side of things). Luna by
Lenny Palozzi is helpful for version 4, but I can't figure out the changes I
need to make for v5. I can't seem to find any good sample code for v5
metatables. The manual doesn't seem to be very helpful to me (and it's
probably just me) in respect to metatables. A sample would be nice,
including how to add events and values.
How do I add members (properties/methods)?
How do I write/receive a index/newindex member?
How do I use set/getmetatable to change the metatable?
I've placed some code below which is derived from the Luna example code. I
hate copying code this closely, but I'm having understanding this.
//The code below doesn't work
//This is what I have so far (it's basically the Luna code with a few
template <class T>
lua_State* L = getLua(); //Get the lua state
Register(L); //register class
// register class T
static void Register(lua_State* L)
//I'm not worried about the GC stuff at the moment, the object can
take care of this themselves.
// member function dispatcher
static int thunk(lua_State* L)
int i = static_cast<int>(lua_tonumber(L,-1));
T* obj = static_cast<T*>(lua_touserdata(L,-1));
//???Here I should be calling the getters and setters of the object,
but I don't know how to determine if we are getting/setting yet???
// constructs T objects
static int constructor(lua_State* L)
//Create a new object of type T
T* obj= new T;
//Create the tables
lua_newtable(L); // new table object
lua_pushnumber(L, 0); // userdata obj at index 0
lua_pushlightuserdata(L, obj); //???This is probably incorrect???
char * extractedName;
//Loop through each float type in the class, and add that member
(should add both the getter and setter)
for (name=0; name < obj->nOfAttribs(P_FLOAT); ++name)
//Get the name of the function
extractedName = obj->getAName(P_FLOAT, name);
//Push that on to the lua stack
lua_pushcclosure(L, &Luna<T>::thunk, 1);