Lua Utils
Biblioteca utilitária para facilitar a integração de Lua com C++
Loading...
Searching...
No Matches
luaProxy.h
Go to the documentation of this file.
1/************************************************************************
2**
3** Copyright (C) 2009 K2FS Sistemas e Projetos Ltd.
4** All rights reserved.
5**
6************************************************************************/
7
20#ifndef _LUA_PROXY_H_
21#define _LUA_PROXY_H_
22
23#include "luaConfig.h"
24
25#include <lua.hpp>
26#include <QVariant>
27
32class LUAUTILS_API_EXPORT LuaProxy
33{
34public:
35
40 class Base
41 {
42 public:
51 virtual void* getClassMetatableID() = 0;
52
68 virtual void populateMetatable(lua_State* L, int index) = 0;
69
70 virtual ~Base() {}
71 };
72
73
79 static void pushObject(lua_State* L, Base* obj);
80
85 static void pushMetatable(lua_State* L, Base* obj);
86
93 static bool isObject(lua_State* L, int index);
94
101 static Base* toObject(lua_State* L, int index);
102
111 template <typename T>
112 static T * toObjectOfClass(lua_State* L, int index)
113 {
114 return dynamic_cast<T *>(toObject(L, index));
115 }
116
124 template <typename T>
125 static T* toObjectOfClass(const QVariant& v)
126 {
127 Base* ptrBase = (Base*)v.value<void*>();
128 return dynamic_cast<T *>(ptrBase);
129 }
130
137 template <typename T>
138 static T* checkObjectOfClass(lua_State* L, int index)
139 {
140 T* ptr = toObjectOfClass<T>(L, index);
141 if(ptr)
142 return ptr;
143 luaL_typerror(L, index, "userdata"); // Não retorna!!!
144 return NULL;
145 }
146
147};
148
149
196template <typename T>
198{
199public:
203 LuaSimpleProxy(const T & obj) : _obj(obj) {}
204
208 T & get() { return _obj; }
209
210 // Implements LuaProxy::Base
212 {
213 static int classID = 0;
214 return &classID;
215 }
216
217 // Implements LuaProxy::Base
218 void populateMetatable(lua_State * L, int index)
219 {
220 lua_pushstring(L, "__gc");
221 lua_pushcfunction(L, l___gc);
222 lua_settable(L, index);
223 }
224
225private:
227
229 static int l___gc(lua_State * L)
230 {
231 // Recover the proxy object (first parameter, encapsulated
232 // in a userdata):
234 if (!proxy)
235 return luaL_error(L, "LuaSimpleProxy garbage collector called on incorrect object type.");
236
237 // Delete it:
238 delete proxy;
239
240 // Done:
241 return 0;
242 }
243};
244
248template <typename T>
250{
251public:
255 LuaSimplePtrProxy(T* obj) : _obj(obj) {}
256
258 virtual ~LuaSimplePtrProxy() { delete _obj; }
259
263 T* get() { return _obj; }
264
266 void release() { delete _obj; _obj = NULL; }
267
268 // Implements LuaProxy::Base
270 {
271 static int classID = 0;
272 return &classID;
273 }
274
275 // Implements LuaProxy::Base
276 void populateMetatable(lua_State * L, int index)
277 {
278 lua_pushstring(L, "__gc");
279 lua_pushcfunction(L, l___gc);
280 lua_settable(L, index);
281 }
282
283private:
284 T* _obj;
285
287 static int l___gc(lua_State * L)
288 {
289 // Recover the proxy object (first parameter, encapsulated
290 // in a userdata):
292 if(!proxy)
293 return luaL_error(L, "LuaSimplePtrProxy garbage collector called on incorrect object type.");
294
295 // Delete it:
296 delete proxy;
297
298 // Done:
299 return 0;
300 }
301};
302
319template <typename T, const char* typeName>
321{
322public:
324
325 // Implements LuaProxy::Base
326 void populateMetatable(lua_State * L, int index)
327 {
329 lua_pushstring(L, typeName);
330 lua_setfield(L, index, "__typeName");
331 }
332};
333
334
335#endif
Definition luaProxy.h:41
virtual void * getClassMetatableID()=0
virtual void populateMetatable(lua_State *L, int index)=0
Definition luaProxy.h:33
static T * toObjectOfClass(const QVariant &v)
Definition luaProxy.h:125
static T * checkObjectOfClass(lua_State *L, int index)
Definition luaProxy.h:138
static T * toObjectOfClass(lua_State *L, int index)
Definition luaProxy.h:112
Definition luaProxy.h:198
T _obj
Objeto encapsulado.
Definition luaProxy.h:226
void * getClassMetatableID()
Definition luaProxy.h:211
LuaSimpleProxy(const T &obj)
Definition luaProxy.h:203
static int l___gc(lua_State *L)
Método de coleta de lixo. Deleta o proxy.
Definition luaProxy.h:229
T & get()
Definition luaProxy.h:208
void populateMetatable(lua_State *L, int index)
Definition luaProxy.h:218
Definition luaProxy.h:250
T * _obj
Objeto encapsulado.
Definition luaProxy.h:284
LuaSimplePtrProxy(T *obj)
Definition luaProxy.h:255
void release()
Deletes the object stored by this proxy. Further calls to get will return NULL.
Definition luaProxy.h:266
virtual ~LuaSimplePtrProxy()
Destructor.
Definition luaProxy.h:258
T * get()
Definition luaProxy.h:263
void populateMetatable(lua_State *L, int index)
Definition luaProxy.h:276
static int l___gc(lua_State *L)
Método de coleta de lixo. Deleta o proxy.
Definition luaProxy.h:287
void * getClassMetatableID()
Definition luaProxy.h:269
Definition luaProxy.h:321
void populateMetatable(lua_State *L, int index)
Definition luaProxy.h:326
Declaration of useful configuration definitions and some compatibility options fro compiling with sev...
T value() const const