luaclass: add support for new()

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-06-25 11:21:51 +02:00
parent 1300b16c1e
commit 45702de158
4 changed files with 47 additions and 15 deletions

View File

@ -302,4 +302,44 @@ luaA_class_newindex(lua_State *L)
return 0; return 0;
} }
/** Generic constructor function for objects.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
int
luaA_class_new(lua_State *L, lua_class_t *lua_class)
{
/* Check we have a table that should contains some properties */
luaA_checktable(L, 2);
/* Create a new object */
void *object = lua_class->allocator(L);
/* Push the first key before iterating */
lua_pushnil(L);
/* Iterate over the property keys */
while(lua_next(L, 2))
{
/* Check that the key is a string.
* We cannot call tostring blindly or Lua will convert a key that is a
* number TO A STRING, confusing lua_next() */
if(lua_isstring(L, -2))
{
/* Lookup the property using token */
size_t len;
const char *attr = lua_tolstring(L, -2, &len);
lua_class_property_t *prop =
lua_class_property_array_getbyid(&lua_class->properties,
a_tokenize(attr, len));
if(prop && prop->new)
prop->new(L, object);
}
/* Remove value */
lua_pop(L, 1);
}
return 1;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -72,6 +72,7 @@ void luaA_class_add_property(lua_class_t *, awesome_token_t, const char *,
int luaA_usemetatable(lua_State *, int, int); int luaA_usemetatable(lua_State *, int, int);
int luaA_class_index(lua_State *); int luaA_class_index(lua_State *);
int luaA_class_newindex(lua_State *); int luaA_class_newindex(lua_State *);
int luaA_class_new(lua_State *, lua_class_t *);
#define LUA_CLASS_FUNCS(prefix, lua_class) \ #define LUA_CLASS_FUNCS(prefix, lua_class) \
static inline int \ static inline int \

View File

@ -141,6 +141,12 @@ luaA_toudata(lua_State *L, int ud, const char *tname)
return p; return p;
} }
#define luaA_checktable(L, n) \
do { \
if(!lua_istable(L, n)) \
luaL_typerror(L, n, "table"); \
} while(0)
#endif #endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

15
luaa.h
View File

@ -35,21 +35,6 @@
luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \ luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
__FUNCTION__, repl) __FUNCTION__, repl)
#define DO_LUA_TOSTRING(type, prefix, lua_type) \
static int \
luaA_##prefix##_tostring(lua_State *L) \
{ \
type *p = luaL_checkudata(L, 1, lua_type); \
lua_pushfstring(L, lua_type ": %p", p); \
return 1; \
}
#define luaA_checktable(L, n) \
do { \
if(!lua_istable(L, n)) \
luaL_typerror(L, n, "table"); \
} while(0)
#define luaA_checkscreen(screen) \ #define luaA_checkscreen(screen) \
do { \ do { \
if(screen < 0 || screen >= globalconf.screens.len) \ if(screen < 0 || screen >= globalconf.screens.len) \