-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.hpp
81 lines (67 loc) · 2.3 KB
/
function.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
#pragma once
#include <tuple>
#include "api.hpp"
#include "ref.hpp"
/**
* function.hpp
* Contains glua::function, a template class which represents
* a lua function, providing a way to call lua functions from
* c++ code.
*/
namespace glua {
template<typename FuncType>
class function {};
template<typename Ret, typename... Args>
class function<Ret(Args...)> : public ref {
public:
template<typename S, typename = typename std::enable_if<std::is_base_of<selector_base, S>::value>::type>
function(S&& sel) : ref(std::forward<S>(sel)) {}
function(ref&& r) : ref(std::move(r)) {}
function(function&& f) : ref(std::move(f)) {}
virtual ~function() {}
auto operator()(Args&&... args)
-> decltype(api::checkGet<Ret>(l))
{
this->ref::push();
api::push(l, std::forward<Args>(args)...);
api::call(l, sizeof...(Args), 1);
decltype(api::checkGet<Ret>(l)) ret = api::checkGet<Ret>(l);
api::clearStack(l);
return ret;
//return api::checkGet<Ret>(l);
}
};
template<typename... Args>
class function<void(Args...)> : public ref {
public:
template<typename S, typename std::enable_if<std::is_base_of<selector_base, S>::value>::type* = nullptr>
function(S&& sel) : ref(std::forward<S>(sel)) {}
function(ref&& r) : ref(std::move(r)) {}
function(function&& f) : ref(std::move(f)) {}
virtual ~function() {}
void operator()(Args&&... args) {
this->ref::push();
api::push(l, std::forward<Args>(args)...);
api::call(l, sizeof...(Args), 0);
}
};
template<typename... Rets, typename... Args>
class function<std::tuple<Rets...>(Args...)> : public ref {
public:
template<typename S, typename std::enable_if<std::is_base_of<selector_base, S>::value>::type* = nullptr>
function(S&& sel) : ref(std::forward<S>(sel)) {}
function(ref&& r) : ref(std::move(r)) {}
function(function&& f) : ref(std::move(f)) {}
virtual ~function() {}
auto operator()(Args&&... args)
-> decltype(api::checkGet<Rets...>(l))
{
this->ref::push();
api::push(l, std::forward<Args>(args)...);
api::call(l, sizeof...(Args), sizeof...(Rets));
decltype(api::checkGet<Rets...>(l)) ret = api::checkGet<Rets...>(l);
api::clearStack(l);
return ret;
}
};
} // namespace glua