style cleanup
This commit is contained in:
+79
-74
@@ -24,84 +24,88 @@ extern "C"
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct RegType
|
struct RegType
|
||||||
{
|
{
|
||||||
const char *name;
|
const char *name;
|
||||||
int (*mfunc)(T *p, lua_State *L);
|
int (*mfunc)(T *p, lua_State *L);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Luna
|
class Luna
|
||||||
{
|
{
|
||||||
typedef struct { T *pT; } userdataType;
|
typedef struct { T *pT; } userdataType;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
static void Register(lua_State *L) {
|
static void Register(lua_State *L)
|
||||||
lua_newtable(L);
|
|
||||||
int methods = lua_gettop(L);
|
|
||||||
|
|
||||||
luaL_newmetatable(L, s_className);
|
|
||||||
int metatable = lua_gettop(L);
|
|
||||||
|
|
||||||
// store method table in globals so that
|
|
||||||
// scripts can add functions written in Lua.
|
|
||||||
lua_pushstring(L, s_className);
|
|
||||||
lua_pushvalue(L, methods);
|
|
||||||
lua_settable(L, LUA_GLOBALSINDEX);
|
|
||||||
|
|
||||||
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, "__tostring");
|
|
||||||
lua_pushcfunction(L, tostring_T);
|
|
||||||
lua_settable(L, metatable);
|
|
||||||
|
|
||||||
// fill method table with methods from class T
|
|
||||||
for (unsigned i=0; i<s_pvMethods->size(); i++ )
|
|
||||||
{
|
{
|
||||||
const MyRegType *l = &(*s_pvMethods)[i];
|
lua_newtable(L);
|
||||||
lua_pushstring(L, l->name);
|
int methods = lua_gettop(L);
|
||||||
lua_pushlightuserdata(L, (void*)l);
|
|
||||||
lua_pushcclosure(L, thunk, 1);
|
|
||||||
lua_settable(L, methods);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pop(L, 2); // drop metatable and method table
|
luaL_newmetatable(L, s_className);
|
||||||
}
|
int metatable = lua_gettop(L);
|
||||||
|
|
||||||
// get userdata from Lua stack and return pointer to T object
|
// store method table in globals so that
|
||||||
static T *check(lua_State *L, int narg) {
|
// scripts can add functions written in Lua.
|
||||||
userdataType *ud =
|
lua_pushstring(L, s_className);
|
||||||
static_cast<userdataType*>(luaL_checkudata(L, narg, s_className));
|
lua_pushvalue(L, methods);
|
||||||
if(!ud) luaL_typerror(L, narg, s_className);
|
lua_settable(L, LUA_GLOBALSINDEX);
|
||||||
return ud->pT; // pointer to T object
|
|
||||||
}
|
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, "__tostring");
|
||||||
|
lua_pushcfunction(L, tostring_T);
|
||||||
|
lua_settable(L, metatable);
|
||||||
|
|
||||||
|
// fill method table with methods from class T
|
||||||
|
for (unsigned i=0; i<s_pvMethods->size(); i++ )
|
||||||
|
{
|
||||||
|
const MyRegType *l = &(*s_pvMethods)[i];
|
||||||
|
lua_pushstring(L, l->name);
|
||||||
|
lua_pushlightuserdata(L, (void*)l);
|
||||||
|
lua_pushcclosure(L, thunk, 1);
|
||||||
|
lua_settable(L, methods);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pop(L, 2); // drop metatable and method table
|
||||||
|
}
|
||||||
|
|
||||||
|
// get userdata from Lua stack and return pointer to T object
|
||||||
|
static T *check(lua_State *L, int narg)
|
||||||
|
{
|
||||||
|
userdataType *ud =
|
||||||
|
static_cast<userdataType*>(luaL_checkudata(L, narg, s_className));
|
||||||
|
if(!ud) luaL_typerror(L, narg, s_className);
|
||||||
|
return ud->pT; // pointer to T object
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
// member function dispatcher
|
// member function dispatcher
|
||||||
static int thunk(lua_State *L) {
|
static int thunk(lua_State *L)
|
||||||
// stack has userdata, followed by method args
|
{
|
||||||
T *obj = check(L, 1); // get 'self', or if you prefer, 'this'
|
// stack has userdata, followed by method args
|
||||||
lua_remove(L, 1); // remove self so member function args start at index 1
|
T *obj = check(L, 1); // get 'self', or if you prefer, 'this'
|
||||||
// get member function from upvalue
|
lua_remove(L, 1); // remove self so member function args start at index 1
|
||||||
MyRegType *l = static_cast<MyRegType*>(lua_touserdata(L, lua_upvalueindex(1)));
|
// get member function from upvalue
|
||||||
return (*(l->mfunc))(obj,L); // call member function
|
MyRegType *l = static_cast<MyRegType*>(lua_touserdata(L, lua_upvalueindex(1)));
|
||||||
}
|
return (*(l->mfunc))(obj,L); // call member function
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// create a new T object and
|
// create a new T object and
|
||||||
// push onto the Lua stack a userdata containing a pointer to T object
|
// push onto the Lua stack a userdata containing a pointer to T object
|
||||||
static int Push(lua_State *L, T* p ) {
|
static int Push(lua_State *L, T* p )
|
||||||
userdataType *ud = static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType)));
|
{
|
||||||
ud->pT = p; // store pointer to object in userdata
|
userdataType *ud = static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType)));
|
||||||
luaL_getmetatable(L, s_className); // lookup metatable in Lua registry
|
ud->pT = p; // store pointer to object in userdata
|
||||||
lua_setmetatable(L, -2);
|
luaL_getmetatable(L, s_className); // lookup metatable in Lua registry
|
||||||
return 1; // userdata containing pointer to T object
|
lua_setmetatable(L, -2);
|
||||||
}
|
return 1; // userdata containing pointer to T object
|
||||||
|
}
|
||||||
|
|
||||||
typedef RegType<T> MyRegType;
|
typedef RegType<T> MyRegType;
|
||||||
typedef vector<MyRegType> RegTypeVector;
|
typedef vector<MyRegType> RegTypeVector;
|
||||||
@@ -115,21 +119,22 @@ public:
|
|||||||
private:
|
private:
|
||||||
static const char s_className[];
|
static const char s_className[];
|
||||||
|
|
||||||
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));
|
char buff[32];
|
||||||
T *obj = ud->pT;
|
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
|
||||||
sprintf(buff, "%p", obj);
|
T *obj = ud->pT;
|
||||||
lua_pushfstring(L, "%s (%s)", s_className, buff);
|
sprintf(buff, "%p", obj);
|
||||||
return 1;
|
lua_pushfstring(L, "%s (%s)", s_className, buff);
|
||||||
}
|
return 1;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#define LUA_REGISTER_CLASS( T ) \
|
#define LUA_REGISTER_CLASS( T ) \
|
||||||
const char Luna##T<T>::s_className[] = #T; \
|
const char Luna##T<T>::s_className[] = #T; \
|
||||||
Luna##T<T>::RegTypeVector* Luna##T<T>::s_pvMethods = NULL; \
|
Luna##T<T>::RegTypeVector* Luna##T<T>::s_pvMethods = NULL; \
|
||||||
static Luna##T<T> registera; \
|
static Luna##T<T> registera; \
|
||||||
void T::PushSelf( lua_State *L ) { Luna##T<T>::Push( L, this ); }
|
void T::PushSelf( lua_State *L ) { Luna##T<T>::Push( L, this ); }
|
||||||
|
|
||||||
#define ADD_METHOD( method_name ) \
|
#define ADD_METHOD( method_name ) \
|
||||||
|
|||||||
Reference in New Issue
Block a user