Debian Codegraph @

engine/tolua/tolua_to.cpp

raw
1 /* tolua: funcitons to convert to C types
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
4 ** TeCGraf/PUC-Rio
5 ** Apr 2003
6 */
7
8 /* This code is free software; you can redistribute it and/or modify it.
9 ** The software provided hereunder is on an "as is" basis, and
10 ** the author has no obligation to provide maintenance, support, updates,
11 ** enhancements, or modifications.
12 */
13
14 #include "tolua++.h"
15
16 #include <string.h>
17 #include <stdlib.h>
18
19 TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def)
20 {
21 return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
22 }
23
24 TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def)
25 {
26 return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
27 }
28
29 TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def)
30 {
31
32 /* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */
33
34 if (lua_gettop(L)<abs(narg)) {
35 return def;
36 };
37
38 if (lua_islightuserdata(L, narg)) {
39
40 return lua_touserdata(L,narg);
41 };
42
43 return tolua_tousertype(L, narg, def);
44 }
45
46 extern int push_table_instance(lua_State* L, int lo);
47
48 TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def)
49 {
50 if (lua_gettop(L)<abs(narg))
51 return def;
52 else
53 {
54 void* u;
55 if (!lua_isuserdata(L, narg)) {
56 if (!push_table_instance(L, narg)) return NULL;
57 };
58 u = lua_touserdata(L,narg);
59 return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */
60 }
61 }
62
63 TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def)
64 {
65 return lua_gettop(L)<abs(narg) ? def : narg;
66 }
67
68 TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def)
69 {
70 return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg);
71 }
72
73 TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def)
74 {
75 double v;
76 lua_pushnumber(L,index);
77 lua_gettable(L,lo);
78 v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
79 lua_pop(L,1);
80 return v;
81 }
82
83 TOLUA_API const char* tolua_tofieldstring
84 (lua_State* L, int lo, int index, const char* def)
85 {
86 const char* v;
87 lua_pushnumber(L,index);
88 lua_gettable(L,lo);
89 v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
90 lua_pop(L,1);
91 return v;
92 }
93
94 TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def)
95 {
96 void* v;
97 lua_pushnumber(L,index);
98 lua_gettable(L,lo);
99 v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
100 lua_pop(L,1);
101 return v;
102 }
103
104 TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def)
105 {
106 void* v;
107 lua_pushnumber(L,index);
108 lua_gettable(L,lo);
109 v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */
110 lua_pop(L,1);
111 return v;
112 }
113
114 TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def)
115 {
116 int v;
117 lua_pushnumber(L,index);
118 lua_gettable(L,lo);
119 v = lua_isnil(L,-1) ? def : lo;
120 lua_pop(L,1);
121 return v;
122 }
123
124 TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def)
125 {
126 int v;
127 lua_pushnumber(L,index);
128 lua_gettable(L,lo);
129 v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1);
130 lua_pop(L,1);
131 return v;
132 }