13#include <unordered_map>
28 [[nodiscard]]
virtual void*
Create() = 0;
38 explicit GFactory(
const std::shared_ptr<GOptions>& gopts)
40 static_assert(std::is_constructible_v<T, const std::shared_ptr<GOptions>&>,
41 "T must be constructible from const std::shared_ptr<GOptions>&");
44 [[nodiscard]]
void*
Create()
override {
return static_cast<void*
>(
new T(gopts_)); }
47 std::shared_ptr<GOptions> gopts_;
80 template <
class Derived>
83 template <
class Derived>
88 [[nodiscard]] Base*
CreateObject(std::string_view name)
const;
98 void registerDL(std::string_view name);
100 std::unordered_map<std::
string, std::unique_ptr<
GFactoryBase>> factoryMap_;
101 std::unordered_map<std::
string, std::shared_ptr<
DynamicLib>> dlMap_;
109inline void GManager::registerDL(std::string_view name) {
110 const std::string filename = std::string{name} +
".gplugin";
111 dlMap_.emplace(std::string{name},
112 std::make_shared<DynamicLib>(log, filename));
113 log->debug(NORMAL,
"Loading DL ", name);
116template <
class Derived>
118 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>());
119 log->debug(NORMAL,
"Registering ", name,
" into factory map");
122template <
class Derived>
124 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>(gopts));
125 log->debug(NORMAL,
"Registering ", name,
" into factory map");
130 auto it = factoryMap_.find(std::string{name});
131 if (it == factoryMap_.end()) {
133 "Couldn't find factory <", name,
"> in factory map.");
135 log->debug(NORMAL,
"Creating instance of <", name,
"> factory.");
136 return static_cast<Base*
>(it->second->Create());
142 auto pluginName = std::string{name};
143 auto pluginLib = dlMap_.at(pluginName);
145 if (pluginLib && pluginLib->handle) {
146 T* raw = T::instantiate(pluginLib->handle, gopts);
147 raw->set_loggers(gopts);
150 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.
GFactory(const std::shared_ptr< GOptions > &gopts)
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 a name.
#define ERR_FACTORYNOTFOUND
#define ERR_DLHANDLENOTFOUND
constexpr const char * PLUGIN_LOGGER
Structure to load dynamically symbols from a shared library.