diff options
Diffstat (limited to 'src/auxiliar.c')
-rw-r--r-- | src/auxiliar.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c new file mode 100644 index 0000000..5e5ba1a --- /dev/null +++ b/src/auxiliar.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /*=========================================================================*\ | ||
2 | * Auxiliar routines for class hierarchy manipulation | ||
3 | * | ||
4 | * RCS ID: $Id$ | ||
5 | \*=========================================================================*/ | ||
6 | #include "aux.h" | ||
7 | |||
8 | /*=========================================================================*\ | ||
9 | * Internal function prototypes | ||
10 | \*=========================================================================*/ | ||
11 | static void *aux_getgroupudata(lua_State *L, const char *group, int objidx); | ||
12 | |||
13 | /*=========================================================================*\ | ||
14 | * Exported functions | ||
15 | \*=========================================================================*/ | ||
16 | /*-------------------------------------------------------------------------*\ | ||
17 | * Creates a new class. A class has methods given by the func array and the | ||
18 | * field 'class' tells the object class. The table 'group' list the class | ||
19 | * groups the object belongs to. | ||
20 | \*-------------------------------------------------------------------------*/ | ||
21 | void aux_newclass(lua_State *L, const char *name, luaL_reg *func) | ||
22 | { | ||
23 | luaL_newmetatable(L, name); | ||
24 | lua_pushstring(L, "__index"); | ||
25 | lua_newtable(L); | ||
26 | luaL_openlib(L, NULL, func, 0); | ||
27 | lua_pushstring(L, "class"); | ||
28 | lua_pushstring(L, name); | ||
29 | lua_settable(L, -3); | ||
30 | lua_settable(L, -3); | ||
31 | lua_pushstring(L, "group"); | ||
32 | lua_newtable(L); | ||
33 | lua_settable(L, -3); | ||
34 | lua_pop(L, 1); | ||
35 | } | ||
36 | |||
37 | /*-------------------------------------------------------------------------*\ | ||
38 | * Add group to object list of groups. | ||
39 | \*-------------------------------------------------------------------------*/ | ||
40 | void aux_add2group(lua_State *L, const char *name, const char *group) | ||
41 | { | ||
42 | luaL_getmetatable(L, name); | ||
43 | lua_pushstring(L, "group"); | ||
44 | lua_gettable(L, -2); | ||
45 | lua_pushstring(L, group); | ||
46 | lua_pushnumber(L, 1); | ||
47 | lua_settable(L, -3); | ||
48 | lua_pop(L, 2); | ||
49 | } | ||
50 | |||
51 | /*-------------------------------------------------------------------------*\ | ||
52 | * Get a userdata making sure the object belongs to a given class. | ||
53 | \*-------------------------------------------------------------------------*/ | ||
54 | void *aux_checkclass(lua_State *L, const char *name, int objidx) | ||
55 | { | ||
56 | void *data = luaL_checkudata(L, objidx, name); | ||
57 | if (!data) { | ||
58 | char msg[45]; | ||
59 | sprintf(msg, "%.35s expected", name); | ||
60 | luaL_argerror(L, objidx, msg); | ||
61 | } | ||
62 | return data; | ||
63 | } | ||
64 | |||
65 | /*-------------------------------------------------------------------------*\ | ||
66 | * Get a userdata making sure the object belongs to a given group. | ||
67 | \*-------------------------------------------------------------------------*/ | ||
68 | void *aux_checkgroup(lua_State *L, const char *group, int objidx) | ||
69 | { | ||
70 | void *data = aux_getgroupudata(L, group, objidx); | ||
71 | if (!data) { | ||
72 | char msg[45]; | ||
73 | sprintf(msg, "%.35s expected", group); | ||
74 | luaL_argerror(L, objidx, msg); | ||
75 | } | ||
76 | return data; | ||
77 | } | ||
78 | |||
79 | /*-------------------------------------------------------------------------*\ | ||
80 | * Set object class. | ||
81 | \*-------------------------------------------------------------------------*/ | ||
82 | void aux_setclass(lua_State *L, const char *name, int objidx) | ||
83 | { | ||
84 | luaL_getmetatable(L, name); | ||
85 | if (objidx < 0) objidx--; | ||
86 | lua_setmetatable(L, objidx); | ||
87 | } | ||
88 | |||
89 | /*=========================================================================*\ | ||
90 | * Internal functions | ||
91 | \*=========================================================================*/ | ||
92 | /*-------------------------------------------------------------------------*\ | ||
93 | * Get a userdata if object belongs to a given group. | ||
94 | \*-------------------------------------------------------------------------*/ | ||
95 | static void *aux_getgroupudata(lua_State *L, const char *group, int objidx) | ||
96 | { | ||
97 | if (!lua_getmetatable(L, objidx)) return NULL; | ||
98 | lua_pushstring(L, "group"); | ||
99 | lua_gettable(L, -2); | ||
100 | if (lua_isnil(L, -1)) { | ||
101 | lua_pop(L, 2); | ||
102 | return NULL; | ||
103 | } | ||
104 | lua_pushstring(L, group); | ||
105 | lua_gettable(L, -2); | ||
106 | if (lua_isnil(L, -1)) { | ||
107 | lua_pop(L, 3); | ||
108 | return NULL; | ||
109 | } | ||
110 | lua_pop(L, 3); | ||
111 | return lua_touserdata(L, objidx); | ||
112 | } | ||
113 | |||