-
Notifications
You must be signed in to change notification settings - Fork 49
/
luaObject.h
187 lines (162 loc) · 4.39 KB
/
luaObject.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Copyright (C) <2012> <huangweilook@21cn.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _LUAOBJECT_H
#define _LUAOBJECT_H
#include "luacommon.h"
#include "luaFunction.h"
#define PREPARE(L,index){\
lua_rawgeti(L,LUA_REGISTRYINDEX,index);\
lua_pushstring(L,name);\
lua_gettable(L,-2);\
lua_rawgeti(L,LUA_REGISTRYINDEX,index);\
}
namespace luacpp{
template<typename T>
T popvalue(lua_State *L);
template<typename T>
void push_obj(lua_State *L,const T &obj);
//代表lua中的一个对象(其实就是一个表)
class luaObject
{
public:
luaObject(lua_State *lState,int index)
:m_iKeyIndex(index),m_plState(lState){
counter = new int(1);
}
luaObject()
:m_iKeyIndex(LUA_NOREF),m_plState(NULL),counter(NULL){}
luaObject &operator = (const luaObject & other)
{
if(this != &other)
{
if(!counter)
{
counter = other.counter;
++(*counter);
this->m_iKeyIndex = other.m_iKeyIndex;
this->m_plState = other.m_plState;
}
else
{
if(--(*counter) <= 0)
{
//原来指向的对象现在已经没有其它对象在引用了,可以从lua中释放掉了
if(!this->isNULL())
{
luaL_unref(this->m_plState,LUA_REGISTRYINDEX,this->m_iKeyIndex);
}
delete counter;
}
counter = other.counter;
++(*counter);
this->m_iKeyIndex = other.m_iKeyIndex;
this->m_plState = other.m_plState;
}
}
return *this;
}
luaObject(const luaObject &other)
:m_iKeyIndex(other.m_iKeyIndex),m_plState(other.m_plState),counter(other.counter)
{
++(*counter);
}
~luaObject()
{
if(counter)
{
if(!(--(*counter)))
{
if(!this->isNULL())
{
luaL_unref(m_plState,LUA_REGISTRYINDEX,m_iKeyIndex);
}
delete counter;
}
}
}
template<typename RET,typename KEY>
RET Get(KEY key)
{
lua_rawgeti(m_plState,LUA_REGISTRYINDEX,m_iKeyIndex);
push_obj<KEY>(m_plState,key);
lua_gettable(m_plState,-2);
RET ret = popvalue<RET>(m_plState);
lua_pop(m_plState,1);//pop the table
return ret;
}
template<typename T,typename KEY>
void Set(KEY key,T value)
{
lua_rawgeti(m_plState,LUA_REGISTRYINDEX,m_iKeyIndex);
push_obj<KEY>(m_plState,key);
push_obj<T>(m_plState,value);
lua_settable(m_plState,-3);
lua_pop(m_plState,1);
}
template<typename Ret>
Ret call(const char *name)
{
PREPARE(m_plState,m_iKeyIndex);
return doLuaCall<Ret>::doCall(m_plState,1,0,true);
}
template<typename Ret,typename Arg1>
Ret call(const char *name,const Arg1 &arg1)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
return doLuaCall<Ret>::doCall(m_plState,2,0,true);
}
template<typename Ret,typename Arg1,typename Arg2>
Ret call(const char *name,const Arg1 &arg1,const Arg2 &arg2)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
push_obj<Arg2>(m_plState,arg2);
return doLuaCall<Ret>::doCall(m_plState,3,0,true);
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3>
Ret call(const char *name,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
push_obj<Arg2>(m_plState,arg2);
push_obj<Arg3>(m_plState,arg3);
return doLuaCall<Ret>::doCall(m_plState,4,0,true);
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Arg4>
Ret call(const char *name,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,const Arg4 &arg4)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
push_obj<Arg2>(m_plState,arg2);
push_obj<Arg3>(m_plState,arg3);
push_obj<Arg4>(m_plState,arg4);
return doLuaCall<Ret>::doCall(m_plState,5,0,true);
}
lua_State *getState() const {
return m_plState;
}
int getIndex() const {
return m_iKeyIndex;
}
bool isNULL() const{
return (m_iKeyIndex == LUA_REFNIL) || (m_iKeyIndex == LUA_NOREF);
}
private:
lua_State *m_plState;
int m_iKeyIndex;
int *counter;
};
}
#endif