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
|
|
|
|
2005-01-31 08:05:27 +00:00
|
|
|
#ifndef LuaBinding_H
|
|
|
|
|
#define LuaBinding_H
|
|
|
|
|
|
2005-01-24 02:11:23 +00:00
|
|
|
#include "LuaManager.h"
|
|
|
|
|
|
2005-02-01 07:49:23 +00:00
|
|
|
template <typename T>
|
|
|
|
|
struct RegType
|
|
|
|
|
{
|
2005-02-01 07:50:26 +00:00
|
|
|
const char *name;
|
|
|
|
|
int (*mfunc)(T *p, lua_State *L);
|
2005-02-01 07:49:23 +00:00
|
|
|
};
|
2005-01-24 02:11:23 +00:00
|
|
|
|
2005-06-20 03:54:51 +00:00
|
|
|
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>
|
2005-01-31 08:05:27 +00:00
|
|
|
class Luna
|
|
|
|
|
{
|
2005-06-20 05:02:03 +00:00
|
|
|
protected:
|
|
|
|
|
typedef Type T;
|
|
|
|
|
|
|
|
|
|
private:
|
2005-02-01 07:50:26 +00:00
|
|
|
typedef struct { T *pT; } userdataType;
|
2005-06-20 03:54:51 +00:00
|
|
|
|
2005-01-24 02:11:23 +00:00
|
|
|
public:
|
2005-06-20 03:54:51 +00:00
|
|
|
|
2005-02-01 07:50:26 +00:00
|
|
|
static void Register(lua_State *L)
|
2005-02-01 07:49:23 +00:00
|
|
|
{
|
2005-06-20 03:54:51 +00:00
|
|
|
/* Create the methods table, if it doesn't already exist. */
|
|
|
|
|
CreateGlobalTable( L, m_sClassName );
|
|
|
|
|
|
2005-02-01 07:50:26 +00:00
|
|
|
int methods = lua_gettop(L);
|
|
|
|
|
|
2005-06-20 03:54:51 +00:00
|
|
|
/* Create a metatable for the userdata objects. */
|
2005-06-20 00:33:07 +00:00
|
|
|
luaL_newmetatable(L, m_sClassName);
|
2005-02-01 07:50:26 +00:00
|
|
|
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, "__index");
|
|
|
|
|
lua_pushvalue(L, methods);
|
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__tostring");
|
|
|
|
|
lua_pushcfunction(L, tostring_T);
|
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
2005-05-24 20:56:38 +00:00
|
|
|
lua_pushliteral(L, "__eq");
|
|
|
|
|
lua_pushcfunction(L, equal);
|
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
2005-02-01 07:50:26 +00:00
|
|
|
// fill method table with methods from class T
|
2005-02-21 17:29:49 +00:00
|
|
|
for (unsigned i=0; s_pvMethods && i<s_pvMethods->size(); i++ )
|
2005-02-01 07:50:26 +00:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2005-06-20 03:54:51 +00:00
|
|
|
|
|
|
|
|
/* Create a metatable for the methods table. */
|
|
|
|
|
lua_newtable( L );
|
|
|
|
|
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-02-01 07:50:26 +00:00
|
|
|
lua_pop(L, 2); // drop metatable and method table
|
|
|
|
|
}
|
2005-05-28 07:27:49 +00:00
|
|
|
|
2005-02-01 07:50:26 +00:00
|
|
|
// get userdata from Lua stack and return pointer to T object
|
2005-05-28 07:27:49 +00:00
|
|
|
static T *check( lua_State *L, int narg, bool bIsSelf = false )
|
2005-02-01 07:50:26 +00:00
|
|
|
{
|
2005-06-20 03:54:51 +00:00
|
|
|
if( !CheckType(L, narg, m_sClassName) )
|
2005-05-28 07:27:49 +00:00
|
|
|
{
|
|
|
|
|
if( bIsSelf )
|
2005-06-20 00:33:07 +00:00
|
|
|
luaL_typerror( L, narg, m_sClassName );
|
2005-05-28 07:27:49 +00:00
|
|
|
else
|
2005-06-20 00:33:07 +00:00
|
|
|
LuaHelpers::TypeError( L, narg, m_sClassName );
|
2005-05-28 07:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-20 03:54:51 +00:00
|
|
|
userdataType *pUserdata = static_cast<userdataType*>( lua_touserdata(L, narg) );
|
2005-05-28 07:27:49 +00:00
|
|
|
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
|
|
|
|
|
static int thunk(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
// stack has userdata, followed by method args
|
2005-05-28 07:27:49 +00:00
|
|
|
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
|
|
|
|
|
MyRegType *l = static_cast<MyRegType*>(lua_touserdata(L, lua_upvalueindex(1)));
|
|
|
|
|
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 )
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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-02-01 07:50:26 +00:00
|
|
|
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
|
2005-06-20 00:33:07 +00:00
|
|
|
luaL_getmetatable(L, m_sClassName); // lookup metatable in Lua registry
|
2005-02-01 07:50:26 +00:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
return 1; // userdata containing pointer to T object
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-01 07:49:23 +00:00
|
|
|
typedef RegType<T> MyRegType;
|
|
|
|
|
typedef vector<MyRegType> RegTypeVector;
|
2005-01-31 08:05:27 +00:00
|
|
|
static RegTypeVector *s_pvMethods;
|
2005-02-01 07:50:26 +00:00
|
|
|
|
2005-02-01 07:49:23 +00:00
|
|
|
static void CreateMethodsVector()
|
|
|
|
|
{
|
|
|
|
|
if(s_pvMethods==NULL)
|
|
|
|
|
s_pvMethods = new RegTypeVector;
|
|
|
|
|
}
|
2005-01-26 11:21:43 +00:00
|
|
|
private:
|
2005-06-20 03:54:51 +00:00
|
|
|
static const char *m_sClassName;
|
|
|
|
|
static const char *m_sBaseClassName;
|
2005-02-01 07:50:26 +00:00
|
|
|
|
|
|
|
|
static int tostring_T (lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
char buff[32];
|
|
|
|
|
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
|
|
|
|
|
T *obj = ud->pT;
|
|
|
|
|
sprintf(buff, "%p", obj);
|
2005-06-20 00:33:07 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2005-01-31 08:05:27 +00:00
|
|
|
#define LUA_REGISTER_CLASS( T ) \
|
2005-06-20 03:54:51 +00:00
|
|
|
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; \
|
2005-06-20 05:02:03 +00:00
|
|
|
Luna<T>::RegTypeVector* Luna<T>::s_pvMethods = NULL; \
|
|
|
|
|
static Luna##T registera; \
|
|
|
|
|
void T::PushSelf( lua_State *L ) { Luna##T::Push( L, this ); } \
|
2005-02-24 20:03:07 +00:00
|
|
|
/* 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-01-31 08:05:27 +00:00
|
|
|
|
|
|
|
|
#define ADD_METHOD( method_name ) \
|
2005-02-05 21:46:52 +00:00
|
|
|
{ Luna<T>::CreateMethodsVector(); RegType<T> r = {#method_name,method_name}; Luna<T>::s_pvMethods->push_back(r); }
|
2005-01-31 08:05:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#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.
|
|
|
|
|
*/
|