awful.remote: enhance description
[awesome.git] / common / luaclass.h
blobf41ec9a19d5b14eafda3acc56328b1d1b6a0510c
1 /*
2 * luaclass.h - 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 #ifndef AWESOME_COMMON_LUACLASS
23 #define AWESOME_COMMON_LUACLASS
25 #include "common/signal.h"
26 #include "common/tokenize.h"
28 typedef struct lua_class_property lua_class_property_t;
30 ARRAY_TYPE(lua_class_property_t, lua_class_property)
32 #define LUA_OBJECT_HEADER \
33 signal_array_t signals;
35 /** Generic type for all objects.
36 * All Lua objects can be casted to this type.
38 typedef struct
40 LUA_OBJECT_HEADER
41 } lua_object_t;
43 typedef lua_object_t *(*lua_class_allocator_t)(lua_State *);
45 typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
47 typedef struct
49 /** Class name */
50 const char *name;
51 /** Class signals */
52 signal_array_t signals;
53 /** Allocator for creating new objects of that class */
54 lua_class_allocator_t allocator;
55 /** Class properties */
56 lua_class_property_array_t properties;
57 /** Function to call when a indexing an unknown property */
58 lua_class_propfunc_t index_miss_property;
59 /** Function to call when a indexing an unknown property */
60 lua_class_propfunc_t newindex_miss_property;
61 } lua_class_t;
63 const char * luaA_typename(lua_State *, int);
64 lua_class_t * luaA_class_get(lua_State *, int);
66 void luaA_class_add_signal(lua_State *, lua_class_t *, const char *, int);
67 void luaA_class_remove_signal(lua_State *, lua_class_t *, const char *, int);
68 void luaA_class_emit_signal(lua_State *, lua_class_t *, const char *, int);
70 void luaA_openlib(lua_State *, const char *, const struct luaL_reg[], const struct luaL_reg[]);
71 void luaA_class_setup(lua_State *, lua_class_t *, const char *, lua_class_allocator_t,
72 lua_class_propfunc_t, lua_class_propfunc_t,
73 const struct luaL_reg[], const struct luaL_reg[]);
75 void luaA_class_add_property(lua_class_t *, awesome_token_t,
76 lua_class_propfunc_t, lua_class_propfunc_t, lua_class_propfunc_t);
78 int luaA_usemetatable(lua_State *, int, int);
79 int luaA_class_index(lua_State *);
80 int luaA_class_newindex(lua_State *);
81 int luaA_class_new(lua_State *, lua_class_t *);
83 void * luaA_checkudata(lua_State *, int, lua_class_t *);
84 void * luaA_toudata(lua_State *L, int ud, lua_class_t *);
86 #define LUA_CLASS_FUNCS(prefix, lua_class) \
87 static inline int \
88 luaA_##prefix##_class_add_signal(lua_State *L) \
89 { \
90 luaA_class_add_signal(L, &(lua_class), luaL_checkstring(L, 1), 2); \
91 return 0; \
92 } \
94 static inline int \
95 luaA_##prefix##_class_remove_signal(lua_State *L) \
96 { \
97 luaA_class_remove_signal(L, &(lua_class), \
98 luaL_checkstring(L, 1), 2); \
99 return 0; \
102 static inline int \
103 luaA_##prefix##_class_emit_signal(lua_State *L) \
105 luaA_class_emit_signal(L, &(lua_class), luaL_checkstring(L, 1), \
106 lua_gettop(L) - 1); \
107 return 0; \
110 #define LUA_CLASS_METHODS(class) \
111 { "add_signal", luaA_##class##_class_add_signal }, \
112 { "remove_signal", luaA_##class##_class_remove_signal }, \
113 { "emit_signal", luaA_##class##_class_emit_signal },
115 #define LUA_CLASS_META \
116 { "__index", luaA_class_index }, \
117 { "__newindex", luaA_class_newindex },
119 #endif
121 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80