FlightGear next
NasalCondition.cxx
Go to the documentation of this file.
1// SPDX-FileComment: expose SGCondition and SGBinding to Nasal
2// SPDX-License-Identifier: GPL-2.0-or-later
3// Copyright (C) 2012 James Turner <james@flightgear.org>
4
5#include "config.h"
6
7#include "NasalCondition.hxx"
8#include "NasalSys.hxx"
9#include <Main/globals.hxx>
10
11#include <simgear/nasal/cppbind/Ghost.hxx>
12#include <simgear/nasal/cppbind/NasalHash.hxx>
13#include <simgear/props/condition.hxx>
14
15using NasalBindingRef = SGSharedPtr<NasalBinding>;
16
17typedef nasal::Ghost<SGConditionRef> NasalCondition;
18
19void NasalBinding::innerFire() const
20{
21 auto nas = globals->get_subsystem<FGNasalSys>();
22 m_callback(nas->wrappedPropsNode(_arg));
23}
24
25//------------------------------------------------------------------------------
26static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
27{
28 SGPropertyNode* node = argc > 0
29 ? ghostToPropNode(args[0])
30 : NULL;
31 SGPropertyNode* root = argc > 1
32 ? ghostToPropNode(args[1])
33 : globals->get_props();
34
35 if( !node || !root )
36 naRuntimeError(c, "createCondition: invalid argument(s)");
37
38 try
39 {
40 return nasal::to_nasal(c, sgReadCondition(root, node));
41 }
42 catch(std::exception& ex)
43 {
44 naRuntimeError(c, "createCondition: %s", ex.what());
45 }
46
47 return naNil();
48}
49
50//------------------------------------------------------------------------------
51naRef initNasalCondition(naRef globals, naContext c)
52{
53 nasal::Ghost<SGConditionRef>::init("Condition")
54 .method("test", &SGCondition::test);
55
56 nasal::Hash(globals, c).set("_createCondition", f_createCondition);
57
58
59 return naNil();
60}
static naRef f_createCondition(naContext c, naRef me, int argc, naRef *args)
naRef initNasalCondition(naRef globals, naContext c)
nasal::Ghost< SGConditionRef > NasalCondition
SGSharedPtr< NasalBinding > NasalBindingRef
FGGlobals * globals
Definition globals.cxx:142
SGPropertyNode * ghostToPropNode(naRef ref)