comparison src/auxiliar.c @ 0:4b915342e2a8

LuaSocket 2.0.2 + CMake build description.
author Eric Wing <ewing . public |-at-| gmail . com>
date Tue, 26 Aug 2008 18:40:01 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4b915342e2a8
1 /*=========================================================================*\
2 * Auxiliar routines for class hierarchy manipulation
3 * LuaSocket toolkit
4 *
5 * RCS ID: $Id: auxiliar.c,v 1.14 2005/10/07 04:40:59 diego Exp $
6 \*=========================================================================*/
7 #include <string.h>
8 #include <stdio.h>
9
10 #include "auxiliar.h"
11
12 /*=========================================================================*\
13 * Exported functions
14 \*=========================================================================*/
15 /*-------------------------------------------------------------------------*\
16 * Initializes the module
17 \*-------------------------------------------------------------------------*/
18 int auxiliar_open(lua_State *L) {
19 (void) L;
20 return 0;
21 }
22
23 /*-------------------------------------------------------------------------*\
24 * Creates a new class with given methods
25 * Methods whose names start with __ are passed directly to the metatable.
26 \*-------------------------------------------------------------------------*/
27 void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func) {
28 luaL_newmetatable(L, classname); /* mt */
29 /* create __index table to place methods */
30 lua_pushstring(L, "__index"); /* mt,"__index" */
31 lua_newtable(L); /* mt,"__index",it */
32 /* put class name into class metatable */
33 lua_pushstring(L, "class"); /* mt,"__index",it,"class" */
34 lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */
35 lua_rawset(L, -3); /* mt,"__index",it */
36 /* pass all methods that start with _ to the metatable, and all others
37 * to the index table */
38 for (; func->name; func++) { /* mt,"__index",it */
39 lua_pushstring(L, func->name);
40 lua_pushcfunction(L, func->func);
41 lua_rawset(L, func->name[0] == '_' ? -5: -3);
42 }
43 lua_rawset(L, -3); /* mt */
44 lua_pop(L, 1);
45 }
46
47 /*-------------------------------------------------------------------------*\
48 * Prints the value of a class in a nice way
49 \*-------------------------------------------------------------------------*/
50 int auxiliar_tostring(lua_State *L) {
51 char buf[32];
52 if (!lua_getmetatable(L, 1)) goto error;
53 lua_pushstring(L, "__index");
54 lua_gettable(L, -2);
55 if (!lua_istable(L, -1)) goto error;
56 lua_pushstring(L, "class");
57 lua_gettable(L, -2);
58 if (!lua_isstring(L, -1)) goto error;
59 sprintf(buf, "%p", lua_touserdata(L, 1));
60 lua_pushfstring(L, "%s: %s", lua_tostring(L, -1), buf);
61 return 1;
62 error:
63 lua_pushstring(L, "invalid object passed to 'auxiliar.c:__tostring'");
64 lua_error(L);
65 return 1;
66 }
67
68 /*-------------------------------------------------------------------------*\
69 * Insert class into group
70 \*-------------------------------------------------------------------------*/
71 void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
72 luaL_getmetatable(L, classname);
73 lua_pushstring(L, groupname);
74 lua_pushboolean(L, 1);
75 lua_rawset(L, -3);
76 lua_pop(L, 1);
77 }
78
79 /*-------------------------------------------------------------------------*\
80 * Make sure argument is a boolean
81 \*-------------------------------------------------------------------------*/
82 int auxiliar_checkboolean(lua_State *L, int objidx) {
83 if (!lua_isboolean(L, objidx))
84 luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
85 return lua_toboolean(L, objidx);
86 }
87
88 /*-------------------------------------------------------------------------*\
89 * Return userdata pointer if object belongs to a given class, abort with
90 * error otherwise
91 \*-------------------------------------------------------------------------*/
92 void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
93 void *data = auxiliar_getclassudata(L, classname, objidx);
94 if (!data) {
95 char msg[45];
96 sprintf(msg, "%.35s expected", classname);
97 luaL_argerror(L, objidx, msg);
98 }
99 return data;
100 }
101
102 /*-------------------------------------------------------------------------*\
103 * Return userdata pointer if object belongs to a given group, abort with
104 * error otherwise
105 \*-------------------------------------------------------------------------*/
106 void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
107 void *data = auxiliar_getgroupudata(L, groupname, objidx);
108 if (!data) {
109 char msg[45];
110 sprintf(msg, "%.35s expected", groupname);
111 luaL_argerror(L, objidx, msg);
112 }
113 return data;
114 }
115
116 /*-------------------------------------------------------------------------*\
117 * Set object class
118 \*-------------------------------------------------------------------------*/
119 void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
120 luaL_getmetatable(L, classname);
121 if (objidx < 0) objidx--;
122 lua_setmetatable(L, objidx);
123 }
124
125 /*-------------------------------------------------------------------------*\
126 * Get a userdata pointer if object belongs to a given group. Return NULL
127 * otherwise
128 \*-------------------------------------------------------------------------*/
129 void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
130 if (!lua_getmetatable(L, objidx))
131 return NULL;
132 lua_pushstring(L, groupname);
133 lua_rawget(L, -2);
134 if (lua_isnil(L, -1)) {
135 lua_pop(L, 2);
136 return NULL;
137 } else {
138 lua_pop(L, 2);
139 return lua_touserdata(L, objidx);
140 }
141 }
142
143 /*-------------------------------------------------------------------------*\
144 * Get a userdata pointer if object belongs to a given class. Return NULL
145 * otherwise
146 \*-------------------------------------------------------------------------*/
147 void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
148 return luaL_checkudata(L, objidx, classname);
149 }