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 "common/luaclass.h"
27 #define LUAA_OBJECT_REGISTRY_KEY "awesome.object.registry"
29 int luaA_settype(lua_State
*, lua_class_t
*);
30 void luaA_object_setup(lua_State
*);
31 void * luaA_object_incref(lua_State
*, int, int);
32 void luaA_object_decref(lua_State
*, int, void *);
34 /** Store an item in the environment table of an object.
35 * \param L The Lua VM state.
36 * \param ud The index of the object on the stack.
37 * \param iud The index of the item on the stack.
38 * \return The item reference.
41 luaA_object_ref_item(lua_State
*L
, int ud
, int iud
)
43 /* Get the env table from the object */
45 void *pointer
= luaA_object_incref(L
, -1, iud
< 0 ? iud
- 1 : iud
);
46 /* Remove env table */
51 /** Unref an item from the environment table of an object.
52 * \param L The Lua VM state.
53 * \param ud The index of the object on the stack.
57 luaA_object_unref_item(lua_State
*L
, int ud
, void *pointer
)
59 /* Get the env table from the object */
62 luaA_object_decref(L
, -1, pointer
);
63 /* Remove env table */
67 /** Push an object item on the stack.
68 * \param L The Lua VM state.
69 * \param ud The object index on the stack.
70 * \param pointer The item pointer.
71 * \return The number of element pushed on stack.
74 luaA_object_push_item(lua_State
*L
, int ud
, void *pointer
)
76 /* Get env table of the object */
79 lua_pushlightuserdata(L
, pointer
);
82 /* Remove env table */
88 luaA_object_registry_push(lua_State
*L
)
90 lua_pushliteral(L
, LUAA_OBJECT_REGISTRY_KEY
);
91 lua_rawget(L
, LUA_REGISTRYINDEX
);
94 /** Reference an object and return a pointer to it.
95 * That only works with userdata, table, thread or function.
96 * \param L The Lua VM state.
97 * \param oud The object index on the stack.
98 * \return The object reference, or NULL if not referencable.
101 luaA_object_ref(lua_State
*L
, int oud
)
103 luaA_object_registry_push(L
);
104 void *p
= luaA_object_incref(L
, -1, oud
< 0 ? oud
- 1 : oud
);
109 /** Unreference an object and return a pointer to it.
110 * That only works with userdata, table, thread or function.
111 * \param L The Lua VM state.
112 * \param oud The object index on the stack.
115 luaA_object_unref(lua_State
*L
, void *pointer
)
117 luaA_object_registry_push(L
);
118 luaA_object_decref(L
, -1, pointer
);
122 /** Push a referenced object onto the stack.
123 * \param L The Lua VM state.
124 * \param pointer The object to push.
125 * \return The number of element pushed on stack.
128 luaA_object_push(lua_State
*L
, void *pointer
)
130 luaA_object_registry_push(L
);
131 lua_pushlightuserdata(L
, pointer
);
137 void signal_object_emit(lua_State
*, signal_array_t
*, const char *, int);
139 void luaA_object_add_signal(lua_State
*, int, const char *, int);
140 void luaA_object_remove_signal(lua_State
*, int, const char *, int);
141 void luaA_object_emit_signal(lua_State
*, int, const char *, int);
143 int luaA_object_add_signal_simple(lua_State
*);
144 int luaA_object_remove_signal_simple(lua_State
*);
145 int luaA_object_emit_signal_simple(lua_State
*);
147 #define LUA_OBJECT_FUNCS(lua_class, type, prefix) \
148 LUA_CLASS_FUNCS(prefix, lua_class) \
149 static inline type * \
150 prefix##_new(lua_State *L) \
152 type *p = lua_newuserdata(L, sizeof(type)); \
154 luaA_settype(L, &(lua_class)); \
157 lua_setmetatable(L, -2); \
158 lua_setfenv(L, -2); \
159 lua_pushvalue(L, -1); \
160 luaA_class_emit_signal(L, &(lua_class), "new", 1); \
164 #define OBJECT_EXPORT_PROPERTY(pfx, type, field) \
165 fieldtypeof(type, field) \
166 pfx##_get_##field(type *object) \
168 return object->field; \
171 #define LUA_OBJECT_EXPORT_PROPERTY(pfx, type, field, pusher) \
173 luaA_##pfx##_get_##field(lua_State *L, type *object) \
175 pusher(L, object->field); \
179 int luaA_object_tostring(lua_State
*);
180 int luaA_object_gc(lua_State
*);
182 #define LUA_OBJECT_META(prefix) \
183 { "__tostring", luaA_object_tostring }, \
184 { "add_signal", luaA_object_add_signal_simple }, \
185 { "remove_signal", luaA_object_remove_signal_simple }, \
186 { "emit_signal", luaA_object_emit_signal_simple },
190 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80