From 368120c479441732f7076768e1598442bccd1585 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 25 Apr 2018 21:34:17 +0100 Subject: [PATCH] Track a root property on subsystem-manager --- simgear/structure/subsystem_mgr.cxx | 12 ++++++++ simgear/structure/subsystem_mgr.hxx | 16 ++++++++++ simgear/structure/subsystem_test.cxx | 44 +++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/simgear/structure/subsystem_mgr.cxx b/simgear/structure/subsystem_mgr.cxx index ac46eeab..52ca6fda 100644 --- a/simgear/structure/subsystem_mgr.cxx +++ b/simgear/structure/subsystem_mgr.cxx @@ -28,6 +28,7 @@ #include "exception.hxx" #include "subsystem_mgr.hxx" +#include #include #include "SGSmplstat.hxx" @@ -966,5 +967,16 @@ void SGSubsystemMgr::notifyDelegatesDidChange(SGSubsystem* sub, State state) { d->didChange(sub, state); }); } +void SGSubsystemMgr::set_root_node(SGPropertyNode_ptr node) +{ + _rootNode = node; +} + +SGPropertyNode_ptr +SGSubsystemMgr::root_node() const +{ + return _rootNode; +} + // end of subsystem_mgr.cxx diff --git a/simgear/structure/subsystem_mgr.hxx b/simgear/structure/subsystem_mgr.hxx index 26674a02..63257dc9 100644 --- a/simgear/structure/subsystem_mgr.hxx +++ b/simgear/structure/subsystem_mgr.hxx @@ -33,6 +33,7 @@ #include #include #include +#include class TimingInfo { @@ -433,6 +434,8 @@ typedef SGSharedPtr SGSubsystemGroupRef; class SGSubsystemMgr : public SGSubsystem { public: + SGSubsystemMgr(const SGSubsystemMgr&) = delete; + SGSubsystemMgr& operator=(const SGSubsystemMgr&) = delete; /** * Types of subsystem groups. @@ -483,6 +486,18 @@ public: void reportTiming(); void setReportTimingCb(void* userData,SGSubsystemTimingCb cb) {reportTimingCb = cb;reportTimingUserData = userData;} + /** + * @brief set the root property node for this subsystem manager + * subsystems can retrieve this value during init/bind (or at any time) + * to base their own properties trees off of. + */ + void set_root_node(SGPropertyNode_ptr node); + + /** + * @brief retrieve the root property node for this subsystem manager + */ + SGPropertyNode_ptr root_node() const; + template T* get_subsystem() const { @@ -631,6 +646,7 @@ private: std::vector _groups; unsigned int _initPosition = 0; bool _destructorActive = false; + SGPropertyNode_ptr _rootNode; // non-owning reference, this is to accelerate lookup // by name which otherwise needs a full walk of the entire tree diff --git a/simgear/structure/subsystem_test.cxx b/simgear/structure/subsystem_test.cxx index cb07e902..90bcab5f 100644 --- a/simgear/structure/subsystem_test.cxx +++ b/simgear/structure/subsystem_test.cxx @@ -7,6 +7,7 @@ #include #include #include +#include using std::string; using std::cout; @@ -26,6 +27,12 @@ public: wasInited = true; } + void bind() override + { + auto node = get_manager()->root_node(); + node->setIntValue("mysub/foo", 42); + } + void update(double dt) override { @@ -44,6 +51,12 @@ public: } + void bind() override + { + auto node = get_manager()->root_node(); + node->setIntValue("anothersub/bar", 172); + } + void update(double dt) override { lastUpdateTime = dt; @@ -297,10 +310,6 @@ void testIncrementalInit() break; } - for (auto ev : d->events) { - std::cerr << "ev:" << ev.nameForEvent() << std::endl; - } - SG_VERIFY(mySub->wasInited); SG_VERIFY(d->hasEvent("mysub-will-init")); @@ -392,6 +401,32 @@ void testSuspendResume() SG_CHECK_EQUAL_EP(5.0, radio2->lastUpdateTime); } +void testPropertyRoot() +{ + SGSharedPtr manager = new SGSubsystemMgr; + SGPropertyNode_ptr props(new SGPropertyNode); + manager->set_root_node(props); + + auto d = new RecorderDelegate; + manager->addDelegate(d); + + manager->add(); + auto anotherSub = manager->add(); + auto instruments = manager->add(); + + auto radio1 = manager->createInstance("nav1"); + auto radio2 = manager->createInstance("nav2"); + + instruments->set_subsystem(radio1); + instruments->set_subsystem(radio2); + + manager->bind(); + manager->init(); + + SG_CHECK_EQUAL(props->getIntValue("mysub/foo"), 42); + SG_CHECK_EQUAL(props->getIntValue("anothersub/bar"), 172); +} + /////////////////////////////////////////////////////////////////////////////// @@ -402,6 +437,7 @@ int main(int argc, char* argv[]) testSubGrouping(); testIncrementalInit(); testSuspendResume(); + testPropertyRoot(); cout << __FILE__ << ": All tests passed" << endl; return EXIT_SUCCESS;