11 #include <string_view>
12 #include <unordered_map>
26 [[nodiscard]]
virtual void*
Create() = 0;
36 [[nodiscard]]
void*
Create()
override {
return new T(); }
56 explicit GManager(std::shared_ptr<GLogger> logger, std::string description =
"");
68 template <class Derived>
73 [[nodiscard]] Base*
CreateObject(std::string_view name) const;
83 void registerDL(std::string_view name);
85 std::unordered_map<std::
string, std::unique_ptr<
GFactoryBase>> factoryMap_;
86 std::unordered_map<std::
string, std::unique_ptr<
DynamicLib>> dlMap_;
89 std::shared_ptr<GLogger> log;
95 gname = log->get_verbosity_name() + description;
96 log->debug(CONSTRUCTOR, gname);
100 log->debug(DESTRUCTOR, gname);
106 inline void GManager::registerDL(std::string_view name) {
107 const std::string filename = std::string{name} +
".gplugin";
108 dlMap_.emplace(std::string{name},
109 std::make_unique<DynamicLib>(log, filename));
110 log->debug(NORMAL,
"Loading DL ", name);
113 template <
class Derived>
115 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>());
116 log->debug(NORMAL,
"Registering ", name,
" into factory map");
119 template <
class Base>
121 auto it = factoryMap_.find(std::string{name});
122 if (it == factoryMap_.end()) {
124 "Couldn't find factory <", name,
"> in factory map.");
126 log->debug(NORMAL,
"Creating instance of <", name,
"> factory.");
127 return static_cast<Base*
>(it->second->Create());
133 auto& dynamicLib = dlMap_.at(std::string{name});
134 if (dynamicLib && dynamicLib->handle) {
135 auto factory = T::instantiate(dynamicLib->handle);
139 factory->set_loggers(gopts);
Abstract creator used by GManager through type‑erased pointers.
virtual ~GFactoryBase()=default
virtual void * Create()=0
Pure virtual instantiation hook implemented by the templated concrete factory.
Concrete factory that creates objects of type T.
void * Create() override
Pure virtual instantiation hook implemented by the templated concrete factory.
Owns factories and dynamically‑loaded libraries, providing run‑time creation.
GManager & operator=(const GManager &)=delete
GManager(GManager &&) noexcept=default
Allow move for container support.
GManager(const GManager &)=delete
No copy – the manager owns unique resources.
Base * CreateObject(std::string_view name) const
Create an instance of previously registered factory as Base*.
GManager(std::shared_ptr< GLogger > logger, std::string description="")
Construct with logger and a human‑readable name.
T * LoadAndRegisterObjectFromLibrary(std::string_view name, GOptions *gopts)
Load a shared library, look up its instantiate symbol, and return object.
void clearDLMap() noexcept
Explicit cleanup (also called by destructor) – idempotent.
void RegisterObjectFactory(std::string_view name)
Register a concrete factory under name.
#define ERR_FACTORYNOTFOUND
#define ERR_DLHANDLENOTFOUND
Structure to load dynamically symbols from a shared library.