Lua Utils
Biblioteca utilitária para facilitar a integração de Lua com C++
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 
32 class LUAUTILS_API_EXPORT LuaProxy
33 {
34 public:
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 
196 template <typename T>
198 {
199 public:
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 
225 private:
226  T _obj;
227 
229  static int l___gc(lua_State * L)
230  {
231  // Recover the proxy object (first parameter, encapsulated
232  // in a userdata):
233  LuaSimpleProxy<T> * proxy = LuaProxy::toObjectOfClass< LuaSimpleProxy<T> >(L, 1);
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 
248 template <typename T>
250 {
251 public:
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 
283 private:
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):
291  LuaSimplePtrProxy<T> * proxy = LuaProxy::toObjectOfClass< LuaSimplePtrProxy<T> >(L, 1);
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 
319 template <typename T, const char* typeName>
321 {
322 public:
323  LuaSimpleTypedPtrProxy(T* obj) : LuaSimplePtrProxy(obj) {}
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
static T * toObjectOfClass(lua_State *L, int index)
Definition: luaProxy.h:112
T * _obj
Objeto encapsulado.
Definition: luaProxy.h:284
static T * checkObjectOfClass(lua_State *L, int index)
Definition: luaProxy.h:138
T * get()
Definition: luaProxy.h:263
static int l___gc(lua_State *L)
Método de coleta de lixo. Deleta o proxy.
Definition: luaProxy.h:287
T _obj
Objeto encapsulado.
Definition: luaProxy.h:226
T value() const const
static T * toObjectOfClass(const QVariant &v)
Definition: luaProxy.h:125
Definition: luaProxy.h:320
Definition: luaProxy.h:32
void * getClassMetatableID()
Definition: luaProxy.h:211
void populateMetatable(lua_State *L, int index)
Definition: luaProxy.h:218
void release()
Deletes the object stored by this proxy. Further calls to get will return NULL.
Definition: luaProxy.h:266
void * getClassMetatableID()
Definition: luaProxy.h:269
T & get()
Definition: luaProxy.h:208
Definition: luaProxy.h:197
LuaSimplePtrProxy(T *obj)
Definition: luaProxy.h:255
static int l___gc(lua_State *L)
Método de coleta de lixo. Deleta o proxy.
Definition: luaProxy.h:229
Declaration of usefull configuration definitions and some compatibility options fro compiling with se...
virtual ~LuaSimplePtrProxy()
Destructor.
Definition: luaProxy.h:258
void populateMetatable(lua_State *L, int index)
Definition: luaProxy.h:326
Definition: luaProxy.h:40
void populateMetatable(lua_State *L, int index)
Definition: luaProxy.h:276
LuaSimpleProxy(const T &obj)
Definition: luaProxy.h:203
Definition: luaProxy.h:249