aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Luamm.cc
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-05-19 11:36:12 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:52:45 (GMT)
commit696039d730244f3543b2e0bed8cd384a9958d5ea (patch)
tree87d360e0fbb9d99904de83a8f63b26ea90fcfd34 /src/FbTk/Luamm.cc
parent7a9411dde66117e9d6e1019e0dd9209ab18e78ed (diff)
downloadfluxbox_pavel-696039d730244f3543b2e0bed8cd384a9958d5ea.zip
fluxbox_pavel-696039d730244f3543b2e0bed8cd384a9958d5ea.tar.bz2
c++ lua binding: get rid of shared_ptr
It was pretty underused anyway. I was just lazy to write a proper destructor.
Diffstat (limited to 'src/FbTk/Luamm.cc')
-rw-r--r--src/FbTk/Luamm.cc101
1 files changed, 53 insertions, 48 deletions
diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc
index d17096f..ff3669f 100644
--- a/src/FbTk/Luamm.cc
+++ b/src/FbTk/Luamm.cc
@@ -221,51 +221,56 @@ namespace lua {
221 } 221 }
222 222
223 state::state() 223 state::state()
224 : cobj(luaL_newstate())
224 { 225 {
225 if(lua_State *l = luaL_newstate()) 226 if(cobj == NULL) {
226 cobj.reset(l, &lua_close);
227 else {
228 // docs say this can happen only in case of a memory allocation error 227 // docs say this can happen only in case of a memory allocation error
229 throw std::bad_alloc(); 228 throw std::bad_alloc();
230 } 229 }
231 230
232 // set our panic function 231 try {
233 lua_atpanic(cobj.get(), panic_throw); 232 // set our panic function
234 233 lua_atpanic(cobj, panic_throw);
235 checkstack(2); 234
236 235 checkstack(2);
237 // store a pointer to ourselves 236
238 pushlightuserdata(this); 237 // store a pointer to ourselves
239 rawsetfield(REGISTRYINDEX, this_cpp_object); 238 pushlightuserdata(this);
240 239 rawsetfield(REGISTRYINDEX, this_cpp_object);
241 // a metatable for C++ exceptions travelling through lua code 240
242 newmetatable(cpp_exception_metatable); 241 // a metatable for C++ exceptions travelling through lua code
243 lua_pushcfunction(cobj.get(), &exception_to_string); 242 newmetatable(cpp_exception_metatable);
244 rawsetfield(-2, "__tostring"); 243 lua_pushcfunction(cobj, &exception_to_string);
245 pushboolean(false); 244 rawsetfield(-2, "__tostring");
246 rawsetfield(-2, "__metatable"); 245 pushboolean(false);
247 pushdestructor<std::exception_ptr>(); 246 rawsetfield(-2, "__metatable");
248 rawsetfield(-2, "__gc"); 247 pushdestructor<std::exception_ptr>();
249 pop(); 248 rawsetfield(-2, "__gc");
250 249 pop();
251 // a metatable for C++ functions callable from lua code 250
252 newmetatable(cpp_function_metatable); 251 // a metatable for C++ functions callable from lua code
253 pushboolean(false); 252 newmetatable(cpp_function_metatable);
254 rawsetfield(-2, "__metatable"); 253 pushboolean(false);
255 pushdestructor<FbTk::Slot<int, state *> >(); 254 rawsetfield(-2, "__metatable");
256 rawsetfield(-2, "__gc"); 255 pushdestructor<FbTk::Slot<int, state *> >();
257 pop(); 256 rawsetfield(-2, "__gc");
258 257 pop();
259 // while they're travelling through C++ code, lua exceptions will reside here 258
260 newtable(); 259 // while they're travelling through C++ code, lua exceptions will reside here
261 rawsetfield(REGISTRYINDEX, lua_exception_namespace); 260 newtable();
262 261 rawsetfield(REGISTRYINDEX, lua_exception_namespace);
263 luaL_openlibs(cobj.get()); 262
263 luaL_openlibs(cobj);
264 }
265 catch(...) {
266 lua_close(cobj);
267 throw;
268 }
264 } 269 }
265 270
266 void state::call(int nargs, int nresults, int errfunc) 271 void state::call(int nargs, int nresults, int errfunc)
267 { 272 {
268 int r = lua_pcall(cobj.get(), nargs, nresults, errfunc); 273 int r = lua_pcall(cobj, nargs, nresults, errfunc);
269 if(r == 0) 274 if(r == 0)
270 return; 275 return;
271 276
@@ -301,7 +306,7 @@ namespace lua {
301 306
302 void state::checkstack(int extra) throw(std::bad_alloc) 307 void state::checkstack(int extra) throw(std::bad_alloc)
303 { 308 {
304 if(not lua_checkstack(cobj.get(), extra)) 309 if(not lua_checkstack(cobj, extra))
305 throw std::bad_alloc(); 310 throw std::bad_alloc();
306 } 311 }
307 312
@@ -309,7 +314,7 @@ namespace lua {
309 { 314 {
310 assert(n>=0); 315 assert(n>=0);
311 checkstack(1); 316 checkstack(1);
312 lua_pushcfunction(cobj.get(), safe_concat_trampoline); 317 lua_pushcfunction(cobj, safe_concat_trampoline);
313 insert(-n-1); 318 insert(-n-1);
314 call(n, 1, 0); 319 call(n, 1, 0);
315 } 320 }
@@ -326,7 +331,7 @@ namespace lua {
326 int state::gc(int what, int data) 331 int state::gc(int what, int data)
327 { 332 {
328 checkstack(3); 333 checkstack(3);
329 lua_pushcfunction(cobj.get(), safe_gc_trampoline); 334 lua_pushcfunction(cobj, safe_gc_trampoline);
330 pushinteger(what); 335 pushinteger(what);
331 pushinteger(data); 336 pushinteger(data);
332 call(2, 1, 0); 337 call(2, 1, 0);
@@ -349,7 +354,7 @@ namespace lua {
349 checkstack(2); 354 checkstack(2);
350 pushvalue(index); 355 pushvalue(index);
351 insert(-2); 356 insert(-2);
352 lua_pushcfunction(cobj.get(), (&safe_misc_trampoline<&lua_gettable, 1>)); 357 lua_pushcfunction(cobj, (&safe_misc_trampoline<&lua_gettable, 1>));
353 insert(-3); 358 insert(-3);
354 call(2, 1, 0); 359 call(2, 1, 0);
355 } 360 }
@@ -362,7 +367,7 @@ namespace lua {
362 void state::loadfile(const char *filename) 367 void state::loadfile(const char *filename)
363 throw(lua::syntax_error, lua::file_error, std::bad_alloc) 368 throw(lua::syntax_error, lua::file_error, std::bad_alloc)
364 { 369 {
365 switch(luaL_loadfile(cobj.get(), filename)) { 370 switch(luaL_loadfile(cobj, filename)) {
366 case 0: 371 case 0:
367 return; 372 return;
368 case LUA_ERRSYNTAX: 373 case LUA_ERRSYNTAX:
@@ -378,7 +383,7 @@ namespace lua {
378 383
379 void state::loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc) 384 void state::loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc)
380 { 385 {
381 switch(luaL_loadstring(cobj.get(), s)) { 386 switch(luaL_loadstring(cobj, s)) {
382 case 0: 387 case 0:
383 return; 388 return;
384 case LUA_ERRSYNTAX: 389 case LUA_ERRSYNTAX:
@@ -395,7 +400,7 @@ namespace lua {
395 checkstack(2); 400 checkstack(2);
396 pushvalue(index); 401 pushvalue(index);
397 insert(-2); 402 insert(-2);
398 lua_pushcfunction(cobj.get(), &safe_next_trampoline); 403 lua_pushcfunction(cobj, &safe_next_trampoline);
399 insert(-3); 404 insert(-3);
400 405
401 call(2, MULTRET, 0); 406 call(2, MULTRET, 0);
@@ -416,10 +421,10 @@ namespace lua {
416 } 421 }
417 422
418 void state::rawgetfield(int index, const char *k) throw(std::bad_alloc) 423 void state::rawgetfield(int index, const char *k) throw(std::bad_alloc)
419 { lua::rawgetfield(cobj.get(), index, k); } 424 { lua::rawgetfield(cobj, index, k); }
420 425
421 void state::rawsetfield(int index, const char *k) throw(std::bad_alloc) 426 void state::rawsetfield(int index, const char *k) throw(std::bad_alloc)
422 { lua::rawsetfield(cobj.get(), index, k); } 427 { lua::rawsetfield(cobj, index, k); }
423 428
424 bool state::safe_compare(lua_CFunction trampoline, int index1, int index2) 429 bool state::safe_compare(lua_CFunction trampoline, int index1, int index2)
425 { 430 {
@@ -432,7 +437,7 @@ namespace lua {
432 index2 = absindex(index2); 437 index2 = absindex(index2);
433 438
434 checkstack(3); 439 checkstack(3);
435 lua_pushcfunction(cobj.get(), trampoline); 440 lua_pushcfunction(cobj, trampoline);
436 pushvalue(index1); 441 pushvalue(index1);
437 pushvalue(index2); 442 pushvalue(index2);
438 call(2, 1, 0); 443 call(2, 1, 0);
@@ -456,7 +461,7 @@ namespace lua {
456 checkstack(2); 461 checkstack(2);
457 pushvalue(index); 462 pushvalue(index);
458 insert(-3); 463 insert(-3);
459 lua_pushcfunction(cobj.get(), (&safe_misc_trampoline<&lua_settable, 0>)); 464 lua_pushcfunction(cobj, (&safe_misc_trampoline<&lua_settable, 0>));
460 insert(-4); 465 insert(-4);
461 call(3, 0, 0); 466 call(3, 0, 0);
462 } 467 }
@@ -464,7 +469,7 @@ namespace lua {
464 std::string state::tostring(int index) throw(lua::not_string_error) 469 std::string state::tostring(int index) throw(lua::not_string_error)
465 { 470 {
466 size_t len; 471 size_t len;
467 const char *str = lua_tolstring(cobj.get(), index, &len); 472 const char *str = lua_tolstring(cobj, index, &len);
468 if(not str) 473 if(not str)
469 throw not_string_error(); 474 throw not_string_error();
470 return std::string(str, len); 475 return std::string(str, len);