-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.hpp
164 lines (138 loc) · 4.88 KB
/
convert.hpp
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
#ifndef __CONVERSIONS_HPP
#define __CONVERSIONS_HPP
#include "detail/class_info.hpp"
#include "detail/operator_new.hpp"
namespace maan{
namespace detail{
int typeerror(lua_State *L, int narg, const char *expected_name, const char *got_name){
const char *msg = lua_pushfstring(L, "%s expected, got %s", expected_name, got_name);
return luaL_argerror(L, narg, msg);
}
template<typename T>
const char* get_class_name(lua_State* L){
lua_rawgetp(L, LUA_REGISTRYINDEX, ClassInfo<T>::get_metatable_key());
lua_pushstring(L, "__name");
lua_rawget(L, -2);
const char* name = lua_tostring(L, -1);
lua_pop(L, 2);
return name;
}
const char* get_class_name(lua_State* L, int idx){
lua_pushstring(L, "__name");
lua_rawget(L, idx - 1);
const char* name = lua_tostring(L, -1);
lua_pop(L, 1);
return name;
}
}
template<class condition, typename R = void >
using EnableIf = typename std::enable_if<condition::value, R>::type;
template<class T>
EnableIf<std::is_floating_point<T>,
T> get_LuaValue(lua_State* L){
T val = lua_tonumber(L, -1);
lua_pop(L, 1);
return val;
}
void push_LuaValue(lua_State* L, bool val){
lua_pushboolean(L, val);
}
void push_LuaValue(lua_State* L, const char* val){
lua_pushstring(L, val);
}
void push_LuaValue(lua_State* L, const std::string& val){
lua_pushstring(L, val.c_str());
}
template<class T>
EnableIf<std::is_integral<T>,
void> push_LuaValue(lua_State* L, T val){
lua_pushinteger(L, val);
}
template<class T>
EnableIf<std::is_integral<T>,
T> get_LuaValue(lua_State* L){
T val = lua_tointeger(L, -1);
lua_pop(L, 1);
return val;
}
template<>
bool get_LuaValue<bool>(lua_State* L){
bool val = lua_toboolean(L, -1);
lua_pop(L, 1);
return val;
}
template<class T>
EnableIf<std::is_floating_point<T>,
void> push_LuaValue(lua_State* L, T val){
lua_pushnumber(L, val);
}
//TODO: Perhaps we should return a reference.
template<
class T,
typename type_ = typename std::remove_const<typename std::remove_reference<T>::type>::type
>
EnableIf<std::is_class<type_>,
T> get_LuaValue(lua_State* L){
//We didn't get a userdatum! Raise error.
if(!lua_isuserdata(L, -1)){
const char* expected_name = detail::get_class_name<type_>(L);
detail::typeerror(L, -1, expected_name, luaL_typename(L, -1));
}
//Check if we get userdata of the expected type by comparing the metatables.
lua_getmetatable(L, -1);
lua_rawgetp(L, LUA_REGISTRYINDEX, detail::ClassInfo<type_>::get_metatable_key());
if(!lua_rawequal(L, -1, -2)){
const char* expected_name = detail::get_class_name(L, -1);
lua_pop(L, 1);
const char* got_name = detail::get_class_name(L, -1);
lua_pop(L, 1);
detail::typeerror(L, -1, expected_name, got_name);
}
lua_pop(L, 2);
const type_* val = static_cast<const type_*>(lua_touserdata(L, -1));
lua_pop(L, 1);
return *val;
}
template<class T>
EnableIf<std::is_class<T>,
void> push_LuaValue(lua_State* L, const T& val){
create_LuaObject<T>(L, val);
lua_rawgetp(L, LUA_REGISTRYINDEX, detail::ClassInfo<T>::get_metatable_key());
lua_setmetatable(L, -2);
}
//TODO: These two need reconsidering. i.e. why not lightuserdata?
//And if normal userdata, we should check for correct class.
template<class T>
EnableIf<std::is_pointer<T>,
T> get_LuaValue(lua_State* L){
T val = static_cast<T>(lua_touserdata(L, -1));
lua_pop(L, 1);
return val;
}
template<>
const char* get_LuaValue<const char*>(lua_State* L){
const char* val = lua_tostring(L, -1);
lua_pop(L, 1);
return val;
}
template<>
std::string get_LuaValue<std::string>(lua_State* L){
const char* val = lua_tostring(L, -1);
lua_pop(L, 1);
return val;
}
template<>
const std::string& get_LuaValue<const std::string&>(lua_State* L){
const char* val = lua_tostring(L, -1);
lua_pop(L, 1);
//@note: We have to rely on lua's gc here because we cannot return a reference to a temporary.
auto ret = create_LuaGCObject<std::string>(L, val);
return *ret;
}
template<class T>
EnableIf<std::is_pointer<T>,
void> push_LuaValue(lua_State* L, T val){
lua_pushuserdata(L, val);
}
}
#endif