From 4b3e39611b4f56b70a92a51272174085103acb3f Mon Sep 17 00:00:00 2001 From: Willem Deconinck Date: Tue, 10 Dec 2024 17:15:32 +0000 Subject: [PATCH] Fix for FactoryRegistration problem --- src/atlas/util/Factory.cc | 9 +++++++++ src/atlas/util/Factory.h | 19 ++++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/atlas/util/Factory.cc b/src/atlas/util/Factory.cc index dee6d5a74..869b1d4df 100644 --- a/src/atlas/util/Factory.cc +++ b/src/atlas/util/Factory.cc @@ -112,5 +112,14 @@ FactoryBase::~FactoryBase() { //---------------------------------------------------------------------------------------------------------------------- +std::shared_ptr FactoryRegistry::instance(const std::string& factory) { + static std::map> registries; + if (registries.find(factory) == registries.end()) { + auto [it_pair, inserted] = registries.emplace(factory, new FactoryRegistry(factory)); + return it_pair->second; + } + return registries.at(factory); +} + } // namespace util } // namespace atlas diff --git a/src/atlas/util/Factory.h b/src/atlas/util/Factory.h index 5474d8b83..a0c4be047 100644 --- a/src/atlas/util/Factory.h +++ b/src/atlas/util/Factory.h @@ -26,7 +26,7 @@ namespace util { class FactoryBase; class FactoryRegistry { -protected: +private: FactoryRegistry(const std::string& factory); virtual ~FactoryRegistry(); @@ -43,19 +43,8 @@ class FactoryRegistry { void add(const std::string& builder, FactoryBase*); void remove(const std::string& builder); FactoryBase* get(const std::string& builder) const; -}; - -template -struct FactoryRegistryT : public FactoryRegistry { -public: - static std::shared_ptr> instance() { - static std::shared_ptr> env(new FactoryRegistryT(T::className())); - return env; - } - virtual ~FactoryRegistryT() {} -protected: - FactoryRegistryT(const std::string& factory): FactoryRegistry(factory) {} + static std::shared_ptr instance(const std::string& factory); }; class FactoryBase { @@ -85,13 +74,13 @@ class Factory : public FactoryBase { Factory(const std::string& builder = ""): FactoryBase(registry(), builder) { if (not builder.empty()) { - attach_registry(FactoryRegistryT::instance()); + attach_registry(FactoryRegistry::instance(T::className())); } } protected: virtual ~Factory(){}; - static FactoryRegistry& registry() { return *FactoryRegistryT::instance().get(); } + static FactoryRegistry& registry() { return *FactoryRegistry::instance(T::className()).get(); } }; //----------------------------------------------------------------------------------------------------------------------