From 2f0dfc4d7463d24b7c36f81f47bb8167192fed88 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Thu, 29 Nov 2012 01:23:15 +0100 Subject: [PATCH] Add methods to get arguments passed to function from Nasal --- simgear/nasal/cppbind/Ghost.hxx | 22 ++++++++++++++++++++-- simgear/nasal/cppbind/cppbind_test.cxx | 8 ++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index 6fce4cbc..6a8ccfb8 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -223,14 +223,32 @@ namespace nasal */ struct CallContext { - CallContext(naContext c, int argc, naRef* args): + CallContext(naContext c, size_t argc, naRef* args): c(c), argc(argc), args(args) {} + template + T get(size_t index, const T& def = T()) const + { + if( index >= argc ) + return def; + + return from_nasal(c, args[index]); + } + + template + T require(size_t index) const + { + if( index >= argc ) + naRuntimeError(c, "Missing required arg #%d", index); + + return from_nasal(c, args[index]); + } + naContext c; - int argc; + size_t argc; naRef *args; }; diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index 7543cdf8..0f0caedf 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -153,6 +153,14 @@ int main(int argc, char* argv[]) obj.set("parents", parents); VERIFY( Ghost::fromNasal(c, obj.get_naRef()) == d3 ); + naRef args[] = { + to_nasal(c, std::string("test-arg")) + }; + CallContext cc(c, sizeof(args)/sizeof(args[0]), args); + VERIFY( cc.require(0) == "test-arg" ); + VERIFY( cc.get(0) == "test-arg" ); + VERIFY( cc.get(1) == "" ); + // TODO actually do something with the ghosts... naFreeContext(c); -- 2.39.5