forked from zhangpanyi/a-star-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaFunction.hpp
76 lines (68 loc) · 1.74 KB
/
LuaFunction.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
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "lua.h"
#include "lauxlib.h"
#ifdef __cplusplus
}
#endif
#include <string>
#include <functional>
struct LuaRef {
public:
LuaRef();
LuaRef(lua_State* aL, int index);
~LuaRef();
LuaRef(const LuaRef& other);
LuaRef& operator=(const LuaRef& rhs);
LuaRef(LuaRef&& other);
LuaRef& operator=(LuaRef&& rhs);
explicit operator bool() const;
void reset(lua_State* aL, int index);
void push() const;
lua_State* state() const;
protected:
void unref() const;
protected:
lua_State* L;
int ref_;
};
typedef std::function<void(lua_State* aL)> CheckReturnFunction;
struct LuaFunction : public LuaRef
{
public:
LuaFunction();
LuaFunction(lua_State* aL, int index);
LuaFunction(const LuaFunction& other);
LuaFunction& operator=(const LuaFunction& rhs);
LuaFunction(LuaFunction&& other);
LuaFunction& operator=(LuaFunction&& rhs);
void operator()() const;
void ppush() const;
void pcall() const;
template<typename...Args>
void operator()(Args&...args) const
{
ppush();
pusharg(args...);
pcall();
}
void pusharg(bool v) const;
void pusharg(float v) const;
void pusharg(double v) const;
void pusharg(int v) const;
void pusharg(const std::string& v) const;
void pusharg(const char* v) const;
template<typename T, typename... Args>
inline void pusharg(T first, Args... args) const {
pusharg(first);
pusharg(args...);
}
void setReturnCnt(int c) { returnCnt_ = c; }
void setCheckReturnFunction(CheckReturnFunction call) { checkReturn_ = call; }
private:
mutable int trackback_;
int returnCnt_;
CheckReturnFunction checkReturn_;
};