diff --git a/keybinding.c b/keybinding.c index b2d415f7b..54795dad4 100644 --- a/keybinding.c +++ b/keybinding.c @@ -132,16 +132,7 @@ luaA_keybinding_remove(lua_State *L) return 0; } -/** Handle keybinding garbage collection. - */ -static int -luaA_keybinding_gc(lua_State *L) -{ - keybinding_t **keybinding = luaA_checkudata(L, 1, "keybinding"); - keybinding_unref(keybinding); - keybinding = NULL; - return 0; -} +DO_LUA_GC(keybinding_t, keybinding, "keybinding", keybinding_unref) /** Convert a keybinding to a printable string. * \return A string. diff --git a/lua.h b/lua.h index ace031f6e..9bab2117f 100644 --- a/lua.h +++ b/lua.h @@ -37,6 +37,16 @@ typedef enum /** Type for Lua function */ typedef int luaA_function; +#define DO_LUA_GC(type, prefix, lua_type, type_unref) \ + static int \ + luaA_##prefix##_gc(lua_State *L) \ + { \ + type **p = luaA_checkudata(L, 1, lua_type); \ + type_unref(p); \ + *p = NULL; \ + return 0; \ + } + #define luaA_dostring(L, cmd) \ do { \ if(cmd) \ diff --git a/mouse.c b/mouse.c index 546d64ff1..6afd1e5c6 100644 --- a/mouse.c +++ b/mouse.c @@ -1054,16 +1054,7 @@ luaA_mouse_new(lua_State *L) return luaA_mouse_userdata_new(button); } -/** Handle mouse garbage collection. - */ -static int -luaA_mouse_gc(lua_State *L) -{ - button_t **b = luaA_checkudata(L, 1, "mouse"); - button_unref(b); - *b = NULL; - return 0; -} +DO_LUA_GC(button_t, mouse, "mouse", button_unref) const struct luaL_reg awesome_mouse_methods[] = { diff --git a/statusbar.c b/statusbar.c index c81df3cb9..2fae5f233 100644 --- a/statusbar.c +++ b/statusbar.c @@ -570,16 +570,7 @@ luaA_statusbar_widget_get(lua_State *L) return 1; } -/** Handle statusbar garbage collection. - */ -static int -luaA_statusbar_gc(lua_State *L) -{ - statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); - statusbar_unref(sb); - *sb = NULL; - return 0; -} +DO_LUA_GC(statusbar_t, statusbar, "statusbar", statusbar_unref) const struct luaL_reg awesome_statusbar_methods[] = { diff --git a/tag.c b/tag.c index b5c7c1685..1ac15a626 100644 --- a/tag.c +++ b/tag.c @@ -535,17 +535,6 @@ luaA_tag_name_set(lua_State *L) return 0; } -/** Handle tag garbage collection. - */ -static int -luaA_tag_gc(lua_State *L) -{ - tag_t **tag = luaA_checkudata(L, 1, "tag"); - tag_unref(tag); - *tag = NULL; - return 0; -} - /** Get the layout of the tag. * \param L The Lua VM state. * @@ -601,6 +590,8 @@ luaA_tag_userdata_new(tag_t *t) return luaA_settype(globalconf.L, "tag"); } +DO_LUA_GC(tag_t, tag, "tag", tag_unref) + const struct luaL_reg awesome_tag_methods[] = { { "new", luaA_tag_new }, diff --git a/titlebar.c b/titlebar.c index c5e23db1c..032caea24 100644 --- a/titlebar.c +++ b/titlebar.c @@ -564,22 +564,6 @@ luaA_titlebar_userdata_new(titlebar_t *t) return luaA_settype(globalconf.L, "titlebar"); } -/** Handle titlebar garbage collection. - * \param L The Lua VM state. - * \return The number of value pushed. - * - * \luastack - * \lvalue A titlebar. - */ -static int -luaA_titlebar_gc(lua_State *L) -{ - titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar"); - titlebar_unref(titlebar); - *titlebar = NULL; - return 0; -} - /** Convert a titlebar to a printable string. * \param L The Lua VM state. * \return The number of value pushed. @@ -614,6 +598,8 @@ luaA_titlebar_eq(lua_State *L) return 1; } +DO_LUA_GC(titlebar_t, titlebar, "titlebar", titlebar_unref) + const struct luaL_reg awesome_titlebar_methods[] = { { "new", luaA_titlebar_new }, diff --git a/widget.c b/widget.c index dd7895a46..4480b762c 100644 --- a/widget.c +++ b/widget.c @@ -442,21 +442,6 @@ luaA_widget_set(lua_State *L) return 0; } -/** Handle widget garbage collection. - * \param L The Lua VM state. - * - * \luastack - * \lvalue A widget. - */ -static int -luaA_widget_gc(lua_State *L) -{ - widget_t **widget = luaA_checkudata(L, 1, "widget"); - widget_unref(widget); - *widget = NULL; - return 0; -} - /** Convert a widget into a printable string. * \param L The Lua VM state. * @@ -488,6 +473,8 @@ luaA_widget_eq(lua_State *L) return 1; } +DO_LUA_GC(widget_t, widget, "widget", widget_unref) + /** Set the widget name. * \param L The Lua VM state. *