-
Notifications
You must be signed in to change notification settings - Fork 52
/
StdFunction.h
135 lines (109 loc) · 4.16 KB
/
StdFunction.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
#ifndef LUAGLUE_STACKTEMPLATES_STDFUNCTION_H_GUARD
#define LUAGLUE_STACKTEMPLATES_STDFUNCTION_H_GUARD
// FIXME: LuaGlueLuaFuncRef should turn into a LuaGlueType subclass
#include "LuaGlue/LuaGlueLuaFuncRef.h"
#include "LuaGlue/LuaGlueStdFuncWrapper.h"
template<typename _Ret, typename... _Args>
inline std::function<_Ret(_Args...)> stack<std::function<_Ret(_Args...)>&>::get(LuaGlueBase *b, lua_State *s, int idx)
{
luaL_checktype(s, idx, LUA_TFUNCTION); // must be a function
const char *funcname = lua_getupvalue(s, idx, 1);
if(funcname) { /* !funcname means no upvalues */
// lua_getupvalue pushes the upvalue value onto the stack
auto wrapper = (FuncWrapperType *)lua_touserdata(s, -1);
if(wrapper)
{
FuncWrapperType fw = *wrapper;
delete wrapper;
lua_pop(s, 1); // remove upvalue value
return fw;
}
}
// got a lua function, create reference to it.
return LuaGlueLuaFuncRef<_Ret, _Args...>(b, idx);
}
template<typename _Ret, typename... _Args>
inline void stack<std::function<_Ret(_Args...)>&>::put(LuaGlueBase *b, lua_State *, std::function<_Ret(_Args...)> _f)
{
auto func = new LuaGlueStdFuncWrapper<_Ret, _Args...>(b, _f);
func->glue(b);
}
template<typename _Ret, typename... _Args>
inline std::function<_Ret(_Args...)> stack<const std::function<_Ret(_Args...)>&>::get(LuaGlueBase *b, lua_State *s, int idx)
{
luaL_checktype(s, idx, LUA_TFUNCTION); // must be a function
const char *funcname = lua_getupvalue(s, idx, 1);
if(funcname) { /* !funcname means no upvalues */
// lua_getupvalue pushes the upvalue value onto the stack
auto wrapper = (FuncWrapperType *)lua_touserdata(s, -1);
if(wrapper)
{
FuncWrapperType fw = *wrapper;
delete wrapper;
lua_pop(s, 1); // remove upvalue value
return fw;
}
}
// got a lua function, create reference to it.
return LuaGlueLuaFuncRef<_Ret, _Args...>(b, idx);
}
template<typename _Ret, typename... _Args>
inline void stack<const std::function<_Ret(_Args...)>&>::put(LuaGlueBase *b, lua_State *s, std::function<_Ret(_Args...)> _f)
{
auto func = new LuaGlueStdFuncWrapper<_Ret, _Args...>(b, _f);
func.glue(b);
}
template<typename... _Args>
inline std::function<void(_Args...)> stack<std::function<void(_Args...)>&>::get(LuaGlueBase *b, lua_State *s, int idx)
{
luaL_checktype(s, idx, LUA_TFUNCTION); // must be a function
const char *funcname = lua_getupvalue(s, idx, 1);
if(funcname) { /* !funcname means no upvalues */
// lua_getupvalue pushes the upvalue value onto the stack
auto wrapper = (FuncWrapperType *)lua_touserdata(s, -1);
if(wrapper)
{
FuncWrapperType fw = *wrapper;
delete wrapper;
lua_pop(s, 1); // remove upvalue value
return fw;
}
lua_pop(s, 1); // remove upvalue value
}
// got a lua function, create reference to it.
return LuaGlueLuaFuncRef<void, _Args...>(b, idx);
}
template<typename... _Args>
inline void stack<std::function<void(_Args...)>&>::put(LuaGlueBase *b, lua_State *s, std::function<void(_Args...)> _f)
{
auto func = new LuaGlueStdFuncWrapper<void, _Args...>(b, _f);
func.glue(b);
}
template<typename... _Args>
inline std::function<void(_Args...)> stack<const std::function<void(_Args...)>&>::get(LuaGlueBase *b, lua_State *s, int idx)
{
luaL_checktype(s, idx, LUA_TFUNCTION); // must be a function
const char *funcname = lua_getupvalue(s, idx, 1);
if(funcname) { /* !funcname means no upvalues */
// lua_getupvalue pushes the upvalue value onto the stack
auto wrapper = (FuncWrapperType *)lua_touserdata(s, -1);
if(wrapper)
{
FuncWrapperType fw = *wrapper;
delete wrapper;
lua_pop(s, 1); // remove upvalue value
return fw;
}
lua_pop(s, 1); // remove upvalue value
}
// got a lua function, create reference to it.
return LuaGlueLuaFuncRef<void, _Args...>(b, idx);
}
template<typename... _Args>
inline void stack<const std::function<void(_Args...)>&>::put(LuaGlueBase *b, lua_State *s, const std::function<void(_Args...)> &_f)
{
auto func = new LuaGlueStdFuncWrapper<void, _Args...>(b, _f);
func.glue(b);
}
// end std::function stack handlers
#endif /* LUAGLUE_STACKTEMPLATES_STDFUNCTION_H_GUARD */