SGSubsystem classes: Renaming of the subsystem ID variables and functions.

This commit is contained in:
Edward d'Auvergne 2018-05-04 14:16:42 +02:00
parent 10662e4ca5
commit d7323f5f19
5 changed files with 78 additions and 73 deletions

View File

@ -60,7 +60,7 @@ public:
void update(double dt) override;
// Subsystem identification.
static const char* subsystemName() { return "sound"; }
static const char* staticSubsystemClassId() { return "sound"; }
void stop();

View File

@ -47,7 +47,6 @@ SGEventMgr::SGEventMgr() :
_inited(false),
_shutdown(false)
{
_name = "EventMgr";
}
SGEventMgr::~SGEventMgr()

View File

@ -138,28 +138,28 @@ void SGSubsystem::stamp(const string& name)
void SGSubsystem::set_name(const std::string &n)
{
assert(_name.empty());
_name = n;
assert(_subsystemId.empty());
_subsystemId = n;
}
std::string SGSubsystem::typeName() const
std::string SGSubsystem::subsystemClassId() const
{
auto pos = _name.find(SUBSYSTEM_NAME_SEPARATOR);
auto pos = _subsystemId.find(SUBSYSTEM_NAME_SEPARATOR);
if (pos == std::string::npos) {
return _name;
return _subsystemId;
}
return _name.substr(0, pos);
return _subsystemId.substr(0, pos);
}
std::string SGSubsystem::instanceName() const
std::string SGSubsystem::subsystemInstanceId() const
{
auto pos = _name.find(SUBSYSTEM_NAME_SEPARATOR);
auto pos = _subsystemId.find(SUBSYSTEM_NAME_SEPARATOR);
if (pos == std::string::npos) {
return {};
}
return _name.substr(pos+1);
return _subsystemId.substr(pos+1);
}
void SGSubsystem::set_group(SGSubsystemGroup* group)
@ -175,7 +175,7 @@ SGSubsystemGroup* SGSubsystem::get_group() const
SGSubsystemMgr* SGSubsystem::get_manager() const
{
if (!get_group())
throw sg_exception("SGSubsystem::get_manager: subsystem " + name() + " has no group");
throw sg_exception("SGSubsystem::get_manager: subsystem " + subsystemId() + " has no group");
return get_group()->get_manager();
}
@ -474,7 +474,7 @@ void SGSubsystem::reportTimingStats(TimerStats *__lastValues) {
if (reportDeltas) {
auto deltaT = _executionTime - _lastExecutionTime;
if (deltaT != 0) {
t << name() << "(+" << std::setprecision(2) << std::right << deltaT << "ms).";
t << subsystemInstanceId() << "(+" << std::setprecision(2) << std::right << deltaT << "ms).";
_name = t.str();
}
}
@ -506,7 +506,7 @@ void SGSubsystem::reportTimingStats(TimerStats *__lastValues) {
void SGSubsystemGroup::reportTimingStats(TimerStats *_lastValues) {
SGSubsystem::reportTimingStats(_lastValues);
std::string _name = name();
std::string _name = subsystemInstanceId();
if (!_name.size())
_name = typeid(this).name();
if (_lastValues) {
@ -514,11 +514,11 @@ void SGSubsystemGroup::reportTimingStats(TimerStats *_lastValues) {
if (deltaT != 0) {
SG_LOG(SG_EVENT, SG_ALERT,
" +" << std::setw(6) << std::setprecision(4) << std::right << deltaT << "ms "
<< name() );
<< subsystemInstanceId() );
}
}
else
SG_LOG(SG_EVENT, SG_ALERT, "SubSystemGroup: " << name() << " " << std::setw(6) << std::setprecision(4) << std::right << _executionTime / 1000.0 << "s");
SG_LOG(SG_EVENT, SG_ALERT, "SubSystemGroup: " << subsystemInstanceId() << " " << std::setw(6) << std::setprecision(4) << std::right << _executionTime / 1000.0 << "s");
for (auto member : _members) {
member->reportTimingStats(_lastValues);
}
@ -575,11 +575,11 @@ SGSubsystemGroup::set_subsystem (const string &name, SGSubsystem * subsystem,
if (name.empty()) {
SG_LOG(SG_GENERAL, SG_DEV_WARN, "adding subsystem to group without a name");
// TODO, make this an exception in the future
} else if (subsystem->name().empty()) {
} else if (subsystem->subsystemId().empty()) {
subsystem->set_name(name);
} else if (name != subsystem->name()) {
} else if (name != subsystem->subsystemId()) {
SG_LOG(SG_GENERAL, SG_DEV_WARN, "adding subsystem to group with name '" << name
<< "', but name() returns '" << subsystem->name() << "'");
<< "', but subsystemId() returns '" << subsystem->subsystemId() << "'");
}
notifyWillChange(subsystem, State::ADD);
@ -615,7 +615,7 @@ SGSubsystemGroup::set_subsystem (const string &name, SGSubsystem * subsystem,
void
SGSubsystemGroup::set_subsystem (SGSubsystem * subsystem, double min_step_sec)
{
set_subsystem(subsystem->name(), subsystem, min_step_sec);
set_subsystem(subsystem->subsystemId(), subsystem, min_step_sec);
}
SGSubsystem *
@ -1039,9 +1039,9 @@ SGSubsystemMgr::get_subsystem (const string &name) const
}
SGSubsystem*
SGSubsystemMgr::get_subsystem(const std::string &name, const std::string& instanceName) const
SGSubsystemMgr::get_subsystem(const std::string &name, const std::string& subsystemInstanceId) const
{
return get_subsystem(name + SUBSYSTEM_NAME_SEPARATOR + instanceName);
return get_subsystem(name + SUBSYSTEM_NAME_SEPARATOR + subsystemInstanceId);
}
@ -1174,7 +1174,7 @@ SGSubsystemMgr::create(const std::string& name)
}
SGSubsystemRef
SGSubsystemMgr::createInstance(const std::string& name, const std::string& instanceName)
SGSubsystemMgr::createInstance(const std::string& name, const std::string& subsystemInstanceId)
{
auto it = findRegistration(name);
auto &global_registrations = getGlobalRegistrations();
@ -1191,7 +1191,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 + SUBSYSTEM_NAME_SEPARATOR + instanceName;
const auto combinedName = name + SUBSYSTEM_NAME_SEPARATOR + subsystemInstanceId;
ref->set_name(combinedName);
return ref;
}
@ -1276,7 +1276,7 @@ namespace {
// allow override of the name but defaultt o the subsystem name
std::string name = arg->getStringValue("name");
std::string instanceName = arg->getStringValue("instance");
std::string subsystemInstanceId = arg->getStringValue("instance");
if (name.empty()) {
// default name to subsystem name, but before we parse any instance name
@ -1285,19 +1285,19 @@ namespace {
auto separatorPos = subsystem.find(SUBSYSTEM_NAME_SEPARATOR);
if (separatorPos != std::string::npos) {
if (!instanceName.empty()) {
if (!subsystemInstanceId.empty()) {
SG_LOG(SG_GENERAL, SG_WARN, "Specified a composite subsystem name and an instance name, please do one or the other: "
<< instanceName << " and " << subsystem);
<< subsystemInstanceId << " and " << subsystem);
return false;
}
instanceName = subsystem.substr(separatorPos + 1);
subsystemInstanceId = subsystem.substr(separatorPos + 1);
subsystem = subsystem.substr(0, separatorPos);
}
SGSubsystem* sub = nullptr;
if (!instanceName.empty()) {
sub = manager->createInstance(subsystem, instanceName);
if (!subsystemInstanceId.empty()) {
sub = manager->createInstance(subsystem, subsystemInstanceId);
} else {
sub = manager->create(subsystem);
}
@ -1313,7 +1313,7 @@ namespace {
double minTime = arg->getDoubleValue("min-time-sec", 0.0);
const auto combinedName = subsystem + SUBSYSTEM_NAME_SEPARATOR + instanceName;
const auto combinedName = subsystem + SUBSYSTEM_NAME_SEPARATOR + subsystemInstanceId;
manager->add(combinedName.c_str(),
sub,
group,

View File

@ -282,19 +282,19 @@ public:
/**
* composite name for this subsystem (type name & optional instance name)
*/
std::string name() const
{ return _name; }
std::string subsystemId() const
{ return _subsystemId; }
/**
* @brief the type (class)-specific part of the subsystem name.
*/
std::string typeName() const;
std::string subsystemClassId() const;
/**
* @brief the instance part of the subsystem name. Empty if this
* subsystem is not instanced
*/
std::string instanceName() const;
std::string subsystemInstanceId() const;
virtual bool is_group() const
{ return false; }
@ -363,6 +363,11 @@ protected:
static int maxTimePerFrame_ms;
private:
/// 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 _subsystemId;
SGSubsystemGroup* _group = nullptr;
protected:
TimerStats _timerStats, _lastTimerStats;
@ -426,7 +431,7 @@ public:
template<class T>
T* get_subsystem()
{
return dynamic_cast<T*>(get_subsystem(T::subsystemName()));
return dynamic_cast<T*>(get_subsystem(T::staticSubsystemClassId()));
}
bool is_group() const override
@ -535,7 +540,7 @@ public:
SGSubsystem* get_subsystem(const std::string &name) const;
SGSubsystem* get_subsystem(const std::string &name, const std::string& instanceName) const;
SGSubsystem* get_subsystem(const std::string &name, const std::string& subsystemInstanceId) const;
void reportTiming();
void setReportTimingCb(void* userData, SGSubsystemTimingCb cb) { reportTimingCb = cb; reportTimingUserData = userData; }
@ -556,21 +561,21 @@ public:
template<class T>
T* get_subsystem() const
{
return dynamic_cast<T*>(get_subsystem(T::subsystemName()));
return dynamic_cast<T*>(get_subsystem(T::staticSubsystemClassId()));
}
// instanced overloads, for both raw char* and std::string
// these concatenate the subsystem type name with the instance name
template<class T>
T* get_subsystem(const char* instanceName) const
T* get_subsystem(const char* subsystemInstanceId) const
{
return dynamic_cast<T*>(get_subsystem(T::subsystemName(), instanceName));
return dynamic_cast<T*>(get_subsystem(T::staticSubsystemClassId(), subsystemInstanceId));
}
template<class T>
T* get_subsystem(const std::string& instanceName) const
T* get_subsystem(const std::string& subsystemInstanceId) const
{
return dynamic_cast<T*>(get_subsystem(T::subsystemName(), instanceName));
return dynamic_cast<T*>(get_subsystem(T::staticSubsystemClassId(), subsystemInstanceId));
}
struct Dependency {
@ -606,7 +611,7 @@ public:
std::initializer_list<Dependency> deps = {})
{
SubsystemFactoryFunctor factory = [](){ return new T; };
SGSubsystemMgr::registerSubsystem(T::subsystemName(),
SGSubsystemMgr::registerSubsystem(T::staticSubsystemClassId(),
factory, group,
false, updateInterval,
deps);
@ -624,7 +629,7 @@ public:
std::initializer_list<Dependency> deps = {})
{
SubsystemFactoryFunctor factory = [](){ return new T; };
SGSubsystemMgr::registerSubsystem(T::subsystemName(),
SGSubsystemMgr::registerSubsystem(T::staticSubsystemClassId(),
factory, group,
true, updateInterval,
deps);
@ -640,14 +645,14 @@ public:
template <class T>
SGSharedPtr<T> add(GroupType customGroup = INVALID, double customInterval = 0.0)
{
auto ref = create(T::subsystemName());
auto ref = create(T::staticSubsystemClassId());
const GroupType group = (customGroup == INVALID) ?
defaultGroupFor(T::subsystemName()) : customGroup;
defaultGroupFor(T::staticSubsystemClassId()) : customGroup;
const double interval = (customInterval == 0.0) ?
defaultUpdateIntervalFor(T::subsystemName()) : customInterval;
add(ref->name().c_str(), ref.ptr(), group, interval);
defaultUpdateIntervalFor(T::staticSubsystemClassId()) : customInterval;
add(ref->subsystemId().c_str(), ref.ptr(), group, interval);
return dynamic_cast<T*>(ref.ptr());
}
@ -658,20 +663,20 @@ public:
template <class T>
SGSharedPtr<T> create()
{
auto ref = create(T::subsystemName());
auto ref = create(T::staticSubsystemClassId());
return dynamic_cast<T*>(ref.ptr());
}
SGSubsystemRef create(const std::string& name);
template <class T>
SGSharedPtr<T> createInstance(const std::string& instanceName)
SGSharedPtr<T> createInstance(const std::string& subsystemInstanceId)
{
auto ref = createInstance(T::subsystemName(), instanceName);
auto ref = createInstance(T::staticSubsystemClassId(), subsystemInstanceId);
return dynamic_cast<T*>(ref.ptr());
}
SGSubsystemRef createInstance(const std::string& name, const std::string& instanceName);
SGSubsystemRef createInstance(const std::string& name, const std::string& subsystemInstanceId);
static GroupType defaultGroupFor(const char* name);
static double defaultUpdateIntervalFor(const char* name);

View File

@ -20,7 +20,7 @@ using std::endl;
class MySub1 : public SGSubsystem
{
public:
static const char* subsystemName() { return "mysub"; }
static const char* staticSubsystemClassId() { return "mysub"; }
void init() override
{
@ -45,7 +45,7 @@ public:
class AnotherSub : public SGSubsystem
{
public:
static const char* subsystemName() { return "anothersub"; }
static const char* staticSubsystemClassId() { return "anothersub"; }
void init() override
{
@ -70,7 +70,7 @@ public:
class FakeRadioSub : public SGSubsystem
{
public:
static const char* subsystemName() { return "fake-radio"; }
static const char* staticSubsystemClassId() { return "fake-radio"; }
void init() override
{
@ -89,8 +89,9 @@ public:
class InstrumentGroup : public SGSubsystemGroup
{
public:
static const char* subsystemName() { return "instruments"; }
InstrumentGroup() : SGSubsystemGroup(InstrumentGroup::subsystemName()) {}
InstrumentGroup() : SGSubsystemGroup(InstrumentGroup::staticSubsystemClassId()) {}
static const char* staticSubsystemClassId() { return "instruments"; }
virtual ~InstrumentGroup()
{
}
@ -119,11 +120,11 @@ class RecorderDelegate : public SGSubsystemMgr::Delegate
public:
void willChange(SGSubsystem* sub, SGSubsystem::State newState) override
{
events.push_back({sub->name(), false, newState});
events.push_back({sub->subsystemId(), false, newState});
}
void didChange(SGSubsystem* sub, SGSubsystem::State newState) override
{
events.push_back({sub->name(), true, newState});
events.push_back({sub->subsystemId(), true, newState});
}
struct Event {
@ -168,10 +169,10 @@ void testRegistrationAndCreation()
auto anotherSub = manager->create<AnotherSub>();
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());
SG_CHECK_EQUAL(anotherSub->subsystemId(), AnotherSub::staticSubsystemClassId());
SG_CHECK_EQUAL(anotherSub->subsystemId(), std::string("anothersub"));
SG_CHECK_EQUAL(anotherSub->subsystemClassId(), std::string("anothersub"));
SG_CHECK_EQUAL(anotherSub->subsystemInstanceId(), std::string());
auto radio1 = manager->createInstance<FakeRadioSub>("nav1");
auto radio2 = manager->createInstance<FakeRadioSub>("nav2");
@ -187,8 +188,8 @@ void testAddGetRemove()
auto anotherSub = manager->add<AnotherSub>();
SG_VERIFY(anotherSub);
SG_CHECK_EQUAL(anotherSub->name(), AnotherSub::subsystemName());
SG_CHECK_EQUAL(anotherSub->name(), std::string("anothersub"));
SG_CHECK_EQUAL(anotherSub->subsystemId(), AnotherSub::staticSubsystemClassId());
SG_CHECK_EQUAL(anotherSub->subsystemId(), std::string("anothersub"));
SG_VERIFY(d->hasEvent("anothersub-will-add"));
SG_VERIFY(d->hasEvent("anothersub-did-add"));
@ -200,14 +201,14 @@ void testAddGetRemove()
// manual create & add
auto mySub = manager->create<MySub1>();
manager->add(MySub1::subsystemName(), mySub.ptr(), SGSubsystemMgr::DISPLAY, 0.1234);
manager->add(MySub1::staticSubsystemClassId(), mySub.ptr(), SGSubsystemMgr::DISPLAY, 0.1234);
SG_VERIFY(d->hasEvent("mysub-will-add"));
SG_VERIFY(d->hasEvent("mysub-did-add"));
SG_CHECK_EQUAL(manager->get_subsystem<MySub1>(), mySub);
bool ok = manager->remove(AnotherSub::subsystemName());
bool ok = manager->remove(AnotherSub::staticSubsystemClassId());
SG_VERIFY(ok);
SG_VERIFY(d->hasEvent("anothersub-will-remove"));
SG_VERIFY(d->hasEvent("anothersub-did-remove"));
@ -219,7 +220,7 @@ void testAddGetRemove()
// re-add of removed, and let's test overriding
auto another2 = manager->add<AnotherSub>(SGSubsystemMgr::SOUND);
SG_CHECK_EQUAL(another2->name(), AnotherSub::subsystemName());
SG_CHECK_EQUAL(another2->subsystemId(), AnotherSub::staticSubsystemClassId());
auto soundGroup = manager->get_group(SGSubsystemMgr::SOUND);
SG_CHECK_EQUAL(soundGroup->get_subsystem("anothersub"), another2);
@ -238,10 +239,10 @@ 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->typeName(), std::string("fake-radio"));
SG_CHECK_EQUAL(radio2->instanceName(), std::string("nav2"));
SG_CHECK_EQUAL(radio1->subsystemId(), std::string("fake-radio.nav1"));
SG_CHECK_EQUAL(radio2->subsystemId(), std::string("fake-radio.nav2"));
SG_CHECK_EQUAL(radio1->subsystemClassId(), std::string("fake-radio"));
SG_CHECK_EQUAL(radio2->subsystemInstanceId(), std::string("nav2"));
instruments->set_subsystem(radio1);
instruments->set_subsystem(radio2);