]> git.mxchange.org Git - flightgear.git/commitdiff
Nasal: expose SGCondition using nasal::Ghost and improved error checking.
authorThomas Geymayer <tomgey@gmail.com>
Sun, 22 Jun 2014 13:37:48 +0000 (15:37 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Sun, 22 Jun 2014 13:37:48 +0000 (15:37 +0200)
src/Scripting/NasalCondition.cxx
src/Scripting/NasalCondition.hxx
src/Scripting/NasalSys.hxx

index f47a52a51b84b0cc6c281577a562dc3cab6b9169..a28842b45c316c78658e5f8ca83cd763cedf46f2 100644 (file)
 #  include "config.h"
 #endif
 
-#include <string.h>
-
 #include "NasalCondition.hxx"
 #include "NasalSys.hxx"
 #include <Main/globals.hxx>
 
-#include <simgear/sg_inlines.h>
+#include <simgear/nasal/cppbind/Ghost.hxx>
+#include <simgear/nasal/cppbind/NasalHash.hxx>
 #include <simgear/props/condition.hxx>
 
-static void conditionGhostDestroy(void* g);
-
-naGhostType ConditionGhostType = { conditionGhostDestroy, "condition" };
+typedef nasal::Ghost<SGConditionRef> NasalCondition;
 
-static void hashset(naContext c, naRef hash, const char* key, naRef val)
-{
-  naRef s = naNewString(c);
-  naStr_fromdata(s, (char*)key, strlen(key));
-  naHash_set(hash, s, val);
-}
-
-SGCondition* conditionGhost(naRef r)
-{
-  if ((naGhost_type(r) == &ConditionGhostType))
-  {
-    return (SGCondition*) naGhost_ptr(r);
-  }
-  
-  return 0;
-}
-
-static void conditionGhostDestroy(void* g)
+//------------------------------------------------------------------------------
+static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
 {
-  SGCondition* cond = (SGCondition*)g;
-  if (!SGCondition::put(cond)) // unref
-    delete cond;
-}
+  SGPropertyNode* node = argc > 0
+                       ? ghostToPropNode(args[0])
+                       : NULL;
+  SGPropertyNode* root = argc > 1
+                       ? ghostToPropNode(args[1])
+                       : globals->get_props();
 
-static naRef conditionPrototype;
+  if( !node || !root )
+    naRuntimeError(c, "createCondition: invalid argument(s)");
 
-naRef ghostForCondition(naContext c, const SGCondition* cond)
-{
-  if (!cond) {
-    return naNil();
+  try
+  {
+    return nasal::to_nasal(c, sgReadCondition(root, node));
   }
-  
-  SGCondition::get(cond); // take a ref
-  return naNewGhost(c, &ConditionGhostType, (void*) cond);
-}
-
-static naRef f_condition_test(naContext c, naRef me, int argc, naRef* args)
-{
-  SGCondition* cond = conditionGhost(me);
-  if (!cond) {
-    naRuntimeError(c, "condition.test called on non-condition object");
+  catch(std::exception& ex)
+  {
+    naRuntimeError(c, "createCondition: %s", ex.what());
   }
-  
-  return naNum(cond->test());
 }
 
-static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
+//------------------------------------------------------------------------------
+naRef initNasalCondition(naRef globals, naContext c)
 {
-  SGPropertyNode* node = ghostToPropNode(args[0]);
-  SGPropertyNode* root = globals->get_props();
-  if (argc > 1) {
-    root = ghostToPropNode(args[1]);
-  }
-  
-  SGCondition* cond = sgReadCondition(root, node);
-  return ghostForCondition(c, cond);
-}
-
-// Table of extension functions.  Terminate with zeros.
-static struct { const char* name; naCFunction func; } funcs[] = {
-  { "_createCondition", f_createCondition },
-  { 0, 0 }
-};
+  nasal::Ghost<SGConditionRef>::init("Condition")
+    .method("test", &SGCondition::test);
 
+  nasal::Hash(globals, c).set("_createCondition", f_createCondition);
 
-naRef initNasalCondition(naRef globals, naContext c)
-{
-  conditionPrototype = naNewHash(c);
-  naSave(c, conditionPrototype);
-  
-  hashset(c, conditionPrototype, "test", naNewFunc(c, naNewCCode(c, f_condition_test)));  
-  for(int i=0; funcs[i].name; i++) {
-    hashset(c, globals, funcs[i].name,
-            naNewFunc(c, naNewCCode(c, funcs[i].func)));
-  }
-  
   return naNil();
 }
index c16b2b5d17e000c4bb3fefb7bfd7df27de93e304..6f4ffe2bd2838cfa71729d38a0ea700ade280b8e 100644 (file)
 
 #include <simgear/nasal/nasal.h>
 
-// forward decls
-class SGCondition;
-
-naRef ghostForCondition(naContext c, const SGCondition* cond);
-
 naRef initNasalCondition(naRef globals, naContext c);
 
 #endif // of SCRIPTING_NASAL_CONDITION_HXX
-
index bbb2ace909f56497574933f3a6ab0347a94dc883..e487a48eb38ffeb56bffb884d607a84ce8836e15 100644 (file)
@@ -29,7 +29,6 @@ class FGNasalModuleListener;
 namespace simgear { class BufferedLogCallback; }
 
 SGPropertyNode* ghostToPropNode(naRef ref);
-SGCondition* conditionGhost(naRef r);
 
 class FGNasalSys : public SGSubsystem
 {