Skip to content

Commit

Permalink
Fix for FactoryRegistration problem
Browse files Browse the repository at this point in the history
  • Loading branch information
wdeconinck committed Dec 12, 2024
1 parent a3b271a commit 4b3e396
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/atlas/util/Factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,14 @@ FactoryBase::~FactoryBase() {

//----------------------------------------------------------------------------------------------------------------------

std::shared_ptr<FactoryRegistry> FactoryRegistry::instance(const std::string& factory) {
static std::map<std::string,std::shared_ptr<FactoryRegistry>> 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
19 changes: 4 additions & 15 deletions src/atlas/util/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace util {
class FactoryBase;

class FactoryRegistry {
protected:
private:
FactoryRegistry(const std::string& factory);
virtual ~FactoryRegistry();

Expand All @@ -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 <typename T>
struct FactoryRegistryT : public FactoryRegistry {
public:
static std::shared_ptr<FactoryRegistryT<T>> instance() {
static std::shared_ptr<FactoryRegistryT<T>> env(new FactoryRegistryT<T>(T::className()));
return env;
}
virtual ~FactoryRegistryT() {}

protected:
FactoryRegistryT(const std::string& factory): FactoryRegistry(factory) {}
static std::shared_ptr<FactoryRegistry> instance(const std::string& factory);
};

class FactoryBase {
Expand Down Expand Up @@ -85,13 +74,13 @@ class Factory : public FactoryBase {

Factory(const std::string& builder = ""): FactoryBase(registry(), builder) {
if (not builder.empty()) {
attach_registry(FactoryRegistryT<T>::instance());
attach_registry(FactoryRegistry::instance(T::className()));
}
}

protected:
virtual ~Factory(){};
static FactoryRegistry& registry() { return *FactoryRegistryT<T>::instance().get(); }
static FactoryRegistry& registry() { return *FactoryRegistry::instance(T::className()).get(); }
};

//----------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 4b3e396

Please sign in to comment.