]> git.mxchange.org Git - simgear.git/commitdiff
Add methods to get arguments passed to function from Nasal
authorThomas Geymayer <tomgey@gmail.com>
Thu, 29 Nov 2012 00:23:15 +0000 (01:23 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 29 Nov 2012 00:23:15 +0000 (01:23 +0100)
simgear/nasal/cppbind/Ghost.hxx
simgear/nasal/cppbind/cppbind_test.cxx

index 6fce4cbc74b1171172893f259f57887ded536a2c..6a8ccfb8bf4a85c16659ea033fa3aedd73efd379 100644 (file)
@@ -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<class T>
+    T get(size_t index, const T& def = T()) const
+    {
+      if( index >= argc )
+        return def;
+
+      return from_nasal<T>(c, args[index]);
+    }
+
+    template<class T>
+    T require(size_t index) const
+    {
+      if( index >= argc )
+        naRuntimeError(c, "Missing required arg #%d", index);
+
+      return from_nasal<T>(c, args[index]);
+    }
+
     naContext   c;
-    int         argc;
+    size_t      argc;
     naRef      *args;
   };
 
index 7543cdf83a40cb682537e194d8466dc29c96a05b..0f0caedf5d148ad5a6bef84425e8f9b6da561f12 100644 (file)
@@ -153,6 +153,14 @@ int main(int argc, char* argv[])
   obj.set("parents", parents);
   VERIFY( Ghost<BasePtr>::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<std::string>(0) == "test-arg" );
+  VERIFY( cc.get<std::string>(0) == "test-arg" );
+  VERIFY( cc.get<std::string>(1) == "" );
+
   // TODO actually do something with the ghosts...
 
   naFreeContext(c);