1 | /* |
---|
2 | * Lenny Palozzi - lenny.palozzi@home.com |
---|
3 | */ |
---|
4 | |
---|
5 | #include <string> |
---|
6 | #include <iostream> |
---|
7 | |
---|
8 | #include "luaincl.h" |
---|
9 | |
---|
10 | /*extern "C" { |
---|
11 | #include <lua.h> |
---|
12 | } |
---|
13 | */ |
---|
14 | template <class T> |
---|
15 | class Luna |
---|
16 | { |
---|
17 | |
---|
18 | |
---|
19 | public: |
---|
20 | /* member function map */ |
---|
21 | struct RegType { |
---|
22 | const char* name; |
---|
23 | int(T::*mfunc)(lua_State*); |
---|
24 | }; |
---|
25 | /* register class T */ |
---|
26 | static void Register(lua_State* L) { |
---|
27 | lua_pushcfunction(L, &Luna<T>::constructor); |
---|
28 | lua_setglobal(L, T::className); |
---|
29 | |
---|
30 | /* if (otag == 0) { |
---|
31 | otag = lua_newtag(L); |
---|
32 | lua_pushcfunction(L, &Luna<T>::gc_obj); |
---|
33 | lua_settagmethod(L, otag, "gc"); /* tm to release objects |
---|
34 | }*/ |
---|
35 | } |
---|
36 | |
---|
37 | private: |
---|
38 | static int otag; /* object tag */ |
---|
39 | |
---|
40 | /* member function dispatcher */ |
---|
41 | static int thunk(lua_State* L) { |
---|
42 | /* stack = closure(-1), [args...], 'self' table(1) */ |
---|
43 | int i = static_cast<int>(lua_tonumber(L,-1)); |
---|
44 | lua_pushnumber(L, 0); /* userdata object at index 0 */ |
---|
45 | lua_gettable(L, 1); |
---|
46 | T* obj = static_cast<T*>(lua_touserdata(L,-1)); |
---|
47 | lua_pop(L, 2); /* pop closure value and obj */ |
---|
48 | return (obj->*(T::Register[i].mfunc))(L); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | static int setScriptable(T* obj) |
---|
53 | { |
---|
54 | //return registerObject(obj); |
---|
55 | } |
---|
56 | |
---|
57 | static int setScriptable(const std::string& objectName) |
---|
58 | { |
---|
59 | #warning 'implement do not use' |
---|
60 | // return registerObject(obj); |
---|
61 | } |
---|
62 | |
---|
63 | /* constructs T objects */ |
---|
64 | static int constructor(lua_State* L) { |
---|
65 | T* obj= new T(L); /* new T */ |
---|
66 | return registerObject(obj, L); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /* releases objects */ |
---|
71 | static int gc_obj(lua_State* L) { |
---|
72 | T* obj = static_cast<T*>(lua_touserdata(L, -1)); |
---|
73 | delete obj; |
---|
74 | return 0; |
---|
75 | } |
---|
76 | protected: |
---|
77 | Luna(); /* hide default constructor */ |
---|
78 | |
---|
79 | private: |
---|
80 | static int registerObject(T* obj, lua_State* L) |
---|
81 | { |
---|
82 | lua_newtable(L); /* new table object */ |
---|
83 | int objRef = luaL_ref (L, LUA_REGISTRYINDEX); |
---|
84 | std::cout<<"test"<<std::endl; |
---|
85 | if(lua_getmetatable(L, objRef) != 0) |
---|
86 | { |
---|
87 | std::cout<<"test"<<std::endl; |
---|
88 | lua_pushstring (L, "__gc"); |
---|
89 | lua_pushcfunction(L, &Luna<T>::gc_obj); |
---|
90 | lua_rawset(L,-3); |
---|
91 | // lua_setfield(L, objRef, "__gc"); |
---|
92 | } |
---|
93 | |
---|
94 | lua_pushnumber(L, 0); /* userdata obj at index 0 */ |
---|
95 | |
---|
96 | // lua_pushusertag(L, obj, otag); /* have gc call tm */ |
---|
97 | |
---|
98 | lua_settable(L, -3); |
---|
99 | |
---|
100 | /* register the member functions */ |
---|
101 | for (int i=0; T::Register[i].name; i++) { |
---|
102 | lua_pushstring(L, T::Register[i].name); |
---|
103 | lua_pushnumber(L, i); |
---|
104 | lua_pushcclosure(L, &Luna<T>::thunk, 1); |
---|
105 | lua_settable(L, -3); |
---|
106 | } |
---|
107 | return 1; /* return the table object */ |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | }; |
---|
112 | template <class T> |
---|
113 | int Luna<T>::otag = 0; |
---|