aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Luamm.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/Luamm.hh')
-rw-r--r--src/FbTk/Luamm.hh109
1 files changed, 55 insertions, 54 deletions
diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh
index e173be9..6b26a17 100644
--- a/src/FbTk/Luamm.hh
+++ b/src/FbTk/Luamm.hh
@@ -130,29 +130,30 @@ namespace lua {
130 130
131 // a fancy wrapper around lua_State 131 // a fancy wrapper around lua_State
132 class state { 132 class state {
133 std::shared_ptr<lua_State> cobj; 133 lua_State *cobj;
134 134
135 // destructor for C++ objects stored as lua userdata 135 // destructor for C++ objects stored as lua userdata
136 template<typename T> 136 template<typename T>
137 static int destroy_cpp_object(lua_State *l) 137 static int destroy_cpp_object(lua_State *l)
138 { 138 {
139 T *ptr = static_cast<T *>(lua_touserdata(l, -1)); 139 T *ptr = static_cast<T *>(lua_touserdata(l, -1));
140 assert(ptr); 140 assert(ptr);
141 try { 141 try {
142 // throwing exceptions in destructors is a bad idea 142 // throwing exceptions in destructors is a bad idea
143 // but we catch (and ignore) them, just in case 143 // but we catch (and ignore) them, just in case
144 ptr->~T(); 144 ptr->~T();
145 }
146 catch(...) {
147 }
148 return 0;
149 } 145 }
146 catch(...) {
147 }
148 return 0;
149 }
150 150
151 bool safe_compare(lua_CFunction trampoline, int index1, int index2); 151 bool safe_compare(lua_CFunction trampoline, int index1, int index2);
152 void do_pushclosure(int n); 152 void do_pushclosure(int n);
153 153
154 public: 154 public:
155 state(); 155 state();
156 ~state() { lua_close(cobj); }
156 157
157 /* 158 /*
158 * Lua functions come in three flavours 159 * Lua functions come in three flavours
@@ -179,68 +180,68 @@ namespace lua {
179 180
180 // type a, never throw 181 // type a, never throw
181 int absindex(int index) throw() { return index<0 && -index<=gettop() ? gettop()+1+index : index; } 182 int absindex(int index) throw() { return index<0 && -index<=gettop() ? gettop()+1+index : index; }
182 bool getmetatable(int index) throw() { return lua_getmetatable(cobj.get(), index); } 183 bool getmetatable(int index) throw() { return lua_getmetatable(cobj, index); }
183 int gettop() throw() { return lua_gettop(cobj.get()); } 184 int gettop() throw() { return lua_gettop(cobj); }
184 void insert(int index) throw() { lua_insert(cobj.get(), index); } 185 void insert(int index) throw() { lua_insert(cobj, index); }
185 bool isboolean(int index) throw() { return lua_isboolean(cobj.get(), index); } 186 bool isboolean(int index) throw() { return lua_isboolean(cobj, index); }
186 bool isfunction(int index) throw() { return lua_isfunction(cobj.get(), index); } 187 bool isfunction(int index) throw() { return lua_isfunction(cobj, index); }
187 bool islightuserdata(int index) throw() { return lua_islightuserdata(cobj.get(), index); } 188 bool islightuserdata(int index) throw() { return lua_islightuserdata(cobj, index); }
188 bool isnil(int index) throw() { return lua_isnil(cobj.get(), index); } 189 bool isnil(int index) throw() { return lua_isnil(cobj, index); }
189 bool isnone(int index) throw() { return lua_isnone(cobj.get(), index); } 190 bool isnone(int index) throw() { return lua_isnone(cobj, index); }
190 bool isnumber(int index) throw() { return lua_isnumber(cobj.get(), index); } 191 bool isnumber(int index) throw() { return lua_isnumber(cobj, index); }
191 bool isstring(int index) throw() { return lua_isstring(cobj.get(), index); } 192 bool isstring(int index) throw() { return lua_isstring(cobj, index); }
192 void pop(int n = 1) throw() { lua_pop(cobj.get(), n); } 193 void pop(int n = 1) throw() { lua_pop(cobj, n); }
193 void pushboolean(bool b) throw() { lua_pushboolean(cobj.get(), b); } 194 void pushboolean(bool b) throw() { lua_pushboolean(cobj, b); }
194 void pushinteger(integer n) throw() { lua_pushinteger(cobj.get(), n); } 195 void pushinteger(integer n) throw() { lua_pushinteger(cobj, n); }
195 void pushlightuserdata(void *p) throw() { lua_pushlightuserdata(cobj.get(), p); } 196 void pushlightuserdata(void *p) throw() { lua_pushlightuserdata(cobj, p); }
196 void pushnil() throw() { lua_pushnil(cobj.get()); } 197 void pushnil() throw() { lua_pushnil(cobj); }
197 void pushnumber(number n) throw() { lua_pushnumber(cobj.get(), n); } 198 void pushnumber(number n) throw() { lua_pushnumber(cobj, n); }
198 void pushvalue(int index) throw() { lua_pushvalue(cobj.get(), index); } 199 void pushvalue(int index) throw() { lua_pushvalue(cobj, index); }
199 void rawget(int index) throw() { lua_rawget(cobj.get(), index); } 200 void rawget(int index) throw() { lua_rawget(cobj, index); }
200 void rawgeti(int index, int n) throw() { lua_rawgeti(cobj.get(), index, n); } 201 void rawgeti(int index, int n) throw() { lua_rawgeti(cobj, index, n); }
201 bool rawequal(int index1, int index2) throw() { return lua_rawequal(cobj.get(), index1, index2); } 202 bool rawequal(int index1, int index2) throw() { return lua_rawequal(cobj, index1, index2); }
202 void replace(int index) throw() { lua_replace(cobj.get(), index); } 203 void replace(int index) throw() { lua_replace(cobj, index); }
203 // lua_setmetatable returns int, but docs don't specify it's meaning :/ 204 // lua_setmetatable returns int, but docs don't specify it's meaning :/
204 int setmetatable(int index) throw() { return lua_setmetatable(cobj.get(), index); } 205 int setmetatable(int index) throw() { return lua_setmetatable(cobj, index); }
205 void settop(int index) throw() { return lua_settop(cobj.get(), index); } 206 void settop(int index) throw() { return lua_settop(cobj, index); }
206 bool toboolean(int index) throw() { return lua_toboolean(cobj.get(), index); } 207 bool toboolean(int index) throw() { return lua_toboolean(cobj, index); }
207 integer tointeger(int index) throw() { return lua_tointeger(cobj.get(), index); } 208 integer tointeger(int index) throw() { return lua_tointeger(cobj, index); }
208 number tonumber(int index) throw() { return lua_tonumber(cobj.get(), index); } 209 number tonumber(int index) throw() { return lua_tonumber(cobj, index); }
209 void* touserdata(int index) throw() { return lua_touserdata(cobj.get(), index); } 210 void* touserdata(int index) throw() { return lua_touserdata(cobj, index); }
210 Type type(int index) throw() { return static_cast<Type>(lua_type(cobj.get(), index)); } 211 Type type(int index) throw() { return static_cast<Type>(lua_type(cobj, index)); }
211 // typename is a reserved word :/ 212 // typename is a reserved word :/
212 const char* type_name(Type tp) throw() { return lua_typename(cobj.get(), tp); } 213 const char* type_name(Type tp) throw() { return lua_typename(cobj, tp); }
213 void unref(int t, int ref) throw() { return luaL_unref(cobj.get(), t, ref); } 214 void unref(int t, int ref) throw() { return luaL_unref(cobj, t, ref); }
214 215
215 // type b, throw only on memory allocation errors 216 // type b, throw only on memory allocation errors
216 // checkstack correctly throws bad_alloc, because lua_checkstack kindly informs us of 217 // checkstack correctly throws bad_alloc, because lua_checkstack kindly informs us of
217 // that sitution 218 // that sitution
218 void checkstack(int extra) throw(std::bad_alloc); 219 void checkstack(int extra) throw(std::bad_alloc);
219 const char* gsub(const char *s, const char *p, const char *r) { return luaL_gsub(cobj.get(), s, p, r); } 220 const char* gsub(const char *s, const char *p, const char *r) { return luaL_gsub(cobj, s, p, r); }
220 bool newmetatable(const char *tname) { return luaL_newmetatable(cobj.get(), tname); } 221 bool newmetatable(const char *tname) { return luaL_newmetatable(cobj, tname); }
221 void newtable() { lua_newtable(cobj.get()); } 222 void newtable() { lua_newtable(cobj); }
222 void *newuserdata(size_t size) { return lua_newuserdata(cobj.get(), size); } 223 void *newuserdata(size_t size) { return lua_newuserdata(cobj, size); }
223 // Functor can be anything that FbTk::Slot can handle, everything else remains 224 // Functor can be anything that FbTk::Slot can handle, everything else remains
224 // identical 225 // identical
225 template<typename Functor> 226 template<typename Functor>
226 void pushclosure(const Functor &fn, int n); 227 void pushclosure(const Functor &fn, int n);
227 template<typename Functor> 228 template<typename Functor>
228 void pushfunction(const Functor &fn) { pushclosure(fn, 0); } 229 void pushfunction(const Functor &fn) { pushclosure(fn, 0); }
229 void pushstring(const char *s) { lua_pushstring(cobj.get(), s); } 230 void pushstring(const char *s) { lua_pushstring(cobj, s); }
230 void pushstring(const char *s, size_t len) { lua_pushlstring(cobj.get(), s, len); } 231 void pushstring(const char *s, size_t len) { lua_pushlstring(cobj, s, len); }
231 void pushstring(const std::string &s) { lua_pushlstring(cobj.get(), s.c_str(), s.size()); } 232 void pushstring(const std::string &s) { lua_pushlstring(cobj, s.c_str(), s.size()); }
232 void rawgetfield(int index, const char *k) throw(std::bad_alloc); 233 void rawgetfield(int index, const char *k) throw(std::bad_alloc);
233 void rawset(int index) { lua_rawset(cobj.get(), index); } 234 void rawset(int index) { lua_rawset(cobj, index); }
234 void rawsetfield(int index, const char *k) throw(std::bad_alloc); 235 void rawsetfield(int index, const char *k) throw(std::bad_alloc);
235 int ref(int t) { return luaL_ref(cobj.get(), t); } 236 int ref(int t) { return luaL_ref(cobj, t); }
236 // len recieves length, if not null. Returned value may contain '\0' 237 // len recieves length, if not null. Returned value may contain '\0'
237 const char* tocstring(int index, size_t *len = NULL) { return lua_tolstring(cobj.get(), index, len); } 238 const char* tocstring(int index, size_t *len = NULL) { return lua_tolstring(cobj, index, len); }
238 // Don't use pushclosure() to create a __gc function. The problem is that lua calls them 239 // Don't use pushclosure() to create a __gc function. The problem is that lua calls them
239 // in an unspecified order, and we may end up destroying the object holding the 240 // in an unspecified order, and we may end up destroying the object holding the
240 // std::function before we get a chance to call it. This pushes a function that simply 241 // std::function before we get a chance to call it. This pushes a function that simply
241 // calls ~T when the time comes. Only set it as __gc on userdata of type T. 242 // calls ~T when the time comes. Only set it as __gc on userdata of type T.
242 template<typename T> 243 template<typename T>
243 void pushdestructor() { lua_pushcfunction(cobj.get(), &destroy_cpp_object<T>); } 244 void pushdestructor() { lua_pushcfunction(cobj, &destroy_cpp_object<T>); }
244 245
245 // type c, throw everything but the kitchen sink 246 // type c, throw everything but the kitchen sink
246 // call() is a protected mode call, we don't allow unprotected calls 247 // call() is a protected mode call, we don't allow unprotected calls