# 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();
}