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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#ifndef __LUAX_CLASS_H__
#define __LUAX_CLASS_H__
#include <vector>
#include "luax_config.h"
#include "luax_ref.h"
#include "luax_memberref.h"
#include "luax_cfunctions.h"
namespace Luax
{
///
/// RegisterLuaxClass עķͳԱö١ȵclass table
/// LuaxGetFactoryName ùͬʱעʱעΪsingletonͨ
/// ʱ
///
#define LUAX_DECL_FACTORY(type) \
static void RegisterLuaxClass(Luax::LuaxState&);\
static void RegisterLuaxPostprocess(Luax::LuaxState&); \
static const char* GetLuaxFactoryName() { return #type; };\
static const char* GetLuaxClassName() { return #type; };\
static bool IsLuaxClassSingleton() { return false; }
///
/// Ϊijʹô˺꣬עһڣעắеãעЩ
///
///
#define LUAX_DECL_ABSTRACT_FACTORY() \
static void RegisterLuaxClass(Luax::LuaxState&);\
static void RegisterLuaxPostprocess(Luax::LuaxState&)
///
/// RegisterLuaxClass עķͳԱö١ȵclass table
/// LuaxGetSingletonName õ
///
#define LUAX_DECL_SINGLETON(type) \
static void RegisterLuaxClass(Luax::LuaxState&); \
static void RegisterLuaxPostprocess(Luax::LuaxState&); \
static const char* GetLuaxSingletonName() { return #type; }; \
static const char* GetLuaxClassName() { return #type; }; \
static bool IsLuaxClassSingleton() { return true; }
#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State* L)
#define LUAX_DECL_ENUM(e, under_line_index) static void _luax_decl_enum_##e()
///
/// ʵֵĺꡣһL
///
#define LUAX_IMPL_METHOD(type, f) int type::f(lua_State* L)
///
/// Ӧóʵֵӿڡһstate
///
#define LUAX_REGISTRY(type) void type::RegisterLuaxClass(Luax::LuaxState& state)
#define LUAX_POSTPROCESS(type) void type::RegisterLuaxPostprocess(Luax::LuaxState& state)
///
/// עĺꡣ
///
#define LUAX_REGISTER_FACTORY(state, type) state.RegisterFactory<type>()
#define LUAX_REGISTER_SINGLETON(state, type) state.RegisterSingleton<type>()
#define LUAX_REGISTER_ABSTRACT_FACTORY(state, type) type::RegisterLuaxPostprocess(state)
#define LUAX_REGISTER_METHODS(state, ...) \
do{ \
luaL_Reg __m[] = {__VA_ARGS__,{0, 0}}; \
state.RegisterMethods(__m); \
}while(0)
#define LUAX_REGISTER_ENUM(state, name, ...) \
do{ \
Luax::LuaxEnum __e[] = {__VA_ARGS__,{0, 0}}; \
state.RegisterEnum(name, __e); \
}while(0)
#define LUAX_PREPARE(L, T) \
LUAX_STATE(L); \
T* self = state.GetUserdata<T>(1);
#define LUAX_INHERIT(state, type) type::RegisterLuaxClass(state)
///
/// ࣬Ϊʵֶ̬ҪЩӿڵⲿҪ̳д֮࣬оͻ
/// öӦʵķע̳дʱʵķʵLuaxNativeClassУʵֻ
/// ¶ԡ
///
/// Effective C++40ڱʹvirtual base£Ӧþܱз
/// ݳԱݳԱʼɵһЩ⡣һ㣬vpbӽӽC#Java
/// InterfaceԣIͷʶһӿڡ
///
class ILuaxNativeAccessor
{
public:
///
/// Աùʵref tableáȡ
///
virtual bool PushLuaxMemberRef(LuaxState& state, int refID) { assert(false); return false; };
virtual bool PushLuaxUserdata(LuaxState& state) { assert(false); return false; };
virtual bool PushLuaxMemberTable(LuaxState& state) { assert(false); return false; };
virtual bool PushLuaxRefTable(LuaxState& state) { assert(false); return false; };
};
///
/// Ҫ¶luanative classҪ̳дࡣͨluaʵҪȷüȷԣ
/// ߳Ҫȷͷš
///
template<class T>
class LuaxNativeClass : virtual public ILuaxNativeAccessor
{
public:
static bool IsTypeOf(ILuaxNativeAccessor);
///
/// userdataΪkeyref tableuserdataһãάuserdataڡ
/// Ƚmember refʵᱻαͬʵõƵЩʵ壬
///luaƵĵgc⡣
///
template<class U> void LuaxRetain(LuaxState& state, U* userdata);
///
/// userdataһref tableԳԻuserdata
///
template<class U> void LuaxRelease(LuaxState& state, U* userdata);
bool PushLuaxMemberRef(LuaxState& state, int refID) override;
///
/// userdata pushջûгʼmUserdataʼúԪѳʼõ
/// userdataջһáһnativeȨƽluaƵķ
///
bool PushLuaxUserdata(LuaxState& state);
bool PushLuaxMemberTable(LuaxState& state);
bool PushLuaxRefTable(LuaxState& state);
protected:
///
/// Աùʵref tableáȡ
///
void SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx);
bool PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
void ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
LuaxNativeClass();
virtual ~LuaxNativeClass();
private:
friend class LuaxState;
static void RegisterLuaxClass(LuaxState& state);
static void RegisterLuaxFactoryClass(LuaxState& state);
static void RegisterLuaxSingletonClass(LuaxState& state);
static void SetLuaxClassTableRef(LuaxState& state, int idx);
static void PushLuaxClassTable(LuaxState& state);
///
/// ȡַҪַֻͨڶϴʵõջϺ;̬ı
/// ȡַ֤üȷҪãʹôݶǴݵַ
///
//void* operator &();
///
/// userdataʵstate
///
void BindToLua(LuaxState& state);
///
/// class table͵С
///
static LuaxStrongRef mClassTable;
///
/// ǵsingletonùϵԱ֤ᱻͨref table
/// ijԱȫڵģֱ_LUAX_STRONGREF_TABLEuserdata
/// LuaxRetain\LuaxReleasemember refʱʵͬǴref table
/// ģtable_LUAX_STRONGREF_TABLE
///
static LuaxStrongRef mSingletonRefTable;
///
/// ͨuserdataõ:
/// 1: ref table
/// 2: member table
/// 3: class table
///
LuaxWeakRef mUserdata;
//
LUAX_DECL_METHOD( _Tostring );
LUAX_DECL_METHOD( l_GetClass );
LUAX_DECL_METHOD( l_GetClassName );
//
LUAX_DECL_METHOD( _GC );
#if LUAX_ENABLE_NATIVE_EXTEND
LUAX_DECL_METHOD( l_ExtendFactory );
#endif
LUAX_DECL_METHOD( l_GetRefTable );
LUAX_DECL_METHOD( l_New );
//
#if LUAX_ENABLE_NATIVE_EXTEND
LUAX_DECL_METHOD( l_ExtendSingleton );
#endif
};
//--------------------------------------------------------------------------------//
#if LUAX_ENABLE_PLAIN_CLASS
///
/// lua
///
class LuaxPlainClass
{
public:
///
/// עںͨregistry()עࡣ
///
static int registry(lua_State* L);
LUAX_DECL_METHOD( _Tostring );
LUAX_DECL_METHOD( l_Extend );
LUAX_DECL_METHOD( l_New );
LUAX_DECL_METHOD( l_TypeOf );
};
#endif
}
#endif
|