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