]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCondition.cxx
Reset: Nasal can be shutdown.
[flightgear.git] / src / Scripting / NasalCondition.cxx
1 // NasalCondition -- expose SGCondition to Nasal
2 //
3 // Written by James Turner, started 2012.
4 //
5 // Copyright (C) 2012 James Turner
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <string.h>
26
27 #include "NasalCondition.hxx"
28 #include "NasalSys.hxx"
29 #include <Main/globals.hxx>
30
31 #include <simgear/sg_inlines.h>
32 #include <simgear/props/condition.hxx>
33
34 static void conditionGhostDestroy(void* g);
35
36 naGhostType ConditionGhostType = { conditionGhostDestroy, "condition" };
37
38 static void hashset(naContext c, naRef hash, const char* key, naRef val)
39 {
40   naRef s = naNewString(c);
41   naStr_fromdata(s, (char*)key, strlen(key));
42   naHash_set(hash, s, val);
43 }
44
45 SGCondition* conditionGhost(naRef r)
46 {
47   if ((naGhost_type(r) == &ConditionGhostType))
48   {
49     return (SGCondition*) naGhost_ptr(r);
50   }
51   
52   return 0;
53 }
54
55 static void conditionGhostDestroy(void* g)
56 {
57   SGCondition* cond = (SGCondition*)g;
58   if (!SGCondition::put(cond)) // unref
59     delete cond;
60 }
61
62 static naRef conditionPrototype;
63
64 naRef ghostForCondition(naContext c, const SGCondition* cond)
65 {
66   if (!cond) {
67     return naNil();
68   }
69   
70   SGCondition::get(cond); // take a ref
71   return naNewGhost(c, &ConditionGhostType, (void*) cond);
72 }
73
74 static naRef f_condition_test(naContext c, naRef me, int argc, naRef* args)
75 {
76   SGCondition* cond = conditionGhost(me);
77   if (!cond) {
78     naRuntimeError(c, "condition.test called on non-condition object");
79   }
80   
81   return naNum(cond->test());
82 }
83
84 static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
85 {
86   SGPropertyNode* node = ghostToPropNode(args[0]);
87   SGPropertyNode* root = globals->get_props();
88   if (argc > 1) {
89     root = ghostToPropNode(args[1]);
90   }
91   
92   SGCondition* cond = sgReadCondition(root, node);
93   return ghostForCondition(c, cond);
94 }
95
96 // Table of extension functions.  Terminate with zeros.
97 static struct { const char* name; naCFunction func; } funcs[] = {
98   { "_createCondition", f_createCondition },
99   { 0, 0 }
100 };
101
102
103 naRef initNasalCondition(naRef globals, naContext c)
104 {
105   conditionPrototype = naNewHash(c);
106   naSave(c, conditionPrototype);
107   
108   hashset(c, conditionPrototype, "test", naNewFunc(c, naNewCCode(c, f_condition_test)));  
109   for(int i=0; funcs[i].name; i++) {
110     hashset(c, globals, funcs[i].name,
111             naNewFunc(c, naNewCCode(c, funcs[i].func)));
112   }
113   
114   return naNil();
115 }