From b192f2dd79fa5e0a760fe6d53f429cb390b3877f Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 29 Jul 2011 18:07:18 +0200 Subject: Add checkstring() to lua::state --- src/FbTk/Luamm.cc | 23 +++++++++++++++++++---- src/FbTk/Luamm.hh | 2 ++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 80d0926..8824b06 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc @@ -285,6 +285,16 @@ namespace lua { } } + std::string state::checkstring(int narg) throw(lua::check_error, std::bad_alloc) + { + try { + return tostring(narg); + } + catch(lua::not_string_error &e) { + throw_check_error(narg, TSTRING); + } + } + void *state::checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc) { checkstack(2); @@ -299,10 +309,7 @@ namespace lua { pop(2); } } - std::ostringstream str; - str << "Invalid argument #" << narg << ": expected " << type_name(TUSERDATA) - << ", got " << type_name(type(narg)); - throw lua::check_error(str.str()); + throw_check_error(narg, TUSERDATA); } void state::checkstack(int extra) throw(std::bad_alloc) @@ -478,6 +485,14 @@ namespace lua { call(3, 0, 0); } + void state::throw_check_error(int narg, Type expected) throw(lua::check_error) + { + std::ostringstream str; + str << "Invalid argument #" << narg << ": expected " << type_name(expected) + << ", got " << type_name(type(narg)); + throw lua::check_error(str.str()); + } + std::string state::tostring(int index) throw(lua::not_string_error) { size_t len; diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index b75fb12..01fb8b1 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh @@ -161,6 +161,7 @@ namespace lua { bool safe_compare(lua_CFunction trampoline, int index1, int index2); void do_pushclosure(int n); + void throw_check_error(int narg, Type expected) throw(lua::check_error) __attribute__((noreturn)); /** * The pointed-to value is true if this object still exists. We need this because the @@ -273,6 +274,7 @@ namespace lua { // call() is a protected mode call, we don't allow unprotected calls void call(int nargs, int nresults, int errfunc = 0); void checkargno(int argno) throw(lua::check_error); + std::string checkstring(int narg) throw(lua::check_error, std::bad_alloc); void *checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc); template T *checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc) { return static_cast(checkudata(narg, tname)); } -- cgit v0.11.2