gfactory
Loading...
Searching...
No Matches
gfactory.h
Go to the documentation of this file.
1#pragma once
9// C++
10#include <memory>
11#include <string>
12#include <string_view>
13#include <unordered_map>
14
15// gfactory
16#include "gdl.h"
17#include "gbase.h"
18#include "gfactory_options.h"
19
25public:
26 virtual ~GFactoryBase() = default;
28 [[nodiscard]] virtual void* Create() = 0;
29};
30
35template <class T>
36class GFactory final : public GFactoryBase {
37public:
38 [[nodiscard]] void* Create() override { return new T(); }
39};
40
55class GManager : public GBase<GManager> {
56public:
58 explicit GManager(const std::shared_ptr<GOptions>& gopt) : GBase(gopt, PLUGIN_LOGGER) {
59 }
60
62 GManager(const GManager&) = delete;
63 GManager& operator=(const GManager&) = delete;
65 GManager(GManager&&) noexcept = default;
66 GManager& operator=(GManager&&) noexcept = default;
67
69
71 template <class Derived>
72 void RegisterObjectFactory(std::string_view name);
73
75 template <class Base>
76 [[nodiscard]] Base* CreateObject(std::string_view name) const;
77
79 template <class T>
80 [[nodiscard]] std::shared_ptr<T> LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr<GOptions>& gopts);
81
83 void clearDLMap() noexcept;
84
85private:
86 void registerDL(std::string_view name);
87
88 std::unordered_map<std::string, std::unique_ptr<GFactoryBase>> factoryMap_;
89 std::unordered_map<std::string, std::shared_ptr<DynamicLib>> dlMap_;
90
91 std::string gname;
92};
93
94
95inline void GManager::clearDLMap() noexcept { dlMap_.clear(); }
96
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);
102}
103
104template <class Derived>
105void GManager::RegisterObjectFactory(std::string_view name) {
106 factoryMap_.emplace(std::string{name}, std::make_unique<GFactory<Derived>>());
107 log->debug(NORMAL, "Registering ", name, " into factory map");
108}
109
110template <class Base>
111Base* GManager::CreateObject(std::string_view name) const {
112 auto it = factoryMap_.find(std::string{name});
113 if (it == factoryMap_.end()) {
114 log->error(ERR_FACTORYNOTFOUND,
115 "Couldn't find factory <", name, "> in factory map.");
116 }
117 log->debug(NORMAL, "Creating instance of <", name, "> factory.");
118 return static_cast<Base*>(it->second->Create());
119}
120
121template <class T>
122std::shared_ptr<T> GManager::LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr<GOptions>& gopts) {
123 registerDL(name);
124 auto pluginName = std::string{name};
125 auto pluginLib = dlMap_.at(pluginName); // shared_ptr<DynamicLib>
126
127 if (pluginLib && pluginLib->handle) {
128 T* raw = T::instantiate(pluginLib->handle, gopts);
129 raw->set_loggers(gopts);
130
131 // return shared_ptr<T> with deleter that captures pluginLib
132 return std::shared_ptr<T>(raw, [pluginLib](T* ptr) {
133 delete ptr;
134 // pluginLib keeps .so alive until ptr is destroyed
135 });
136 }
137
138 log->error(ERR_DLHANDLENOTFOUND, "Plugin ", name, " could not be loaded.");
139}
Abstract creator used by GManager through type‑erased pointers.
Definition gfactory.h:24
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.
Definition gfactory.h:36
void * Create() override
Pure virtual instantiation hook implemented by the templated concrete factory.
Definition gfactory.h:38
Owns factories and dynamically‑loaded libraries, providing run‑time creation.
Definition gfactory.h:55
GManager(const std::shared_ptr< GOptions > &gopt)
Construct with logger and a human‑readable name.
Definition gfactory.h:58
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.
Definition gfactory.h:122
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.
Definition gfactory.h:111
GManager & operator=(const GManager &)=delete
void clearDLMap() noexcept
Explicit cleanup (also called by destructor) – idempotent.
Definition gfactory.h:95
void RegisterObjectFactory(std::string_view name)
Register a concrete factory under name.
Definition gfactory.h:105
#define ERR_FACTORYNOTFOUND
Definition gdl.h:23
#define ERR_DLHANDLENOTFOUND
Definition gdl.h:24
constexpr const char * PLUGIN_LOGGER
Structure to load dynamically symbols from a shared library.
Definition gdl.h:31