2003-05-25 03:54:13 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Auxiliar routines for class hierarchy manipulation
|
|
|
|
*
|
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2003-06-09 20:23:40 +02:00
|
|
|
#include "auxiliar.h"
|
2003-05-25 03:54:13 +02:00
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Exported functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Creates a new class. A class has methods given by the func array and the
|
|
|
|
* field 'class' tells the object class. The table 'group' list the class
|
|
|
|
* groups the object belongs to.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void aux_newclass(lua_State *L, const char *name, luaL_reg *func)
|
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_newtable(L);
|
2003-05-25 03:54:13 +02:00
|
|
|
lua_pushstring(L, "__index");
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_openlib(L, NULL, func, 0);
|
|
|
|
lua_pushstring(L, "class");
|
|
|
|
lua_pushstring(L, name);
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_rawset(L, -3);
|
2003-05-25 03:54:13 +02:00
|
|
|
lua_pushstring(L, "group");
|
|
|
|
lua_newtable(L);
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_rawset(L, -3);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Add group to object list of groups.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void aux_add2group(lua_State *L, const char *name, const char *group)
|
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
lua_pushstring(L, "__index");
|
|
|
|
lua_rawget(L, -2);
|
2003-05-25 03:54:13 +02:00
|
|
|
lua_pushstring(L, "group");
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_rawget(L, -2);
|
2003-05-25 03:54:13 +02:00
|
|
|
lua_pushstring(L, group);
|
|
|
|
lua_pushnumber(L, 1);
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_rawset(L, -3);
|
|
|
|
lua_pop(L, 3);
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Get a userdata making sure the object belongs to a given class.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void *aux_checkclass(lua_State *L, const char *name, int objidx)
|
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
void *data = aux_getclassudata(L, name, objidx);
|
2003-05-25 03:54:13 +02:00
|
|
|
if (!data) {
|
|
|
|
char msg[45];
|
|
|
|
sprintf(msg, "%.35s expected", name);
|
|
|
|
luaL_argerror(L, objidx, msg);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Get a userdata making sure the object belongs to a given group.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void *aux_checkgroup(lua_State *L, const char *group, int objidx)
|
|
|
|
{
|
|
|
|
void *data = aux_getgroupudata(L, group, objidx);
|
|
|
|
if (!data) {
|
|
|
|
char msg[45];
|
|
|
|
sprintf(msg, "%.35s expected", group);
|
|
|
|
luaL_argerror(L, objidx, msg);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Set object class.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void aux_setclass(lua_State *L, const char *name, int objidx)
|
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
2003-05-25 03:54:13 +02:00
|
|
|
if (objidx < 0) objidx--;
|
|
|
|
lua_setmetatable(L, objidx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Internal functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Get a userdata if object belongs to a given group.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-09 20:23:40 +02:00
|
|
|
void *aux_getgroupudata(lua_State *L, const char *group, int objidx)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
if (!lua_getmetatable(L, objidx))
|
|
|
|
return NULL;
|
|
|
|
lua_pushstring(L, "__index");
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
if (!lua_istable(L, -1)) {
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-05-25 03:54:13 +02:00
|
|
|
lua_pushstring(L, "group");
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_rawget(L, -2);
|
|
|
|
if (!lua_istable(L, -1)) {
|
|
|
|
lua_pop(L, 3);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lua_pushstring(L, group);
|
|
|
|
lua_rawget(L, -2);
|
2003-05-25 03:54:13 +02:00
|
|
|
if (lua_isnil(L, -1)) {
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_pop(L, 4);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lua_pop(L, 4);
|
|
|
|
return lua_touserdata(L, objidx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Get a userdata if object belongs to a given class.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void *aux_getclassudata(lua_State *L, const char *group, int objidx)
|
|
|
|
{
|
|
|
|
if (!lua_getmetatable(L, objidx))
|
|
|
|
return NULL;
|
|
|
|
lua_pushstring(L, "__index");
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
if (!lua_istable(L, -1)) {
|
2003-05-25 03:54:13 +02:00
|
|
|
lua_pop(L, 2);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_pushstring(L, "class");
|
|
|
|
lua_rawget(L, -2);
|
2003-05-25 03:54:13 +02:00
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
lua_pop(L, 3);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lua_pop(L, 3);
|
|
|
|
return lua_touserdata(L, objidx);
|
|
|
|
}
|