From a1480ef7d88ff82bc4e04e564463fe1ac9856889 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 9 Apr 2009 10:57:00 +0200 Subject: [PATCH] luaobject: import Signed-off-by: Julien Danjou --- common/luaobject.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++ luaa.h | 12 +------ 2 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 common/luaobject.h diff --git a/common/luaobject.h b/common/luaobject.h new file mode 100644 index 00000000..bbd65bc6 --- /dev/null +++ b/common/luaobject.h @@ -0,0 +1,87 @@ +/* + * luaobject.h - useful functions for handling Lua objects + * + * Copyright © 2009 Julien Danjou + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#ifndef AWESOME_COMMON_LUAOBJECT +#define AWESOME_COMMON_LUAOBJECT + +#include +#include "common/array.h" + +/** Type for Lua references */ +typedef int luaA_ref; + +static inline int +luaA_settype(lua_State *L, const char *type) +{ + luaL_getmetatable(L, type); + lua_setmetatable(L, -2); + return 1; +} + +DO_ARRAY(luaA_ref, luaA_ref, DO_NOTHING) + +#define LUA_OBJECT_FUNCS(type, prefix, lua_type) \ + static inline type * \ + prefix##_new(lua_State *L) \ + { \ + type *p = lua_newuserdata(L, sizeof(type)); \ + p_clear(p, 1); \ + luaA_settype(L, lua_type); \ + return p; \ + } \ + \ + static inline int \ + prefix##_push(lua_State *L, type *item) \ + { \ + if(item) \ + { \ + assert(item->refs.len); \ + lua_rawgeti(L, LUA_REGISTRYINDEX, item->refs.tab[0]); \ + } \ + else \ + lua_pushnil(L); \ + return 1; \ + } \ + \ + static inline type * \ + prefix##_ref(lua_State *L) \ + { \ + if(lua_isnil(L, -1)) \ + return NULL; \ + type *item = luaL_checkudata(L, -1, lua_type); \ + luaA_ref_array_append(&item->refs, luaL_ref(L, LUA_REGISTRYINDEX)); \ + return item; \ + } \ + \ + static inline void \ + prefix##_unref(lua_State *L, type *item) \ + { \ + if(item) \ + { \ + assert(items->refs.len); \ + luaL_unref(L, LUA_REGISTRYINDEX, item->refs.tab[0]); \ + luaA_ref_array_take(&item->refs, 0); \ + } \ + } + +#endif + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/luaa.h b/luaa.h index 9f6f1fee..8efa082e 100644 --- a/luaa.h +++ b/luaa.h @@ -33,9 +33,7 @@ #include "draw.h" #include "common/util.h" - -/** Type for Lua function */ -typedef int luaA_ref; +#include "common/luaobject.h" #define luaA_deprecate(L, repl) \ luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \ @@ -218,14 +216,6 @@ luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def) return b; } -static inline int -luaA_settype(lua_State *L, const char *type) -{ - luaL_getmetatable(L, type); - lua_setmetatable(L, -2); - return 1; -} - /** Push a area type to a table on stack. * \param L The Lua VM state. * \param geometry The area geometry to push.