-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME
110 lines (82 loc) · 2.3 KB
/
README
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
README
======
Table of Contents
=================
1 LuaH
1.1 What is LuaH?
1.2 Tutor
1.2.1 Import a function to Lua
1.2.2 Call a lua function
1.2.3 Import a class to Lua
1 LuaH
~~~~~~~
1.1 What is LuaH?
==================
When using lua in C++, the lua's native api is well designed,
but import functions to lua become a repetitive work.
So I wrote this library to help me to do this work. Compare
to other bind librarys, it keeps very lightweight. I'm trying
to keep the API simple.
1.2 Tutor
==========
1.2.1 Import a function to Lua
-------------------------------
in C++:
You can import a function to lua as follow:
// function:
int function_to_export(int x) {
return x + 1;
}
// import:
// ...
luah::add_func(L, "my_func", function_to_export);
// ...
then, use it in lua:
print(my_func(3))
You can also import a function to a table:
// following code import the function_to_export to table
lua_newtable(L);
luah::add_func_table(L, -1, "my_func", function_to_export);
lua_setglobal(L, "my_table");
1.2.2 Call a lua function
--------------------------
if we have a lua function:
function double(x)
return x * 2
end
then we can call it in C++:
int ret = luah::call_func<int>(L, "double", 6);
1.2.3 Import a class to Lua
----------------------------
To make import a class to lua, it can be do as follow:
// a example class
class MyClass {
public:
MyClass(int x, int y) :
x_(x), y_(y) {}
int get_sum() {
return x_ + y_;
}
void set_x(int x) {
x_ = x;
}
void set_y(int y) {
y_ = y;
}
private:
int x_, y_;
};
// import code:
luah::add_class<MyClass>("myclass")[luah::ctor<int, int>()]
<< luah::method("get_sum", &MyClass::get_sum)
<< luah::method("set_x", &MyClass::set_x)
<< luah::method("set_y", &MyClass::set_y);
then use in lua:
a = myclass(2, 3)
print(a.get_sum())
a.set_x(4)
print(a.get_sum())
hint:
- the [luah::ctor<int, int>] is the to set the construct function of the class,
it means new T(int, int) will be called to create a new instance,
if you just want to call new T(), this part could be ignored.