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());
}