Files
itgmania212121/stepmania/src/LuaBinding.h
T

208 lines
6.5 KiB
C++
Raw Normal View History

2005-06-20 05:05:55 +00:00
/* LuaBinding - helpers to expose Lua bindings for C++ classes. */
2005-01-24 02:11:23 +00:00
#ifndef LuaBinding_H
#define LuaBinding_H
2005-01-24 02:11:23 +00:00
#include "LuaManager.h"
void CreateGlobalTable( lua_State *L, const CString &szName );
bool CheckType( lua_State *L, int narg, const char *szType );
2005-06-20 05:02:03 +00:00
template <typename Type>
class Luna
{
2005-06-20 05:02:03 +00:00
protected:
typedef Type T;
2005-06-20 09:28:45 +00:00
struct RegType
{
2005-06-20 09:53:02 +00:00
const char *szName;
2005-06-20 09:28:45 +00:00
int (*mfunc)(T *p, lua_State *L);
};
2005-02-01 07:50:26 +00:00
typedef struct { T *pT; } userdataType;
2005-01-24 02:11:23 +00:00
public:
2005-06-20 09:53:02 +00:00
static void Register( Lua *L )
2005-02-01 07:49:23 +00:00
{
/* Create the methods table, if it doesn't already exist. */
CreateGlobalTable( L, m_sClassName );
2005-06-20 09:53:02 +00:00
int methods = lua_gettop( L );
2005-02-01 07:50:26 +00:00
/* Create a metatable for the userdata objects. */
2005-06-20 09:53:02 +00:00
luaL_newmetatable( L, m_sClassName );
int metatable = lua_gettop( L );
2005-02-01 07:50:26 +00:00
2005-06-20 09:53:02 +00:00
lua_pushliteral( L, "__metatable" );
lua_pushvalue( L, methods );
lua_settable( L, metatable ); // hide metatable from Lua getmetatable()
2005-02-01 07:50:26 +00:00
2005-06-20 09:53:02 +00:00
lua_pushliteral( L, "__index" );
lua_pushvalue( L, methods );
lua_settable( L, metatable );
2005-02-01 07:50:26 +00:00
2005-06-20 09:53:02 +00:00
lua_pushliteral( L, "__tostring" );
lua_pushcfunction( L, tostring_T );
lua_settable( L, metatable );
2005-02-01 07:50:26 +00:00
2005-06-20 09:53:02 +00:00
lua_pushliteral( L, "__eq" );
lua_pushcfunction( L, equal );
lua_settable( L, metatable );
2005-05-24 20:56:38 +00:00
2005-02-01 07:50:26 +00:00
// fill method table with methods from class T
2005-06-20 09:53:02 +00:00
for( unsigned i=0; s_pvMethods && i < s_pvMethods->size(); i++ )
2005-02-01 07:50:26 +00:00
{
2005-06-20 09:28:45 +00:00
const RegType *l = &(*s_pvMethods)[i];
2005-06-20 09:53:02 +00:00
lua_pushstring( L, l->szName );
lua_pushlightuserdata( L, (void*)l );
lua_pushcclosure( L, thunk, 1 );
lua_settable( L, methods );
2005-02-01 07:50:26 +00:00
}
/* Create a metatable for the methods table. */
lua_newtable( L );
2005-06-20 09:53:02 +00:00
int methods_metatable = lua_gettop( L );
// Hide the metatable.
lua_pushliteral( L, "__metatable" );
lua_pushstring( L, "(hidden)" );
lua_settable( L, methods_metatable );
if( strcmp(m_sBaseClassName, "none") )
{
// If this type has a base class, set the __index of this type
// to the base class. If the base class doesn't exist, we probably
// were called before the Register() of the base class; we'll fill
// it in when we get to it.
lua_pushliteral( L, "__index" );
CreateGlobalTable( L, m_sBaseClassName );
lua_settable( L, methods_metatable );
}
lua_pushliteral( L, "__type" );
lua_pushstring( L, m_sClassName );
lua_settable( L, methods_metatable );
/* Set and pop the methods metatable. */
lua_setmetatable( L, methods );
2005-06-20 09:53:02 +00:00
lua_pop( L, 2 ); // drop metatable and method table
2005-02-01 07:50:26 +00:00
}
2005-02-01 07:50:26 +00:00
// get userdata from Lua stack and return pointer to T object
static T *check( lua_State *L, int narg, bool bIsSelf = false )
2005-02-01 07:50:26 +00:00
{
if( !CheckType(L, narg, m_sClassName) )
{
if( bIsSelf )
2005-06-20 00:33:07 +00:00
luaL_typerror( L, narg, m_sClassName );
else
2005-06-20 00:33:07 +00:00
LuaHelpers::TypeError( L, narg, m_sClassName );
}
userdataType *pUserdata = static_cast<userdataType*>( lua_touserdata(L, narg) );
return pUserdata->pT; // pointer to T object
2005-02-01 07:50:26 +00:00
}
2005-01-24 02:11:23 +00:00
private:
2005-02-01 07:50:26 +00:00
// member function dispatcher
2005-06-20 09:53:02 +00:00
static int thunk( Lua *L )
2005-02-01 07:50:26 +00:00
{
// stack has userdata, followed by method args
T *obj = check( L, 1, true ); // get self
2005-02-01 07:50:26 +00:00
lua_remove(L, 1); // remove self so member function args start at index 1
// get member function from upvalue
2005-06-20 09:28:45 +00:00
RegType *l = static_cast<RegType*>(lua_touserdata(L, lua_upvalueindex(1)));
2005-02-01 07:50:26 +00:00
return (*(l->mfunc))(obj,L); // call member function
}
2005-05-24 20:56:38 +00:00
/* Two objects are equal if the underlying object is the same. */
static int equal( lua_State *L )
{
2005-06-20 09:53:02 +00:00
userdataType *obj1 = static_cast<userdataType*>( lua_touserdata(L, 1) );
userdataType *obj2 = static_cast<userdataType*>( lua_touserdata(L, 2) );
2005-05-24 20:56:38 +00:00
lua_pushboolean( L, obj1->pT == obj2->pT );
return 1;
}
2005-01-26 11:21:43 +00:00
public:
2005-06-20 04:42:11 +00:00
// push a userdata containing a pointer to T object
2005-06-20 09:53:02 +00:00
static int Push( Lua *L, T* p )
2005-02-01 07:50:26 +00:00
{
2005-06-20 09:53:02 +00:00
userdataType *ud = static_cast<userdataType*>( lua_newuserdata(L, sizeof(userdataType)) );
2005-02-01 07:50:26 +00:00
ud->pT = p; // store pointer to object in userdata
2005-06-20 09:53:02 +00:00
luaL_getmetatable( L, m_sClassName ); // lookup metatable in Lua registry
lua_setmetatable( L, -2 );
2005-02-01 07:50:26 +00:00
return 1; // userdata containing pointer to T object
}
2005-06-20 09:35:06 +00:00
static void AddMethod( const char *szName, int (*pFunc)(T *p, lua_State *L) )
{
if( s_pvMethods == NULL )
s_pvMethods = new RegTypeVector;
2005-06-20 09:53:02 +00:00
2005-06-20 09:35:06 +00:00
RegType r = { szName, pFunc };
2005-06-20 09:53:02 +00:00
s_pvMethods->push_back(r);
2005-06-20 09:35:06 +00:00
}
private:
2005-06-20 09:28:45 +00:00
typedef vector<RegType> RegTypeVector;
static RegTypeVector *s_pvMethods;
2005-06-20 09:35:06 +00:00
static const char *m_sClassName;
static const char *m_sBaseClassName;
2005-02-01 07:50:26 +00:00
2005-06-20 09:53:02 +00:00
static int tostring_T( lua_State *L )
2005-02-01 07:50:26 +00:00
{
char buff[32];
2005-06-20 09:53:02 +00:00
userdataType *ud = static_cast<userdataType*>( lua_touserdata(L, 1) );
2005-02-01 07:50:26 +00:00
T *obj = ud->pT;
2005-06-20 09:53:02 +00:00
sprintf( buff, "%p", obj) ;
lua_pushfstring( L, "%s (%s)", m_sClassName, buff );
2005-02-01 07:50:26 +00:00
return 1;
}
2005-01-24 02:11:23 +00:00
};
#define LUA_REGISTER_CLASS( T ) \
LUA_REGISTER_DERIVED_CLASS( T, none )
#define LUA_REGISTER_DERIVED_CLASS( T, B ) \
template<> const char *Luna<T>::m_sClassName = #T; \
template<> const char *Luna<T>::m_sBaseClassName = #B; \
template<> Luna<T>::RegTypeVector* Luna<T>::s_pvMethods = NULL; \
2005-06-20 05:02:03 +00:00
static Luna##T registera; \
void T::PushSelf( lua_State *L ) { Luna##T::Push( L, this ); } \
/* Call PushSelf, so we always call the derived Luna<T>::Push. */ \
namespace LuaHelpers { template<> void Push( T *pObject, lua_State *L ) { pObject->PushSelf( L ); } }
2005-06-20 09:42:24 +00:00
#define ADD_METHOD( method_name ) AddMethod( #method_name, method_name );
#endif
2005-01-24 02:11:23 +00:00
/*
2005-06-20 05:08:51 +00:00
* (c) 2001-2005 Peter Shook, Chris Danford, Glenn Maynard
2005-01-24 02:11:23 +00:00
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/