gfactory
Loading...
Searching...
No Matches
GManager Class Reference

Factory registry and dynamic-library manager for run-time creation of plugin objects. More...

#include <gfactory.h>

Public Member Functions

 GManager (const std::shared_ptr< GOptions > &gopt)
 Construct a manager instance.
 
 GManager (const GManager &)=delete
 No copy – the manager owns unique resources (factory objects and loaded libraries).
 
GManageroperator= (const GManager &)=delete
 
 GManager (GManager &&) noexcept=default
 Allow move for container support.
 
GManageroperator= (GManager &&) noexcept=default
 
 ~GManager () override
 Destructor.
 
template<class Derived >
void RegisterObjectFactory (std::string_view name)
 Register a concrete factory under a string key.
 
template<class Derived >
void RegisterObjectFactory (std::string_view name, const std::shared_ptr< GOptions > &gopts)
 Register a concrete factory under a string key using a specific options object.
 
template<class Base >
Base * CreateObject (std::string_view name) const
 Create an instance of a previously registered factory.
 
template<class T >
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.
 
void clearDLMap () noexcept
 Release all loaded dynamic libraries.
 
- Public Member Functions inherited from GBase< GManager >
 GBase (const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
 
 GBase (const std::shared_ptr< GLogger > &logger)
 
 GBase (const GBase &)=default
 
 GBase (GBase &&) noexcept=default
 
virtual ~GBase ()
 
GBaseoperator= (const GBase &)=default
 
GBaseoperator= (GBase &&) noexcept=default
 

Additional Inherited Members

- Protected Attributes inherited from GBase< GManager >
std::shared_ptr< GLoggerlog
 

Detailed Description

GManager provides two related services:

  1. Static factory registry
    • Register a concrete type under a string key (e.g., "triangle").
    • Instantiate it later using CreateObject().
  2. Dynamic library loading
    • Load a module (<name>.gplugin) into the process.
    • Instantiate an object by calling a well-known entry point (via dlsym inside the product type).

Ownership and lifetime

  • For static creation (CreateObject()), the returned raw pointer is owned by the caller.
  • For dynamic creation (LoadAndRegisterObjectFromLibrary()), the returned std::shared_ptr<T> ensures:
    • the object is deleted when the shared pointer is released, and
    • the dynamic library remains loaded at least as long as the object is alive (the deleter captures the std::shared_ptr<DynamicLib>).

Error handling

If a factory key is unknown or a library cannot be loaded, the manager logs an error using its logger. The exact policy after logging depends on the logger configuration (e.g., may abort, throw, or continue).

Definition at line 121 of file gfactory.h.

Constructor & Destructor Documentation

◆ GManager() [1/3]

GManager::GManager ( const std::shared_ptr< GOptions > & gopt)
inlineexplicit
Parameters
goptShared options object used to configure the base logger and behavior.
Note
The manager uses the PLUGIN_LOGGER channel for plugin-related output.

Definition at line 131 of file gfactory.h.

◆ GManager() [2/3]

GManager::GManager ( const GManager & )
delete

◆ GManager() [3/3]

GManager::GManager ( GManager && )
defaultnoexcept

◆ ~GManager()

GManager::~GManager ( )
inlineoverride

Calls clearDLMap() to release any loaded libraries.

Definition at line 147 of file gfactory.h.

Member Function Documentation

◆ clearDLMap()

void GManager::clearDLMap ( )
inlinenoexcept

This is safe to call multiple times.

Definition at line 226 of file gfactory.h.

◆ CreateObject()

template<class Base >
Base * GManager::CreateObject ( std::string_view name) const
Template Parameters
BaseBase type to cast the created object to.
Parameters
nameFactory key.
Returns
A pointer to a heap-allocated object cast to Base.
Warning
The caller owns the returned pointer and must delete it.

Definition at line 257 of file gfactory.h.

◆ LoadAndRegisterObjectFromLibrary()

template<class T >
std::shared_ptr< T > GManager::LoadAndRegisterObjectFromLibrary ( std::string_view name,
const std::shared_ptr< GOptions > & gopts )

The product type T must provide a static method instantiate(dlhandle, std::shared_ptr<GOptions>) that performs the symbol lookup and returns a raw pointer.

Template Parameters
TProduct base type.
Parameters
namePlugin name (used to form <name>.gplugin).
goptsOptions to pass to the instantiated object.
Returns
std::shared_ptr<T> owning the created object; its deleter also retains the library.

Definition at line 270 of file gfactory.h.

◆ operator=() [1/2]

GManager & GManager::operator= ( const GManager & )
delete

◆ operator=() [2/2]

GManager & GManager::operator= ( GManager && )
defaultnoexcept

◆ RegisterObjectFactory() [1/2]

template<class Derived >
void GManager::RegisterObjectFactory ( std::string_view name)
Template Parameters
DerivedConcrete type to instantiate.
Parameters
nameFactory key used later by CreateObject().
Note
This overload registers the type only. The factory must still be constructible as required by GFactory.

Definition at line 244 of file gfactory.h.

◆ RegisterObjectFactory() [2/2]

template<class Derived >
void GManager::RegisterObjectFactory ( std::string_view name,
const std::shared_ptr< GOptions > & gopts )
Template Parameters
DerivedConcrete type to instantiate.
Parameters
nameFactory key used later by CreateObject().
goptsOptions forwarded to the factory and then to constructed objects.

Definition at line 251 of file gfactory.h.


The documentation for this class was generated from the following file: