swindow: fix draw_context_update for north and south orientation
[awesome.git] / common / luaobject.h
blob7d20d693f74c007c9b9261db3bf198e9e2cd8c61
1 /*
2 * luaobject.h - useful functions for handling Lua objects
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef AWESOME_COMMON_LUAOBJECT
23 #define AWESOME_COMMON_LUAOBJECT
25 #include <lauxlib.h>
26 #include "common/array.h"
28 /** Type for Lua references */
29 typedef int luaA_ref;
31 static inline int
32 luaA_settype(lua_State *L, const char *type)
34 luaL_getmetatable(L, type);
35 lua_setmetatable(L, -2);
36 return 1;
39 DO_ARRAY(luaA_ref, luaA_ref, DO_NOTHING)
41 #define LUA_OBJECT_FUNCS(type, prefix, lua_type) \
42 static inline type * \
43 prefix##_new(lua_State *L) \
44 { \
45 type *p = lua_newuserdata(L, sizeof(type)); \
46 p_clear(p, 1); \
47 luaA_settype(L, lua_type); \
48 return p; \
49 } \
51 static inline int \
52 prefix##_push(lua_State *L, type *item) \
53 { \
54 if(item) \
55 { \
56 assert(item->refs.len); \
57 lua_rawgeti(L, LUA_REGISTRYINDEX, item->refs.tab[0]); \
58 } \
59 else \
60 lua_pushnil(L); \
61 return 1; \
62 } \
64 static inline type * \
65 prefix##_ref(lua_State *L) \
66 { \
67 if(lua_isnil(L, -1)) \
68 return NULL; \
69 type *item = luaL_checkudata(L, -1, lua_type); \
70 luaA_ref_array_append(&item->refs, luaL_ref(L, LUA_REGISTRYINDEX)); \
71 return item; \
72 } \
74 static inline void \
75 prefix##_unref(lua_State *L, type *item) \
76 { \
77 if(item) \
78 { \
79 assert(item->refs.len); \
80 luaL_unref(L, LUA_REGISTRYINDEX, item->refs.tab[0]); \
81 luaA_ref_array_take(&item->refs, 0); \
82 } \
85 #endif
87 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80