From 7a9411dde66117e9d6e1019e0dd9209ab18e78ed Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 19 May 2011 13:25:32 +0200 Subject: c++ lua binding: get rid of deleted functions and rvalue references --- src/FbTk/Luamm.cc | 15 ++++++++++---- src/FbTk/Luamm.hh | 58 ++++++++++++++----------------------------------------- 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 81a8fb3..d17096f 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc @@ -108,10 +108,6 @@ namespace lua { /* * This function is called when lua encounters an error outside of any protected * environment - * Throwing the exception through lua code appears to work, even if it was compiled - * without -fexceptions. If it turns out, it fails in some conditions, it could be - * replaced with some longjmp() magic. But that shouldn't be necessary, as this function - * will not be called under normal conditions (we execute everything in protected mode). */ int panic_throw(lua_State *l) { @@ -191,6 +187,17 @@ namespace lua { L->pop(1); } + exception::exception(const exception &other) + : std::runtime_error(other), L(other.L) + { + L->checkstack(2); + + L->rawgetfield(REGISTRYINDEX, lua_exception_namespace); + L->rawgeti(-1, other.key); + key = L->ref(-2); + L->pop(1); + } + exception::~exception() throw() { if(not L) diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 5c8d382..e173be9 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh @@ -26,8 +26,9 @@ #include #include -#include #include +#include +#include #include "Slot.hh" @@ -81,79 +82,50 @@ namespace lua { * pushed into the same state it was generated in. */ class exception: public std::runtime_error { - /* - * We only allow moving, to avoid complications with multiple references. It shouldn't be - * difficult to modify this to work with copying, if that proves unavoidable. - */ state *L; int key; static std::string get_error_msg(state *L); - exception(const exception &) = delete; - const exception& operator=(const exception &) = delete; - - public: - exception(exception &&other) - : std::runtime_error(std::move(other)), L(other.L), key(other.key) - { other.L = NULL; } + exception& operator=(const exception &other); // not implemented + public: explicit exception(state *l); + exception(const exception &other); virtual ~exception() throw(); void push_lua_error(state *l); }; class not_string_error: public std::runtime_error { - public: - not_string_error() - : std::runtime_error("Cannot convert value to a string") - {} + public: + not_string_error() + : std::runtime_error("Cannot convert value to a string") + {} }; // the name says it all class syntax_error: public lua::exception { - syntax_error(const syntax_error &) = delete; - const syntax_error& operator=(const syntax_error &) = delete; - - public: + public: syntax_error(state *L) : lua::exception(L) {} - - syntax_error(syntax_error &&other) - : lua::exception(std::move(other)) - {} }; // loadfile() encountered an error while opening/reading the file class file_error: public lua::exception { - file_error(const file_error &) = delete; - const file_error& operator=(const file_error &) = delete; - - public: + public: file_error(state *L) : lua::exception(L) {} - - file_error(file_error &&other) - : lua::exception(std::move(other)) - {} }; // double fault, lua encountered an error while running the error handler function class errfunc_error: public lua::exception { - errfunc_error(const errfunc_error &) = delete; - const errfunc_error& operator=(const errfunc_error &) = delete; - - public: + public: errfunc_error(state *L) : lua::exception(L) {} - - errfunc_error(errfunc_error &&other) - : lua::exception(std::move(other)) - {} }; // a fancy wrapper around lua_State @@ -313,13 +285,11 @@ namespace lua { * since we have exception.what() for that, putting the message on the stack is not * necessary. */ - class stack_sentry { + class stack_sentry: private FbTk::NotCopyable { state *L; int n; - stack_sentry(const stack_sentry &) = delete; - const stack_sentry& operator=(const stack_sentry &) = delete; - public: + public: explicit stack_sentry(state &l, int n_ = 0) throw() : L(&l), n(l.gettop()+n_) { assert(n >= 0); } -- cgit v0.11.2