From: Thomas Geymayer Date: Tue, 5 Mar 2013 16:27:21 +0000 (+0100) Subject: cppbind: Add some methods to nasal::CallContext X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bc96ac85f43c5360f6c34ffd66d1faef8d6c9f52;p=simgear.git cppbind: Add some methods to nasal::CallContext --- diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index c1d2dae2..4c185a2c 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -121,6 +121,16 @@ namespace nasal }; BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type) + + template + struct reduced_is_same: + public boost::is_same< + typename boost::remove_cv< + typename boost::remove_reference::type + >::type, + T2 + > + {}; } /** @@ -134,6 +144,48 @@ namespace nasal args(args) {} + bool isNumeric(size_t index) const + { + return (index < argc && naIsNum(args[index])); + } + + bool isString(size_t index) const + { + return (index < argc && naIsString(args[index])); + } + + bool isHash(size_t index) const + { + return (index < argc && naIsHash(args[index])); + } + + bool isVector(size_t index) const + { + return (index < argc && naIsVector(args[index])); + } + + bool isGhost(size_t index) const + { + return (index < argc && naIsGhost(args[index])); + } + + void popFront(size_t num = 1) + { + if( argc < num ) + return; + + args += num; + argc -= num; + } + + void popBack(size_t num = 1) + { + if( argc < num ) + return; + + argc -= num; + } + /** * Get the argument with given index if it exists. Otherwise returns the * passed default value. @@ -149,7 +201,7 @@ namespace nasal if( index >= argc ) return def; - return (*from_nasal_ptr::get())(c, args[index]); + return from_nasal(args[index]); } /** @@ -163,7 +215,7 @@ namespace nasal if( index >= argc ) naRuntimeError(c, "Missing required arg #%d", index); - return (*from_nasal_ptr::get())(c, args[index]); + return from_nasal(args[index]); } template @@ -172,6 +224,13 @@ namespace nasal return nasal::to_nasal(c, arg); } + template + typename from_nasal_ptr::return_type + from_nasal(naRef ref) const + { + return (*from_nasal_ptr::get())(c, ref); + } + naContext c; size_t argc; naRef *args; @@ -794,7 +853,7 @@ namespace nasal template static typename boost::disable_if< - boost::is_same, + internal::reduced_is_same, typename from_nasal_ptr::return_type >::type arg_from_nasal(const CallContext& ctx, size_t index) @@ -808,11 +867,14 @@ namespace nasal template static typename boost::enable_if< - boost::is_same, + internal::reduced_is_same, typename from_nasal_ptr::return_type >::type arg_from_nasal(const CallContext& ctx, size_t) { + // Either const CallContext& or CallContext, non-const reference + // does not make sense. + BOOST_STATIC_ASSERT( (!boost::is_same::value) ); return ctx; }; diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index e6f142db..23e5e33f 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -227,13 +227,27 @@ int main(int argc, char* argv[]) // Test nasal::CallContext //---------------------------------------------------------------------------- + + int int_vec[] = {1,2,3}; + std::map map; naRef args[] = { - to_nasal(c, std::string("test-arg")) + to_nasal(c, std::string("test-arg")), + to_nasal(c, 4), + to_nasal(c, int_vec), + to_nasal(c, map) }; CallContext cc(c, sizeof(args)/sizeof(args[0]), args); VERIFY( cc.requireArg(0) == "test-arg" ); VERIFY( cc.getArg(0) == "test-arg" ); - VERIFY( cc.getArg(1) == "" ); + VERIFY( cc.getArg(10) == "" ); + VERIFY( cc.isString(0) ); + VERIFY( !cc.isNumeric(0) ); + VERIFY( !cc.isVector(0) ); + VERIFY( !cc.isHash(0) ); + VERIFY( !cc.isGhost(0) ); + VERIFY( cc.isNumeric(1) ); + VERIFY( cc.isVector(2) ); + VERIFY( cc.isHash(3) ); naRef args_vec = nasal::to_nasal(c, args); VERIFY( naIsVector(args_vec) );