13#include <unordered_map>
28 [[nodiscard]]
virtual void*
Create() = 0;
38 [[nodiscard]]
void*
Create()
override {
return new T(); }
71 template <
class Derived>
76 [[nodiscard]] Base*
CreateObject(std::string_view name)
const;
86 void registerDL(std::string_view name);
88 std::unordered_map<std::
string, std::unique_ptr<
GFactoryBase>> factoryMap_;
89 std::unordered_map<std::
string, std::shared_ptr<
DynamicLib>> dlMap_;
97inline void GManager::registerDL(std::string_view name) {
98 const std::string filename = std::string{name} +
".gplugin";
99 dlMap_.emplace(std::string{name},
100 std::make_shared<DynamicLib>(log, filename));
101 log->debug(NORMAL,
"Loading DL ", name);
104template <
class Derived>
106 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>());
107 log->debug(NORMAL,
"Registering ", name,
" into factory map");
112 auto it = factoryMap_.find(std::string{name});
113 if (it == factoryMap_.end()) {
115 "Couldn't find factory <", name,
"> in factory map.");
117 log->debug(NORMAL,
"Creating instance of <", name,
"> factory.");
118 return static_cast<Base*
>(it->second->Create());
124 auto pluginName = std::string{name};
125 auto pluginLib = dlMap_.at(pluginName);
127 if (pluginLib && pluginLib->handle) {
128 T* raw = T::instantiate(pluginLib->handle, gopts);
129 raw->set_loggers(gopts);
132 return std::shared_ptr<T>(raw, [pluginLib](T* ptr) {
Abstract creator used by GManager through type‑erased pointers.
virtual void * Create()=0
Pure virtual instantiation hook implemented by the templated concrete factory.
virtual ~GFactoryBase()=default
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(const std::shared_ptr< GOptions > &gopt)
Construct with logger and a human‑readable name.
GManager(GManager &&) noexcept=default
Allow move for container support.
std::shared_ptr< T > LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr< GOptions > &gopts)
Load a shared library, look up its instantiate symbol, and return object.
GManager(const GManager &)=delete
No copy – the manager owns unique resources.
Base * CreateObject(std::string_view name) const
Create an instance of the previously registered factory.
GManager & operator=(const GManager &)=delete
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
constexpr const char * PLUGIN_LOGGER
Structure to load dynamically symbols from a shared library.