This commit is contained in:
Glenn Maynard
2005-06-20 09:53:02 +00:00
parent 6ac3eff751
commit 74828836cb
+37 -36
View File
@@ -16,53 +16,53 @@ protected:
struct RegType
{
const char *name;
const char *szName;
int (*mfunc)(T *p, lua_State *L);
};
typedef struct { T *pT; } userdataType;
public:
static void Register(lua_State *L)
static void Register( Lua *L )
{
/* Create the methods table, if it doesn't already exist. */
CreateGlobalTable( L, m_sClassName );
int methods = lua_gettop(L);
int methods = lua_gettop( L );
/* Create a metatable for the userdata objects. */
luaL_newmetatable(L, m_sClassName);
int metatable = lua_gettop(L);
luaL_newmetatable( L, m_sClassName );
int metatable = lua_gettop( L );
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methods);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
lua_pushliteral( L, "__metatable" );
lua_pushvalue( L, methods );
lua_settable( L, metatable ); // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__index");
lua_pushvalue(L, methods);
lua_settable(L, metatable);
lua_pushliteral( L, "__index" );
lua_pushvalue( L, methods );
lua_settable( L, metatable );
lua_pushliteral(L, "__tostring");
lua_pushcfunction(L, tostring_T);
lua_settable(L, metatable);
lua_pushliteral( L, "__tostring" );
lua_pushcfunction( L, tostring_T );
lua_settable( L, metatable );
lua_pushliteral(L, "__eq");
lua_pushcfunction(L, equal);
lua_settable(L, metatable);
lua_pushliteral( L, "__eq" );
lua_pushcfunction( L, equal );
lua_settable( L, metatable );
// fill method table with methods from class T
for (unsigned i=0; s_pvMethods && i<s_pvMethods->size(); i++ )
for( unsigned i=0; s_pvMethods && i < s_pvMethods->size(); i++ )
{
const RegType *l = &(*s_pvMethods)[i];
lua_pushstring(L, l->name);
lua_pushlightuserdata(L, (void*)l);
lua_pushcclosure(L, thunk, 1);
lua_settable(L, methods);
lua_pushstring( L, l->szName );
lua_pushlightuserdata( L, (void*)l );
lua_pushcclosure( L, thunk, 1 );
lua_settable( L, methods );
}
/* Create a metatable for the methods table. */
lua_newtable( L );
int methods_metatable = lua_gettop(L);
int methods_metatable = lua_gettop( L );
// Hide the metatable.
lua_pushliteral( L, "__metatable" );
@@ -87,7 +87,7 @@ public:
/* Set and pop the methods metatable. */
lua_setmetatable( L, methods );
lua_pop(L, 2); // drop metatable and method table
lua_pop( L, 2 ); // drop metatable and method table
}
// get userdata from Lua stack and return pointer to T object
@@ -107,7 +107,7 @@ public:
private:
// member function dispatcher
static int thunk(lua_State *L)
static int thunk( Lua *L )
{
// stack has userdata, followed by method args
T *obj = check( L, 1, true ); // get self
@@ -120,20 +120,20 @@ private:
/* Two objects are equal if the underlying object is the same. */
static int equal( lua_State *L )
{
userdataType *obj1 = static_cast<userdataType*>(lua_touserdata(L, 1));
userdataType *obj2 = static_cast<userdataType*>(lua_touserdata(L, 2));
userdataType *obj1 = static_cast<userdataType*>( lua_touserdata(L, 1) );
userdataType *obj2 = static_cast<userdataType*>( lua_touserdata(L, 2) );
lua_pushboolean( L, obj1->pT == obj2->pT );
return 1;
}
public:
// push a userdata containing a pointer to T object
static int Push(lua_State *L, T* p )
static int Push( Lua *L, T* p )
{
userdataType *ud = static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType)));
userdataType *ud = static_cast<userdataType*>( lua_newuserdata(L, sizeof(userdataType)) );
ud->pT = p; // store pointer to object in userdata
luaL_getmetatable(L, m_sClassName); // lookup metatable in Lua registry
lua_setmetatable(L, -2);
luaL_getmetatable( L, m_sClassName ); // lookup metatable in Lua registry
lua_setmetatable( L, -2 );
return 1; // userdata containing pointer to T object
}
@@ -141,8 +141,9 @@ public:
{
if( s_pvMethods == NULL )
s_pvMethods = new RegTypeVector;
RegType r = { szName, pFunc };
Luna<T>::s_pvMethods->push_back(r);
s_pvMethods->push_back(r);
}
private:
@@ -152,13 +153,13 @@ private:
static const char *m_sClassName;
static const char *m_sBaseClassName;
static int tostring_T (lua_State *L)
static int tostring_T( lua_State *L )
{
char buff[32];
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
userdataType *ud = static_cast<userdataType*>( lua_touserdata(L, 1) );
T *obj = ud->pT;
sprintf(buff, "%p", obj);
lua_pushfstring(L, "%s (%s)", m_sClassName, buff);
sprintf( buff, "%p", obj) ;
lua_pushfstring( L, "%s (%s)", m_sClassName, buff );
return 1;
}
};