change codename
[awesome.git] / common / luaclass.c
blob85f115aa252c33de4f5ba6900f94234fd663dd5e
1 /*
2 * luaclass.c - useful functions for handling Lua classes
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 #include "common/luaclass.h"
23 #include "common/luaobject.h"
25 struct lua_class_property
27 /** ID matching the property */
28 awesome_token_t id;
29 /** Callback function called when the property is found in object creation. */
30 lua_class_propfunc_t new;
31 /** Callback function called when the property is found in object __index. */
32 lua_class_propfunc_t index;
33 /** Callback function called when the property is found in object __newindex. */
34 lua_class_propfunc_t newindex;
37 DO_ARRAY(lua_class_t *, lua_class, DO_NOTHING)
39 static lua_class_array_t luaA_classes;
41 /** Convert a object to a udata if possible.
42 * \param L The Lua VM state.
43 * \param ud The index.
44 * \param class The wanted class.
45 * \return A pointer to the object, NULL otherwise.
47 void *
48 luaA_toudata(lua_State *L, int ud, lua_class_t *class)
50 void *p = lua_touserdata(L, ud);
51 if(p) /* value is a userdata? */
52 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
54 lua_pushlightuserdata(L, class);
55 lua_rawget(L, LUA_REGISTRYINDEX);
56 if(!lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
57 p = NULL;
58 lua_pop(L, 2); /* remove both metatables */
60 return p;
63 /** Check for a udata class.
64 * \param L The Lua VM state.
65 * \param ud The object index on the stack.
66 * \param class The wanted class.
68 void *
69 luaA_checkudata(lua_State *L, int ud, lua_class_t *class)
71 void *p = luaA_toudata(L, ud, class);
72 if(!p)
73 luaL_typerror(L, ud, class->name);
74 return p;
77 /** Get an object lua_class.
78 * \param L The Lua VM state.
79 * \param idx The index of the object on the stack.
81 lua_class_t *
82 luaA_class_get(lua_State *L, int idx)
84 int type = lua_type(L, idx);
86 if(type == LUA_TUSERDATA)
87 foreach(class, luaA_classes)
88 if(luaA_toudata(L, idx, *class))
89 return *class;
91 return NULL;
94 /** Enhanced version of lua_typename that recognizes setup Lua classes.
95 * \param L The Lua VM state.
96 * \param idx The index of the object on the stack.
98 const char *
99 luaA_typename(lua_State *L, int idx)
101 int type = lua_type(L, idx);
103 if(type == LUA_TUSERDATA)
105 lua_class_t *lua_class = luaA_class_get(L, idx);
106 if(lua_class)
107 return lua_class->name;
110 return lua_typename(L, type);
113 void
114 luaA_openlib(lua_State *L, const char *name,
115 const struct luaL_reg methods[],
116 const struct luaL_reg meta[])
118 luaL_newmetatable(L, name); /* 1 */
119 lua_pushvalue(L, -1); /* dup metatable 2 */
120 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
122 luaL_register(L, NULL, meta); /* 1 */
123 luaL_register(L, name, methods); /* 2 */
124 lua_pushvalue(L, -1); /* dup self as metatable 3 */
125 lua_setmetatable(L, -2); /* set self as metatable 2 */
126 lua_pop(L, 2);
129 static int
130 lua_class_property_cmp(const void *a, const void *b)
132 const lua_class_property_t *x = a, *y = b;
133 return x->id > y->id ? 1 : (x->id < y->id ? -1 : 0);
136 BARRAY_FUNCS(lua_class_property_t, lua_class_property, DO_NOTHING, lua_class_property_cmp)
138 void
139 luaA_class_add_property(lua_class_t *lua_class,
140 awesome_token_t token,
141 lua_class_propfunc_t cb_new,
142 lua_class_propfunc_t cb_index,
143 lua_class_propfunc_t cb_newindex)
145 lua_class_property_array_insert(&lua_class->properties, (lua_class_property_t)
147 .id = token,
148 .new = cb_new,
149 .index = cb_index,
150 .newindex = cb_newindex
154 void
155 luaA_class_setup(lua_State *L, lua_class_t *class,
156 const char *name,
157 lua_class_allocator_t allocator,
158 lua_class_propfunc_t index_miss_property,
159 lua_class_propfunc_t newindex_miss_property,
160 const struct luaL_reg methods[],
161 const struct luaL_reg meta[])
163 /* Create the metatable */
164 lua_newtable(L);
165 /* Register it with class pointer as key in the registry */
166 lua_pushlightuserdata(L, class);
167 /* Duplicate the metatable */
168 lua_pushvalue(L, -2);
169 lua_rawset(L, LUA_REGISTRYINDEX);
171 lua_pushvalue(L, -1); /* dup metatable 2 */
172 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
174 luaL_register(L, NULL, meta); /* 1 */
175 luaL_register(L, name, methods); /* 2 */
176 lua_pushvalue(L, -1); /* dup self as metatable 3 */
177 lua_setmetatable(L, -2); /* set self as metatable 2 */
178 lua_pop(L, 2);
180 class->allocator = allocator;
181 class->name = name;
182 class->index_miss_property = index_miss_property;
183 class->newindex_miss_property = newindex_miss_property;
185 lua_class_array_append(&luaA_classes, class);
188 void
189 luaA_class_add_signal(lua_State *L, lua_class_t *lua_class,
190 const char *name, int ud)
192 luaA_checkfunction(L, ud);
193 signal_add(&lua_class->signals, name, luaA_object_ref(L, ud));
196 void
197 luaA_class_remove_signal(lua_State *L, lua_class_t *lua_class,
198 const char *name, int ud)
200 luaA_checkfunction(L, ud);
201 void *ref = (void *) lua_topointer(L, ud);
202 signal_remove(&lua_class->signals, name, ref);
203 luaA_object_unref(L, (void *) ref);
204 lua_remove(L, ud);
207 void
208 luaA_class_emit_signal(lua_State *L, lua_class_t *lua_class,
209 const char *name, int nargs)
211 signal_object_emit(L, &lua_class->signals, name, nargs);
214 /** Try to use the metatable of an object.
215 * \param L The Lua VM state.
216 * \param idxobj The index of the object.
217 * \param idxfield The index of the field (attribute) to get.
218 * \return The number of element pushed on stack.
221 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
223 /* Get metatable of the object. */
224 lua_getmetatable(L, idxobj);
225 /* Get the field */
226 lua_pushvalue(L, idxfield);
227 lua_rawget(L, -2);
228 /* Do we have a field like that? */
229 if(!lua_isnil(L, -1))
231 /* Yes, so return it! */
232 lua_remove(L, -2);
233 return 1;
235 /* No, so remove everything. */
236 lua_pop(L, 2);
238 return 0;
241 static lua_class_property_t *
242 lua_class_property_array_getbyid(lua_class_property_array_t *arr,
243 awesome_token_t id)
245 lua_class_property_t lookup_prop = { .id = id };
246 return lua_class_property_array_lookup(arr, &lookup_prop);
249 /** Get a property of a object.
250 * \param L The Lua VM state.
251 * \param lua_class The Lua class.
252 * \param fieldidx The index of the field name.
253 * \return The object property if found, NULL otherwise.
255 static lua_class_property_t *
256 luaA_class_property_get(lua_State *L, lua_class_t *lua_class, int fieldidx)
258 /* Lookup the property using token */
259 size_t len;
260 const char *attr = luaL_checklstring(L, fieldidx, &len);
261 awesome_token_t token = a_tokenize(attr, len);
263 return lua_class_property_array_getbyid(&lua_class->properties, token);
266 /** Generic index meta function for objects.
267 * \param L The Lua VM state.
268 * \return The number of elements pushed on stack.
271 luaA_class_index(lua_State *L)
273 /* Try to use metatable first. */
274 if(luaA_usemetatable(L, 1, 2))
275 return 1;
277 lua_class_t *class = luaA_class_get(L, 1);
279 lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
281 /* Property does exist and has an index callback */
282 if(prop)
284 if(prop->index)
285 return prop->index(L, luaA_checkudata(L, 1, class));
287 else
289 if(class->index_miss_property)
290 return class->index_miss_property(L, luaA_checkudata(L, 1, class));
293 return 0;
296 /** Generic newindex meta function for objects.
297 * \param L The Lua VM state.
298 * \return The number of elements pushed on stack.
301 luaA_class_newindex(lua_State *L)
303 /* Try to use metatable first. */
304 if(luaA_usemetatable(L, 1, 2))
305 return 1;
307 lua_class_t *class = luaA_class_get(L, 1);
309 lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
311 /* Property does exist and has a newindex callback */
312 if(prop)
314 if(prop->newindex)
315 return prop->newindex(L, luaA_checkudata(L, 1, class));
317 else
319 if(class->newindex_miss_property)
320 return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
323 return 0;
326 /** Generic constructor function for objects.
327 * \param L The Lua VM state.
328 * \return The number of elements pushed on stack.
331 luaA_class_new(lua_State *L, lua_class_t *lua_class)
333 /* Check we have a table that should contains some properties */
334 luaA_checktable(L, 2);
336 /* Create a new object */
337 void *object = lua_class->allocator(L);
339 /* Push the first key before iterating */
340 lua_pushnil(L);
341 /* Iterate over the property keys */
342 while(lua_next(L, 2))
344 /* Check that the key is a string.
345 * We cannot call tostring blindly or Lua will convert a key that is a
346 * number TO A STRING, confusing lua_next() */
347 if(lua_isstring(L, -2))
349 /* Lookup the property using token */
350 size_t len;
351 const char *attr = lua_tolstring(L, -2, &len);
352 lua_class_property_t *prop =
353 lua_class_property_array_getbyid(&lua_class->properties,
354 a_tokenize(attr, len));
356 if(prop && prop->new)
357 prop->new(L, object);
359 /* Remove value */
360 lua_pop(L, 1);
363 return 1;
366 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80