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
|
#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; };
///
/// 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)
///
/// ��������ʵ�ֵĺꡣ����������һ��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_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);
///
/// ����࣬Ϊ��ʵ�ֶ�̬����Ҫ����������Щ�ӿڵ��ⲿ������Ҫ��̳д��֮࣬�����������оͻ�
/// ���ö�Ӧʵ��ķ�����ע��̳д���ʱ����ʵ������ķ�����ʵ����LuaxNativeClass�У�ʵ�ֻ�
/// ���¶����ԡ�
///
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; };
};
///
/// ��Ҫ��¶��lua��native class��Ҫ�̳д��ࡣͨ��lua������ʵ��Ҫȷ�����ü�������ȷ�ԣ���
/// ����߳�����Ҫȷ���������ͷš�
///
template<class T>
class LuaxNativeClass : public virtual ILuaxNativeAccessor
{
public:
static bool IsTypeOf(ILuaxNativeAccessor);
///
/// ��userdata��Ϊkey����ref table���userdata����һ�����ã���ά��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_TABLE��������userdata����
/// LuaxRetain\LuaxRelease��member ref����ʱ����ʵ����ͬ���Ǵ����������ref table��
/// ����table��_LUAX_STRONGREF_TABLE�
///
static LuaxStrongRef mSingletonRefTable;
///
/// ͨ��userdata�����õ�:
/// 1: ref table
/// 2: member table
/// 3: class table
///
LuaxWeakRef mUserdata;
public:
// ��������
LUAX_DECL_METHOD( l___tostring );
LUAX_DECL_METHOD( l_GetClass );
LUAX_DECL_METHOD( l_GetClassName );
// ���������
LUAX_DECL_METHOD( l___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( l___tostring );
LUAX_DECL_METHOD( l_Extend );
LUAX_DECL_METHOD( l_New );
LUAX_DECL_METHOD( l_TypeOf );
};
#endif
}
#endif
|