aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Luamm.cc
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-25 13:42:39 (GMT)
committerPaul Tagliamonte <paultag@fluxbox.org>2012-04-07 02:11:33 (GMT)
commit9a9264e580d5368c4942adf6a3785c830a703984 (patch)
tree6ba20ef9fba236d2b81bb8b39f2c79786f9f5018 /src/FbTk/Luamm.cc
parent7742efa7060f4468ab9ff0ea9dc3e6d9539f3622 (diff)
downloadfluxbox_paul-9a9264e580d5368c4942adf6a3785c830a703984.zip
fluxbox_paul-9a9264e580d5368c4942adf6a3785c830a703984.tar.bz2
Add checkudata and checkargno functions to lua::state
These can be used by functions to check the saneness of arguments. They throw an exception on error. In the future, I might add more informative error messages.
Diffstat (limited to 'src/FbTk/Luamm.cc')
-rw-r--r--src/FbTk/Luamm.cc31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc
index 1ef751f..80d0926 100644
--- a/src/FbTk/Luamm.cc
+++ b/src/FbTk/Luamm.cc
@@ -24,6 +24,8 @@
24 24
25#include "Luamm.hh" 25#include "Luamm.hh"
26 26
27#include <sstream>
28
27namespace lua { 29namespace lua {
28 namespace { 30 namespace {
29 // keys for storing values in lua registry 31 // keys for storing values in lua registry
@@ -274,6 +276,35 @@ namespace lua {
274 throw lua::exception(this); 276 throw lua::exception(this);
275 } 277 }
276 278
279 void state::checkargno(int argno) throw(lua::check_error)
280 {
281 if(gettop() != argno) {
282 std::ostringstream str;
283 str << "Wrong number of arguments: expected " << argno << ", got " << gettop();
284 throw lua::check_error(str.str());
285 }
286 }
287
288 void *state::checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc)
289 {
290 checkstack(2);
291 stack_sentry s(*this);
292
293 void *p = touserdata(narg);
294 if(p != NULL) {
295 if(getmetatable(narg)) {
296 rawgetfield(REGISTRYINDEX, tname);
297 if(rawequal(-1, -2))
298 return p;
299 pop(2);
300 }
301 }
302 std::ostringstream str;
303 str << "Invalid argument #" << narg << ": expected " << type_name(TUSERDATA)
304 << ", got " << type_name(type(narg));
305 throw lua::check_error(str.str());
306 }
307
277 void state::checkstack(int extra) throw(std::bad_alloc) 308 void state::checkstack(int extra) throw(std::bad_alloc)
278 { 309 {
279 if(not lua_checkstack(cobj, extra)) 310 if(not lua_checkstack(cobj, extra))