-
Notifications
You must be signed in to change notification settings - Fork 49
/
PopValue.h
executable file
·191 lines (166 loc) · 3.84 KB
/
PopValue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright (C) <2012> <huangweilook@21cn.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _POPVALUE_H
#define _POPVALUE_H
#include <set>
namespace luacpp{
extern std::set<void*> userdataSet;
template<typename T>
inline T _pop(lua_State *L,Int2Type<true>)
{
typedef typename pointerTraits<T>::PointeeType rType;
objUserData<rType> *obj = objUserData<rType>::checkobjuserdata(L,-1);
lua_pop(L,1);
return obj->ptr;
}
template<typename T>
inline T _pop_impl(lua_State *L,Int2Type<true>)
{
T ret = (T)lua_tonumber(L,-1);
lua_pop(L,1);
return ret;
}
template<typename T>
inline T _pop_impl(lua_State *L,Int2Type<false>)
{
return *_pop<typename refTraits<T>::RefType*>(L,Int2Type<true>());
}
template<typename T>
inline T _pop(lua_State *L,Int2Type<false>)
{
return _pop_impl<T>(L,Int2Type<IndexOf<SupportType,T>::value >= 0>());
}
template<typename T>
inline T popvalue(lua_State *L)
{
return _pop<T>(L,Int2Type<pointerTraits<T>::isPointer>());
}
template<>
inline luaObject popvalue(lua_State *L)
{
int r = luaL_ref(L,LUA_REGISTRYINDEX);
luaObject obj(L,r);
return obj;
}
template<>
inline std::string popvalue(lua_State *L)
{
const char *str = lua_tostring(L,-1);
std::string ret(str);
lua_pop(L,1);
return ret;
}
template<typename T>
inline T pop_void_ptr(lua_State *L)
{
T ret;
ret = lua_touserdata(L,-1);
if(userdataSet.find((void*)ret) != userdataSet.end())
ret = ((objUserData<void>*)ret)->ptr;
lua_pop(L,1);
return ret;
}
template<>
inline const void *popvalue(lua_State *L)
{
return pop_void_ptr<const void*>(L);
}
template<>
inline void *popvalue(lua_State *L)
{
return pop_void_ptr<void*>(L);
}
template<>
inline bool popvalue(lua_State *L)
{
bool ret = lua_toboolean(L,-1) == 1;
lua_pop(L,1);
return ret;
}
template<>
inline double popvalue(lua_State *L)
{
double ret = lua_tonumber(L,-1);
lua_pop(L,1);
return ret;
}
template<>
inline float popvalue(lua_State *L)
{
double ret = lua_tonumber(L,-1);
lua_pop(L,1);
return (float)ret;
}
template<>
inline luatable popvalue(lua_State *L)
{
luatable ret;
int len = luaL_len(L, -1);
for( int i = 1; i <= len; ++i)
{
lua_rawgeti(L,-1,i);
int type = lua_type(L,-1);
if(type == LUA_TNIL)
{
ret.push_back(any());
lua_pop(L,1);
}
else if(type == LUA_TUSERDATA || type == LUA_TLIGHTUSERDATA)
{
void *r = lua_touserdata(L,-1);
if(userdataSet.find(r) != userdataSet.end())
ret.push_back((const void*)((objUserData<void>*)r)->ptr);
else
ret.push_back(r);
lua_pop(L,1);
}
else if(type == LUA_TNUMBER){
ret.push_back(popvalue<double>(L));
}
else if(type == LUA_TSTRING)
ret.push_back(popvalue<std::string>(L));
else if(type == LUA_TBOOLEAN)
ret.push_back(popvalue<bool>(L));
else if(type == LUA_TTABLE)
{
int _len = luaL_len(L, -1);
if(_len == 0)
{
lua_pushnil(L);//push a key
if(lua_next(L,-2)){
lua_pop(L,2);//pop value and value
ret.push_back(popvalue<luaObject>(L));
}
else{
ret.push_back(popvalue<luatable>(L));
}
}else{
ret.push_back(popvalue<luatable>(L));
}
}
else
throw std::string("error");
}
lua_pop(L,1);
return ret;
}
//Get lua global object
template<typename T>
T Get(lua_State *L,const char *name)
{
lua_getglobal(L,name);
return popvalue<T>(L);
}
}
#endif