| 1 | // NasalSys.hxx - |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | // SPDX-FileCopyrightText: Copyright (C) 2013 James Turner |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <simgear/debug/BufferedLogCallback.hxx> |
| 8 | #include <simgear/nasal/nasal.h> |
| 9 | #include <simgear/props/props.hxx> |
| 10 | #include <simgear/threads/SGQueue.hxx> |
| 11 | #include <simgear/xml/easyxml.hxx> |
| 12 | |
| 13 | #include "NasalModelData.hxx" |
| 14 | |
| 15 | // forward decls |
| 16 | class FGNasalSys; |
| 17 | class TimerObj; ///< persistent timer created by maketimer |
| 18 | class FGNasalModuleListener; |
| 19 | class NasalCommand; |
| 20 | |
| 21 | /** |
| 22 | @breif wrapper for naEqual which recursively checks vec/hash equality |
| 23 | Probably not very performant. |
| 24 | */ |
| 25 | int nasalStructEqual(naContext ctx, naRef a, naRef b); |
| 26 | |
| 27 | class FGNasalListener : public SGPropertyChangeListener { |
| 28 | public: |
| 29 | FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal, |
| 30 | int key, int id, int init, int type); |
| 31 | |
| 32 | virtual ~FGNasalListener(); |
| 33 | virtual void valueChanged(SGPropertyNode* node); |
| 34 | virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child); |
| 35 | virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child); |
| 36 | |
| 37 | private: |
| 38 | bool changed(SGPropertyNode* node); |
| 39 | void call(SGPropertyNode* which, naRef mode); |
| 40 | |
| 41 | friend class FGNasalSys; |
| 42 | SGPropertyNode_ptr _node; |
| 43 | naRef _code; |
| 44 | int _gcKey; |
| 45 | int _id; |
| 46 | FGNasalSys* _nas; |
| 47 | int _init; |
| 48 | int _type; |
| 49 | unsigned int _active; |
| 50 | bool _dead; |
| 51 | long _last_int; |
| 52 | double _last_float; |
| 53 | std::string _last_string; |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | class NasalXMLVisitor : public XMLVisitor { |
| 58 | public: |
| 59 | NasalXMLVisitor(naContext c, int argc, naRef* args); |
| 60 | virtual ~NasalXMLVisitor() { naFreeContext(_c); } |
| 61 | |
| 62 | virtual void startElement(const char* tag, const XMLAttributes& a); |
| 63 | virtual void endElement(const char* tag); |
| 64 | virtual void data(const char* str, int len); |
| 65 | virtual void pi(const char* target, const char* data); |
| 66 | |
| 67 | private: |
| 68 | void call(naRef func, int num, naRef a = naNil(), naRef b = naNil()); |
| 69 | naRef make_string(const char* s, int n = -1); |
| 70 | |
| 71 | naContext _c; |
| 72 | naRef _start_element, _end_element, _data, _pi; |
| 73 | }; |
| 74 | |
| 75 | // |
| 76 | // See the implementation of the settimer() extension function for |
| 77 | // more notes. |
| 78 | // |
| 79 | struct NasalTimer |
| 80 | { |
| 81 | NasalTimer(naRef handler, FGNasalSys* sys); |
| 82 | |
| 83 | void timerExpired(); |
| 84 | ~NasalTimer(); |
| 85 | |
| 86 | naRef handler; |
| 87 | int gcKey = 0; |
| 88 | FGNasalSys* nasal = nullptr; |
| 89 | }; |
| 90 | |
| 91 | // declare the interface to the unit-testing module |
| 92 | naRef initNasalUnitTestCppUnit(naRef globals, naContext c); |
| 93 | naRef initNasalUnitTestInSim(naRef globals, naContext c); |
| 94 | |
| 95 | |
| 96 | class NasalSysPrivate |
| 97 | { |
| 98 | public: |
| 99 | ~NasalSysPrivate(); |
| 100 | |
| 101 | //friend class FGNasalScript; |
| 102 | // friend class FGNasalListener; |
| 103 | // friend class FGNasalModuleListener; |
| 104 | |
| 105 | SGLockedQueue<SGSharedPtr<FGNasalModelData>> _loadList; |
| 106 | SGLockedQueue<SGSharedPtr<FGNasalModelData>> _unloadList; |
| 107 | // Delay removing items of the _loadList to ensure the are already attached |
| 108 | // to the scene graph (eg. enables to retrieve world position in load |
| 109 | // callback). |
| 110 | bool _delay_load; |
| 111 | |
| 112 | // Listener |
| 113 | std::map<int, FGNasalListener*> _listener; |
| 114 | std::vector<FGNasalListener*> _dead_listener; |
| 115 | |
| 116 | std::vector<FGNasalModuleListener*> _moduleListeners; |
| 117 | |
| 118 | static int _listenerId; |
| 119 | |
| 120 | bool _inited = false; |
| 121 | naContext _context = nullptr; |
| 122 | naRef _globals, |
| 123 | _string; |
| 124 | |
| 125 | SGPropertyNode_ptr _cmdArg; |
| 126 | |
| 127 | std::unique_ptr<simgear::BufferedLogCallback> _log; |
| 128 | |
| 129 | typedef std::map<std::string, NasalCommand*> NasalCommandDict; |
| 130 | NasalCommandDict _commands; |
| 131 | |
| 132 | naRef _wrappedNodeFunc; |
| 133 | |
| 134 | // track NasalTimer instances (created via settimer() call) - |
| 135 | // this allows us to clean these up on shutdown |
| 136 | std::vector<NasalTimer*> _nasalTimers; |
| 137 | |
| 138 | |
| 139 | // track persistent timers. These are owned from the Nasal side, so we |
| 140 | // only track a non-owning reference here. |
| 141 | std::vector<TimerObj*> _persistentTimers; |
| 142 | }; |