From 6db59c64aa8e08d5b4a7da4515669de06d44bbfa Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 27 Apr 2018 13:59:11 +0100 Subject: [PATCH] Subsystems: change naming scheme, add accessors Add some hopefully clearer accessors for the subsystem naming, and some comments documenting what is stored where. --- simgear/structure/subsystem_mgr.cxx | 25 ++++++++++++++-- simgear/structure/subsystem_mgr.hxx | 20 ++++++++++++- simgear/structure/subsystem_test.cxx | 43 ++++++++++++++++------------ 3 files changed, 67 insertions(+), 21 deletions(-) diff --git a/simgear/structure/subsystem_mgr.cxx b/simgear/structure/subsystem_mgr.cxx index 033bb244..3205b746 100644 --- a/simgear/structure/subsystem_mgr.cxx +++ b/simgear/structure/subsystem_mgr.cxx @@ -33,6 +33,7 @@ #include "SGSmplstat.hxx" const int SG_MAX_SUBSYSTEM_EXCEPTIONS = 4; +const char SUBSYSTEM_NAME_SEPARATOR = '.'; using std::string; using State = SGSubsystem::State; @@ -137,6 +138,26 @@ void SGSubsystem::set_name(const std::string &n) _name = n; } +std::string SGSubsystem::typeName() const +{ + auto pos = _name.find(SUBSYSTEM_NAME_SEPARATOR); + if (pos == std::string::npos) { + return _name; + } + + return _name.substr(0, pos); +} + +std::string SGSubsystem::instanceName() const +{ + auto pos = _name.find(SUBSYSTEM_NAME_SEPARATOR); + if (pos == std::string::npos) { + return {}; + } + + return _name.substr(pos+1); +} + void SGSubsystem::set_group(SGSubsystemGroup* group) { _group = group; @@ -815,7 +836,7 @@ SGSubsystemMgr::get_subsystem (const string &name) const SGSubsystem* SGSubsystemMgr::get_subsystem(const std::string &name, const std::string& instanceName) const { - return get_subsystem(name + "-" + instanceName); + return get_subsystem(name + SUBSYSTEM_NAME_SEPARATOR + instanceName); } @@ -954,7 +975,7 @@ SGSubsystemMgr::createInstance(const std::string& name, const std::string& insta throw sg_exception("SGSubsystemMgr::create: functor failed to create an instsance of " + name); } - const auto combinedName = name + "-" + instanceName; + const auto combinedName = name + SUBSYSTEM_NAME_SEPARATOR + instanceName; ref->set_name(combinedName); return ref; } diff --git a/simgear/structure/subsystem_mgr.hxx b/simgear/structure/subsystem_mgr.hxx index 6ce18dfc..0f3dd3b9 100644 --- a/simgear/structure/subsystem_mgr.hxx +++ b/simgear/structure/subsystem_mgr.hxx @@ -276,9 +276,23 @@ public: */ void stamp(const std::string& name); + /** + * composite name for this subsystem (type name & optional instance name) + */ std::string name() const { return _name; } + /** + * @brief the type (class)-specific part of the subsystem name. + */ + std::string typeName() const; + + /** + * @brief the instance part of the subsystem name. Empty if this + * subsystem is not instanced + */ + std::string instanceName() const; + virtual bool is_group() const { return false; } @@ -313,7 +327,11 @@ protected: void set_group(SGSubsystemGroup* group); - std::string _name; + /// composite name for the subsystem (type name and instance name if this + /// is an instanced subsystem. (Since this member was originally defined as + /// protected, not private, we can't rename it easily) + std::string _name; + bool _suspended = false; eventTimeVec timingInfo; diff --git a/simgear/structure/subsystem_test.cxx b/simgear/structure/subsystem_test.cxx index cd604272..dfc816d0 100644 --- a/simgear/structure/subsystem_test.cxx +++ b/simgear/structure/subsystem_test.cxx @@ -170,7 +170,9 @@ void testRegistrationAndCreation() SG_VERIFY(anotherSub); SG_CHECK_EQUAL(anotherSub->name(), AnotherSub::subsystemName()); SG_CHECK_EQUAL(anotherSub->name(), std::string("anothersub")); - + SG_CHECK_EQUAL(anotherSub->typeName(), std::string("anothersub")); + SG_CHECK_EQUAL(anotherSub->instanceName(), std::string()); + auto radio1 = manager->createInstance("nav1"); auto radio2 = manager->createInstance("nav2"); @@ -236,14 +238,16 @@ void testSubGrouping() auto radio1 = manager->createInstance("nav1"); auto radio2 = manager->createInstance("nav2"); - SG_CHECK_EQUAL(radio1->name(), std::string("fake-radio-nav1")); - SG_CHECK_EQUAL(radio2->name(), std::string("fake-radio-nav2")); - + SG_CHECK_EQUAL(radio1->name(), std::string("fake-radio.nav1")); + SG_CHECK_EQUAL(radio2->name(), std::string("fake-radio.nav2")); + SG_CHECK_EQUAL(radio1->typeName(), std::string("fake-radio")); + SG_CHECK_EQUAL(radio2->instanceName(), std::string("nav2")); + instruments->set_subsystem(radio1); instruments->set_subsystem(radio2); - SG_VERIFY(d->hasEvent("fake-radio-nav1-did-add")); - SG_VERIFY(d->hasEvent("fake-radio-nav1-will-add")); + SG_VERIFY(d->hasEvent("fake-radio.nav1-did-add")); + SG_VERIFY(d->hasEvent("fake-radio.nav1-will-add")); // lookup of the group should also work SG_CHECK_EQUAL(manager->get_subsystem(), instruments); @@ -257,24 +261,27 @@ void testSubGrouping() SG_VERIFY(d->hasEvent("instruments-will-init")); SG_VERIFY(d->hasEvent("instruments-did-init")); - SG_VERIFY(d->hasEvent("fake-radio-nav1-will-init")); - SG_VERIFY(d->hasEvent("fake-radio-nav2-did-init")); + SG_VERIFY(d->hasEvent("fake-radio.nav1-will-init")); + SG_VERIFY(d->hasEvent("fake-radio.nav2-did-init")); manager->update(0.5); SG_CHECK_EQUAL_EP(0.5, instruments->lastUpdateTime); SG_CHECK_EQUAL_EP(0.5, radio1->lastUpdateTime); SG_CHECK_EQUAL_EP(0.5, radio2->lastUpdateTime); - SG_CHECK_EQUAL(radio1, instruments->get_subsystem("fake-radio-nav1")); - SG_CHECK_EQUAL(radio2, instruments->get_subsystem("fake-radio-nav2")); + SG_CHECK_EQUAL(0, instruments->get_subsystem("fake-radio")); + + + SG_CHECK_EQUAL(radio1, instruments->get_subsystem("fake-radio.nav1")); + SG_CHECK_EQUAL(radio2, instruments->get_subsystem("fake-radio.nav2")); // type-safe lookup of instanced SG_CHECK_EQUAL(radio1, manager->get_subsystem("nav1")); SG_CHECK_EQUAL(radio2, manager->get_subsystem("nav2")); - bool ok = manager->remove("fake-radio-nav2"); + bool ok = manager->remove("fake-radio.nav2"); SG_VERIFY(ok); - SG_VERIFY(instruments->get_subsystem("fake-radio-nav2") == nullptr); + SG_VERIFY(instruments->get_subsystem("fake-radio.nav2") == nullptr); manager->update(1.0); SG_CHECK_EQUAL_EP(1.0, instruments->lastUpdateTime); @@ -286,7 +293,7 @@ void testSubGrouping() manager->unbind(); SG_VERIFY(d->hasEvent("instruments-will-unbind")); SG_VERIFY(d->hasEvent("instruments-did-unbind")); - SG_VERIFY(d->hasEvent("fake-radio-nav1-will-unbind")); + SG_VERIFY(d->hasEvent("fake-radio.nav1-will-unbind")); } @@ -326,11 +333,11 @@ void testIncrementalInit() // SG_VERIFY(d->hasEvent("instruments-did-init")); - SG_VERIFY(d->hasEvent("fake-radio-nav1-will-init")); - SG_VERIFY(d->hasEvent("fake-radio-nav1-did-init")); + SG_VERIFY(d->hasEvent("fake-radio.nav1-will-init")); + SG_VERIFY(d->hasEvent("fake-radio.nav1-did-init")); - SG_VERIFY(d->hasEvent("fake-radio-nav2-will-init")); - SG_VERIFY(d->hasEvent("fake-radio-nav2-did-init")); + SG_VERIFY(d->hasEvent("fake-radio.nav2-will-init")); + SG_VERIFY(d->hasEvent("fake-radio.nav2-did-init")); @@ -364,7 +371,7 @@ void testSuspendResume() SG_VERIFY(d->hasEvent("anothersub-will-suspend")); SG_VERIFY(d->hasEvent("anothersub-did-suspend")); - SG_VERIFY(!d->hasEvent("radio1-will-suspend")); + SG_VERIFY(!d->hasEvent("fake-radio.nav1-will-suspend")); manager->update(0.5);