19#include <unordered_map>
50 [[nodiscard]]
virtual void*
Create() = 0;
75 explicit GFactory(
const std::shared_ptr<GOptions>& gopts)
77 static_assert(std::is_constructible_v<T, const std::shared_ptr<GOptions>&>,
78 "T must be constructible from const std::shared_ptr<GOptions>&");
85 [[nodiscard]]
void*
Create()
override {
return static_cast<void*
>(
new T(gopts_)); }
89 std::shared_ptr<GOptions> gopts_;
158 template <
class Derived>
168 template <
class Derived>
181 template <
class Base>
182 [[nodiscard]] Base*
CreateObject(std::string_view name)
const;
198 const std::shared_ptr<GOptions>& gopts);
213 void registerDL(std::string_view name);
216 std::unordered_map<std::
string, std::unique_ptr<
GFactoryBase>> factoryMap_;
219 std::unordered_map<std::
string, std::shared_ptr<
DynamicLib>> dlMap_;
232inline void GManager::registerDL(std::string_view name) {
234 const std::string filename = std::string{name} +
".gplugin";
238 dlMap_.emplace(std::string{name},
239 std::make_shared<DynamicLib>(
log, filename));
240 log->
debug(NORMAL,
"Loading DL ", name);
243template <
class Derived>
246 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>());
250template <
class Derived>
252 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>(gopts));
258 auto it = factoryMap_.find(std::string{name});
259 if (it == factoryMap_.end()) {
261 "Couldn't find factory <", name,
"> in factory map.");
266 return static_cast<Base*
>(it->second->Create());
271 const std::shared_ptr<GOptions>& gopts) {
273 auto pluginName = std::string{name};
274 auto pluginLib = dlMap_.at(pluginName);
276 if (pluginLib && pluginLib->handle) {
278 T* raw = T::instantiate(pluginLib->handle, gopts);
281 raw->set_loggers(gopts);
285 return std::shared_ptr<T>(raw, [pluginLib](T* ptr) {
std::shared_ptr< GLogger > log
Type-erased factory interface used by GManager.
virtual void * Create()=0
Instantiate the concrete product.
virtual ~GFactoryBase()=default
Virtual destructor for safe deletion through base pointer.
Concrete factory that creates objects of type T.
GFactory(const std::shared_ptr< GOptions > &gopts)
Construct a factory bound to a specific configuration/options instance.
void * Create() override
Allocate a new instance of T.
void debug(debug_type type, Args &&... args) const
void error(int exit_code, Args &&... args) const
Factory registry and dynamic-library manager for run-time creation of plugin objects.
GManager(const std::shared_ptr< GOptions > &gopt)
Construct a manager instance.
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 plugin library and instantiate an object from it.
GManager(const GManager &)=delete
No copy – the manager owns unique resources (factory objects and loaded libraries).
Base * CreateObject(std::string_view name) const
Create an instance of a previously registered factory.
GManager & operator=(const GManager &)=delete
void clearDLMap() noexcept
Release all loaded dynamic libraries.
void RegisterObjectFactory(std::string_view name)
Register a concrete factory under a string key.
#define ERR_FACTORYNOTFOUND
#define ERR_DLHANDLENOTFOUND
constexpr const char * PLUGIN_LOGGER
Logger channel used by the gfactory module and plugins loaded through it.
Helper that loads a shared library and holds its POSIX handle.