Subsystems: change naming scheme, add accessors

Add some hopefully clearer accessors for the subsystem naming, and some
comments documenting what is stored where.
This commit is contained in:
James Turner
2018-04-27 13:59:11 +01:00
parent 888d7fb262
commit 6db59c64aa
3 changed files with 67 additions and 21 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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<FakeRadioSub>("nav1");
auto radio2 = manager->createInstance<FakeRadioSub>("nav2");
@@ -236,14 +238,16 @@ void testSubGrouping()
auto radio1 = manager->createInstance<FakeRadioSub>("nav1");
auto radio2 = manager->createInstance<FakeRadioSub>("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<InstrumentGroup>(), 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<FakeRadioSub>("nav1"));
SG_CHECK_EQUAL(radio2, manager->get_subsystem<FakeRadioSub>("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);