2 * luaclass.h - useful functions for handling Lua classes
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2009 Julien Danjou <julien@danjou.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef LUAKIT_LUACLASS_H
23 #define LUAKIT_LUACLASS_H
25 #include <glib/gtree.h>
28 typedef struct lua_class_property lua_class_property_t
;
29 typedef GPtrArray lua_class_property_array_t
;
31 static GPtrArray luaH_classes
;
33 #define LUA_OBJECT_HEADER \
36 /* Generic type for all objects. All Lua objects can be casted
42 typedef lua_object_t
*(*lua_class_allocator_t
)(lua_State
*);
44 typedef gint (*lua_class_propfunc_t
)(lua_State
*, lua_object_t
*);
51 /** Allocator for creating new objects of that class */
52 lua_class_allocator_t allocator
;
53 /** Class properties */
54 lua_class_property_array_t
*properties
;
55 /** Function to call when a indexing an unknown property */
56 lua_class_propfunc_t index_miss_property
;
57 /** Function to call when a indexing an unknown property */
58 lua_class_propfunc_t newindex_miss_property
;
61 const gchar
*luaH_typename(lua_State
*, gint
);
62 lua_class_t
*luaH_class_get(lua_State
*, gint
);
64 void luaH_class_add_signal(lua_State
*, lua_class_t
*, const gchar
*, gint
);
65 void luaH_class_remove_signal(lua_State
*, lua_class_t
*, const gchar
*, gint
);
66 void luaH_class_emit_signal(lua_State
*, lua_class_t
*, const gchar
*, gint
);
68 void luaH_openlib(lua_State
*, const gchar
*, const struct luaL_reg
[], const struct luaL_reg
[]);
69 void luaH_class_setup(lua_State
*, lua_class_t
*, const gchar
*, lua_class_allocator_t
,
70 lua_class_propfunc_t
, lua_class_propfunc_t
,
71 const struct luaL_reg
[], const struct luaL_reg
[]);
73 void luaH_class_add_property(lua_class_t
*, const gchar
*,
74 lua_class_propfunc_t
, lua_class_propfunc_t
, lua_class_propfunc_t
);
76 gint
luaH_usemetatable(lua_State
*, gint
, gint
);
77 gint
luaH_class_index(lua_State
*);
78 gint
luaH_class_newindex(lua_State
*);
79 gint
luaH_class_new(lua_State
*, lua_class_t
*);
81 gpointer
luaH_checkudata(lua_State
*, gint
, lua_class_t
*);
82 gpointer
luaH_toudata(lua_State
*L
, gint ud
, lua_class_t
*);
84 static inline gpointer
85 luaH_checkudataornil(lua_State
*L
, gint udx
, lua_class_t
*class) {
88 return luaH_checkudata(L
, udx
, class);
91 #define LUA_CLASS_FUNCS(prefix, lua_class) \
93 luaH_##prefix##_class_add_signal(lua_State *L) { \
94 luaH_class_add_signal(L, &(lua_class), luaL_checkstring(L, 1), 2); \
99 luaH_##prefix##_class_remove_signal(lua_State *L) { \
100 luaH_class_remove_signal(L, &(lua_class), \
101 luaL_checkstring(L, 1), 2); \
106 luaH_##prefix##_class_emit_signal(lua_State *L) { \
107 luaH_class_emit_signal(L, &(lua_class), luaL_checkstring(L, 1), \
108 lua_gettop(L) - 1); \
112 #define LUA_CLASS_METHODS(class) \
113 { "add_signal", luaH_##class##_class_add_signal }, \
114 { "remove_signal", luaH_##class##_class_remove_signal }, \
115 { "emit_signal", luaH_##class##_class_emit_signal },
117 #define LUA_CLASS_META \
118 { "__index", luaH_class_index }, \
119 { "__newindex", luaH_class_newindex },
123 // vim: ft=c:et:sw=4:ts=8:sts=4:enc=utf-8:tw=80