Simplify the function signature of the FactoryManager function

This commit is contained in:
Alexander Bock
2022-02-12 00:55:15 +01:00
parent 44655e5faa
commit 40ed640bc8
8 changed files with 29 additions and 66 deletions

View File

@@ -88,12 +88,14 @@ public:
/**
* Adds the passed \p factory to the FactoryManager. Factories may only be added once.
* \param factory The ghoul::TemplateFactory to add to this FactoryManager
* \param name A user-readable name for the registered factory.
* \pre \p factory must not be nullptr
*
* \tparam Factory The type for which a factory should be created and added
*
* \pre \p name must not be empty
*/
void addFactory(std::unique_ptr<ghoul::TemplateFactoryBase> factory,
std::string name = "");
template <typename T>
void addFactory(std::string name);
/**
* This method provides access to all registered ghoul::TemplateFactory%s through
@@ -108,8 +110,8 @@ public:
ghoul::TemplateFactory<T>* factory() const;
std::string generateJson() const override;
private:
private:
/// Singleton member for the Factory Manager
static FactoryManager* _manager;

View File

@@ -26,9 +26,17 @@
namespace openspace {
template <typename T>
void FactoryManager::addFactory(std::string name) {
ghoul_assert(!name.empty(), "Name must not be empty");
auto f = std::make_unique<ghoul::TemplateFactory<T>>();
_factories.push_back({ std::move(f), std::move(name) });
}
template <class T>
ghoul::TemplateFactory<T>* FactoryManager::factory() const {
for (auto& f : _factories) {
for (const FactoryInfo& f : _factories) {
if (f.factory->baseClassType() == typeid(T))
return dynamic_cast<ghoul::TemplateFactory<T>*>(f.factory.get());
}

View File

@@ -88,10 +88,7 @@ ghoul::opengl::TextureManager BaseModule::TextureManager;
BaseModule::BaseModule() : OpenSpaceModule(BaseModule::Name) {}
void BaseModule::internalInitialize(const ghoul::Dictionary&) {
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<ScreenSpaceRenderable>>(),
"ScreenSpaceRenderable"
);
FactoryManager::ref().addFactory<ScreenSpaceRenderable>("ScreenSpaceRenderable");
ghoul::TemplateFactory<ScreenSpaceRenderable>* fSsRenderable =
FactoryManager::ref().factory<ScreenSpaceRenderable>();

View File

@@ -289,10 +289,7 @@ void GlobeBrowsingModule::internalInitialize(const ghoul::Dictionary& dict) {
ghoul_assert(fRotation, "Rotation factory was not created");
fRotation->registerClass<globebrowsing::GlobeRotation>("GlobeRotation");
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<TileProvider>>(),
_factoryName
);
FactoryManager::ref().addFactory<TileProvider>(_factoryName);
ghoul::TemplateFactory<TileProvider>* fTileProvider =
FactoryManager::ref().factory<TileProvider>();

View File

@@ -73,10 +73,7 @@ SpaceModule::SpaceModule()
}
void SpaceModule::internalInitialize(const ghoul::Dictionary& dictionary) {
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<planetgeometry::PlanetGeometry>>(),
"PlanetGeometry"
);
FactoryManager::ref().addFactory<planetgeometry::PlanetGeometry>("PlanetGeometry");
ghoul::TemplateFactory<Renderable>* fRenderable =
FactoryManager::ref().factory<Renderable>();

View File

@@ -48,10 +48,7 @@ void SpacecraftInstrumentsModule::internalInitialize(const ghoul::Dictionary&) {
ImageSequencer::initialize();
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<Decoder>>(),
"Decoder"
);
FactoryManager::ref().addFactory<Decoder>("Decoder");
ghoul::TemplateFactory<DashboardItem>* fDashboard =
FactoryManager::ref().factory<DashboardItem>();

View File

@@ -131,42 +131,15 @@ OpenSpaceEngine::OpenSpaceEngine()
: _printEvents(PrintEventsInfo, false)
{
FactoryManager::initialize();
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<Renderable>>(),
"Renderable"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<Translation>>(),
"Translation"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<Rotation>>(),
"Rotation"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<Scale>>(),
"Scale"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<TimeFrame>>(),
"TimeFrame"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<LightSource>>(),
"LightSource"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<Task>>(),
"Task"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<ResourceSynchronization>>(),
"ResourceSynchronization"
);
FactoryManager::ref().addFactory(
std::make_unique<ghoul::TemplateFactory<DashboardItem>>(),
"DashboardItem"
);
FactoryManager::ref().addFactory<Renderable>("Renderable");
FactoryManager::ref().addFactory<Translation>("Translation");
FactoryManager::ref().addFactory<Rotation>("Rotation");
FactoryManager::ref().addFactory<Scale>("Scale");
FactoryManager::ref().addFactory<TimeFrame>("TimeFrame");
FactoryManager::ref().addFactory<LightSource>("LightSource");
FactoryManager::ref().addFactory<Task>("Task");
FactoryManager::ref().addFactory<ResourceSynchronization>("ResourceSynchronization");
FactoryManager::ref().addFactory<DashboardItem>("DashboardItem");
SpiceManager::initialize();
TransformationManager::initialize();

View File

@@ -70,14 +70,6 @@ FactoryManager& FactoryManager::ref() {
return *_manager;
}
void FactoryManager::addFactory(std::unique_ptr<ghoul::TemplateFactoryBase> f,
std::string name)
{
ghoul_assert(f, "Factory must not be nullptr");
_factories.push_back({ std::move(f), std::move(name) });
}
std::string FactoryManager::generateJson() const {
std::stringstream json;