diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2016-01-02 20:06:32 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2016-01-02 20:06:32 +0000 |
commit | 7540dae59505b5c5d38507a05116c493cce628cb (patch) | |
tree | 8e27782c0a968ebd2c641d1eb241f2de6f8fca84 /plugins/MirLua/src/mlua_metatable.h | |
parent | cc5d3042679c7afbbf510e4f3e8e9937b2ce0233 (diff) |
MirLua: metatable fixes
git-svn-id: http://svn.miranda-ng.org/main/trunk@15997 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/MirLua/src/mlua_metatable.h')
-rw-r--r-- | plugins/MirLua/src/mlua_metatable.h | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index df42f2dca9..048c4f425c 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -19,6 +19,8 @@ template<typename T> class MT
{
private:
+ lua_State *L;
+
static const char *name;
static std::map<std::string, MTField*> fields;
@@ -30,25 +32,29 @@ private: return res;
}
- static int lua__new(lua_State *L)
+ static void Init(lua_State *L, T **obj)
+ {
+ luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+ T *udata = (T*)lua_touserdata(L, 1);
+ memcpy(*obj, udata, sizeof(T));
+ }
+
+ static void Free(T **obj)
{
- T *obj = NULL;
- T *udata = NULL;
+ *obj = NULL;
+ }
- int type = lua_type(L, 1);
- switch (type)
+ static int lua__new(lua_State *L)
+ {
+ T *udata = (T*)lua_newuserdata(L, sizeof(T));
+ memset(udata, 0, sizeof(T));
+ Init(L, &udata);
+ if (udata == NULL)
{
- case LUA_TLIGHTUSERDATA:
- obj = (T*)MT::Load(L);
- if (obj == NULL) break;
- //case LUA_TNONE:
- udata = (T*)lua_newuserdata(L, sizeof(T));
- memcpy(udata, obj, sizeof(T));
- case LUA_TUSERDATA:
- luaL_setmetatable(L, MT::name);
+ lua_pushnil(L);
return 1;
}
- lua_pushnil(L);
+ luaL_setmetatable(L, MT::name);
return 1;
}
@@ -106,11 +112,8 @@ private: return 0;
}
- static T* Load(lua_State *L) { return (T*)lua_touserdata(L, 1); }
- static void Free(T **obj) { *obj = NULL; }
-
public:
- MT(lua_State *L, const char *tname)
+ MT(lua_State *L, const char *tname) : L(L)
{
MT::name = tname;
@@ -133,7 +136,16 @@ public: return *this;
}
- static void Set(lua_State *L, T* obj)
+ MT& Method(lua_CFunction func, const char *name)
+ {
+ luaL_getmetatable(L, MT::name);
+ lua_pushcfunction(L, func);
+ lua_setfield(L, -2, name);
+ lua_pop(L, 1);
+ return *this;
+ }
+
+ static void Set(lua_State *L, T *obj)
{
if (obj == NULL)
{
|