move LuaBinding into namespace

This commit is contained in:
Chris Danford
2005-12-27 22:58:30 +00:00
parent 1f81db0cec
commit d507f3fd28
3 changed files with 23 additions and 20 deletions
+1 -1
View File
@@ -280,7 +280,7 @@ bool Actor::IsType( const CString &sType )
{ {
Lua *L = LUA->Get(); Lua *L = LUA->Get();
this->PushSelf( L ); this->PushSelf( L );
bool bRet = CheckLuaObjectType( L, lua_gettop(L), sType, false ); bool bRet = LuaBinding::CheckLuaObjectType( L, lua_gettop(L), sType, false );
lua_pop( L, 1 ); lua_pop( L, 1 );
LUA->Release( L ); LUA->Release( L );
+6 -6
View File
@@ -8,7 +8,7 @@
// types (eg. "Actor.x(GAMESTATE, 10)"), which will crash or cause corruption. // types (eg. "Actor.x(GAMESTATE, 10)"), which will crash or cause corruption.
// #define FAST_LUA // #define FAST_LUA
void CreateMethodsTable( lua_State *L, const CString &sName ) void LuaBinding::CreateMethodsTable( lua_State *L, const CString &sName )
{ {
lua_pushlstring( L, sName.data(), sName.size() ); lua_pushlstring( L, sName.data(), sName.size() );
lua_rawget( L, LUA_GLOBALSINDEX ); lua_rawget( L, LUA_GLOBALSINDEX );
@@ -26,7 +26,7 @@ void CreateMethodsTable( lua_State *L, const CString &sName )
* Get a userdata, and check that it's either szType or a type * Get a userdata, and check that it's either szType or a type
* derived from szType, by walking the __index chain. * derived from szType, by walking the __index chain.
*/ */
bool CheckLuaObjectType( lua_State *L, int narg, const char *szType, bool bOptional ) bool LuaBinding::CheckLuaObjectType( lua_State *L, int narg, const char *szType, bool bOptional )
{ {
#if defined(FAST_LUA) #if defined(FAST_LUA)
if( bOptional ) if( bOptional )
@@ -77,7 +77,7 @@ bool CheckLuaObjectType( lua_State *L, int narg, const char *szType, bool bOptio
} }
} }
bool GetGlobalTable( Lua *L, bool bCreate ) static bool GetGlobalTable( Lua *L, bool bCreate )
{ {
lua_pushstring( L, "userdatas" ); lua_pushstring( L, "userdatas" );
lua_rawget( L, LUA_REGISTRYINDEX ); lua_rawget( L, LUA_REGISTRYINDEX );
@@ -99,7 +99,7 @@ bool GetGlobalTable( Lua *L, bool bCreate )
/* The object is on the stack. It's either a table or a userdata. /* The object is on the stack. It's either a table or a userdata.
* If needed, associate the metatable; if a table, also add it to * If needed, associate the metatable; if a table, also add it to
* the userdata table. */ * the userdata table. */
void ApplyDerivedType( Lua *L, const CString &sClassName, void *pSelf ) void LuaBinding::ApplyDerivedType( Lua *L, const CString &sClassName, void *pSelf )
{ {
int iTable = lua_gettop( L ); int iTable = lua_gettop( L );
@@ -147,7 +147,7 @@ LuaClass::LuaClass()
m_pSelf = NULL; m_pSelf = NULL;
} }
void *GetUserdataFromGlobalTable( Lua *L, const char *szType, int iArg ) void *LuaBinding::GetUserdataFromGlobalTable( Lua *L, const char *szType, int iArg )
{ {
if( !GetGlobalTable(L, false) ) if( !GetGlobalTable(L, false) )
luaL_error( L, "stale %s referenced (object used but no longer exists)", szType ); luaL_error( L, "stale %s referenced (object used but no longer exists)", szType );
@@ -256,7 +256,7 @@ void LuaClass::Register()
{ {
Lua *L = LUA->Get(); Lua *L = LUA->Get();
this->PushSelf(L); this->PushSelf(L);
ApplyDerivedType( L, m_sClassName, m_pSelf ); LuaBinding::ApplyDerivedType( L, m_sClassName, m_pSelf );
LUA->Release( L ); LUA->Release( L );
/* To conserve memory, clear the class name. We only need it while restoring. */ /* To conserve memory, clear the class name. We only need it while restoring. */
+12 -9
View File
@@ -6,10 +6,13 @@
#include "LuaManager.h" #include "LuaManager.h"
class LuaReference; class LuaReference;
namespace LuaBinding
{
void CreateMethodsTable( lua_State *L, const CString &szName ); void CreateMethodsTable( lua_State *L, const CString &szName );
bool CheckLuaObjectType( lua_State *L, int narg, const char *szType, bool bOptional=false ); bool CheckLuaObjectType( lua_State *L, int narg, const char *szType, bool bOptional=false );
void *GetUserdataFromGlobalTable( Lua *L, const char *szType, int iArg ); void *GetUserdataFromGlobalTable( Lua *L, const char *szType, int iArg );
void ApplyDerivedType( Lua *L, const CString &sClassname, void *pSelf ); void ApplyDerivedType( Lua *L, const CString &sClassname, void *pSelf );
};
template <typename Type> template <typename Type>
class Luna class Luna
@@ -27,7 +30,7 @@ public:
static void Register( Lua *L ) static void Register( Lua *L )
{ {
/* Create the methods table, if it doesn't already exist. */ /* Create the methods table, if it doesn't already exist. */
CreateMethodsTable( L, m_sClassName ); LuaBinding::CreateMethodsTable( L, m_sClassName );
int methods = lua_gettop( L ); int methods = lua_gettop( L );
@@ -79,7 +82,7 @@ public:
// were called before the Register() of the base class; we'll fill // were called before the Register() of the base class; we'll fill
// it in when we get to it. // it in when we get to it.
lua_pushliteral( L, "__index" ); lua_pushliteral( L, "__index" );
CreateMethodsTable( L, m_sBaseClassName ); LuaBinding::CreateMethodsTable( L, m_sBaseClassName );
lua_settable( L, methods_metatable ); lua_settable( L, methods_metatable );
} }
@@ -96,7 +99,7 @@ public:
// Get userdata from the Lua stack and return a pointer to T object. // Get userdata from the Lua stack and return a pointer to T object.
static T *check( lua_State *L, int narg, bool bIsSelf = false ) static T *check( lua_State *L, int narg, bool bIsSelf = false )
{ {
if( !CheckLuaObjectType(L, narg, m_sClassName, true) ) if( !LuaBinding::CheckLuaObjectType(L, narg, m_sClassName, true) )
{ {
if( bIsSelf ) if( bIsSelf )
luaL_typerror( L, narg, m_sClassName ); luaL_typerror( L, narg, m_sClassName );
@@ -112,7 +115,7 @@ public:
/* The stack has a userdata or a table. If it's a table, look up the associated userdata. */ /* The stack has a userdata or a table. If it's a table, look up the associated userdata. */
if( lua_istable(L, narg) ) if( lua_istable(L, narg) )
{ {
return (T *) GetUserdataFromGlobalTable( L, m_sClassName, narg ); return (T *) LuaBinding::GetUserdataFromGlobalTable( L, m_sClassName, narg );
} }
else if( lua_isuserdata(L, narg) ) else if( lua_isuserdata(L, narg) )
{ {
@@ -169,8 +172,8 @@ private:
return 1; return 1;
} }
if( !CheckLuaObjectType(L, 1, m_sClassName) || if( !LuaBinding::CheckLuaObjectType(L, 1, m_sClassName) ||
!CheckLuaObjectType(L, 2, m_sClassName) ) !LuaBinding::CheckLuaObjectType(L, 2, m_sClassName) )
{ {
lua_pushboolean( L, false ); lua_pushboolean( L, false );
} }
@@ -228,17 +231,17 @@ protected:
#define LUA_REGISTER_INSTANCED_BASE_CLASS( T ) \ #define LUA_REGISTER_INSTANCED_BASE_CLASS( T ) \
LUA_REGISTER_CLASS_BASIC( T, none ) \ LUA_REGISTER_CLASS_BASIC( T, none ) \
template<> void Luna<T>::PushObject( Lua *L, T* p ) { p->m_pLuaInstance->PushSelf( L ); } \ template<> void Luna<T>::PushObject( Lua *L, T* p ) { p->m_pLuaInstance->PushSelf( L ); } \
void T::PushSelf( lua_State *L ) { Luna<T>::PushObject( L, this ); ApplyDerivedType( L, #T, this ); } void T::PushSelf( lua_State *L ) { Luna<T>::PushObject( L, this ); LuaBinding::ApplyDerivedType( L, #T, this ); }
#define LUA_REGISTER_CLASS( T ) \ #define LUA_REGISTER_CLASS( T ) \
LUA_REGISTER_CLASS_BASIC( T, none ) \ LUA_REGISTER_CLASS_BASIC( T, none ) \
template<> void Luna<T>::PushObject( Lua *L, T* p ) { void **pData = (void **) lua_newuserdata( L, sizeof(void *) ); *pData = p; } \ template<> void Luna<T>::PushObject( Lua *L, T* p ) { void **pData = (void **) lua_newuserdata( L, sizeof(void *) ); *pData = p; } \
void T::PushSelf( lua_State *L ) { Luna<T>::PushObject( L, this ); ApplyDerivedType( L, #T, this ); } void T::PushSelf( lua_State *L ) { Luna<T>::PushObject( L, this ); LuaBinding::ApplyDerivedType( L, #T, this ); }
#define LUA_REGISTER_DERIVED_CLASS( T, B ) \ #define LUA_REGISTER_DERIVED_CLASS( T, B ) \
LUA_REGISTER_CLASS_BASIC( T, B ) \ LUA_REGISTER_CLASS_BASIC( T, B ) \
template<> void Luna<T>::PushObject( Lua *L, T* p ) { Luna<B>::PushObject( L, p ); } \ template<> void Luna<T>::PushObject( Lua *L, T* p ) { Luna<B>::PushObject( L, p ); } \
void T::PushSelf( lua_State *L ) { Luna<B>::PushObject( L, this ); ApplyDerivedType( L, #T, this ); } void T::PushSelf( lua_State *L ) { Luna<B>::PushObject( L, this ); LuaBinding::ApplyDerivedType( L, #T, this ); }
#define LUA_REGISTER_CLASS_BASIC( T, B ) \ #define LUA_REGISTER_CLASS_BASIC( T, B ) \
template<> const char *Luna<T>::m_sClassName = #T; \ template<> const char *Luna<T>::m_sClassName = #T; \