fourdst_plugin 0.0.1a
C++ Plugin Manager
Loading...
Searching...
No Matches
fourdst::plugin::manager::PluginManager Class Reference

Central manager for plugin loading and lifecycle management. More...

#include <plugin_manager.h>

Classes

struct  Impl
 

Public Member Functions

 PluginManager (const PluginManager &)=delete
 Copy constructor (deleted)
 
PluginManageroperator= (const PluginManager &)=delete
 Copy assignment operator (deleted)
 
 PluginManager (PluginManager &&)=delete
 Move constructor (deleted)
 
PluginManageroperator= (PluginManager &&)=delete
 Move assignment operator (deleted)
 
void load (const std::filesystem::path &library_path) const
 Load a plugin from the specified library path.
 
void unload (const std::string &plugin_name) const
 Unload a plugin by name.
 
template<typename T>
T * get (const std::string &plugin_name)
 Get a type-safe pointer to a loaded plugin.
 

Static Public Member Functions

static PluginManagergetInstance ()
 

Private Member Functions

 PluginManager ()
 
 ~PluginManager ()
 
IPluginget_raw (const std::string &plugin_name) const
 Internal method to get raw plugin pointer without type checking.
 

Private Attributes

std::unique_ptr< Implpimpl
 Forward declaration for PIMPL implementation.
 

Detailed Description

Central manager for plugin loading and lifecycle management.

The PluginManager class provides a comprehensive interface for working with dynamically loaded plugins. It handles:

  • Loading plugins from shared library files
  • Managing plugin lifetimes and cleanup
  • Providing type-safe access to loaded plugins
  • Preventing name collisions between plugins

The manager uses the PIMPL idiom to hide implementation details and maintain ABI stability. It automatically handles proper cleanup of all loaded plugins when the manager is destroyed.

Note
This class is not copyable or movable to prevent issues with plugin handles
All plugin access is thread-safe at the manager level, but individual plugin instances may not be thread-safe

Constructor & Destructor Documentation

◆ PluginManager() [1/3]

fourdst::plugin::manager::PluginManager::PluginManager ( const PluginManager & )
delete

Copy constructor (deleted)

PluginManager instances cannot be copied due to the nature of shared library handles and plugin instance ownership.

◆ PluginManager() [2/3]

fourdst::plugin::manager::PluginManager::PluginManager ( PluginManager && )
delete

Move constructor (deleted)

PluginManager instances cannot be moved to maintain stability of plugin handles and prevent issues with library cleanup.

◆ PluginManager() [3/3]

fourdst::plugin::manager::PluginManager::PluginManager ( )
private

◆ ~PluginManager()

fourdst::plugin::manager::PluginManager::~PluginManager ( )
private

Member Function Documentation

◆ get()

template<typename T>
T * fourdst::plugin::manager::PluginManager::get ( const std::string & plugin_name)
inline

Get a type-safe pointer to a loaded plugin.

Retrieves a plugin by name and attempts to cast it to the specified type. The type must inherit from IPlugin and the plugin must have been loaded and must be compatible with the requested type.

Template Parameters
TThe plugin interface type to cast to (must inherit from IPlugin)
Parameters
plugin_nameThe name of the plugin to retrieve
Returns
T* A pointer to the plugin cast to the requested type
Exceptions
fourdst::plugin::exception::PluginNotLoadedErrorIf no plugin with the given name has been loaded
fourdst::plugin::exception::PluginTypeErrorIf the plugin cannot be cast to the requested type T
Note
The returned pointer remains valid until the plugin is unloaded or the manager is destroyed
The template parameter T is validated at compile-time to ensure it inherits from IPlugin

Example usage:

auto* my_plugin = manager.get<IMyPluginInterface>("my_plugin");
Definition plugin_manager.h:20

◆ get_raw()

IPlugin * fourdst::plugin::manager::PluginManager::get_raw ( const std::string & plugin_name) const
nodiscardprivate

Internal method to get raw plugin pointer without type checking.

Parameters
plugin_nameThe name of the plugin to retrieve
Returns
IPlugin* Raw pointer to the plugin, or nullptr if not found
Exceptions
Neverthrows

◆ getInstance()

manager::PluginManager & fourdst::plugin::manager::PluginManager::getInstance ( )
static

◆ load()

void fourdst::plugin::manager::PluginManager::load ( const std::filesystem::path & library_path) const

Load a plugin from the specified library path.

Attempts to load a shared library as a plugin. The library must export the required symbols (create_plugin and destroy_plugin) and the plugin must have a unique name within this manager instance.

Parameters
library_pathPath to the shared library file to load
Exceptions
fourdst::plugin::exception::PluginLoadErrorIf the library file cannot be found, opened, or if the plugin factory returns nullptr
fourdst::plugin::exception::PluginSymbolErrorIf the required symbols (create_plugin/destroy_plugin) cannot be found in the library
fourdst::plugin::exception::PluginNameCollisionErrorIf a plugin with the same name is already loaded
Note
The library_path can be absolute or relative to the current working directory
Once loaded, the plugin will remain in memory until explicitly unloaded or the manager is destroyed

◆ operator=() [1/2]

PluginManager & fourdst::plugin::manager::PluginManager::operator= ( const PluginManager & )
delete

Copy assignment operator (deleted)

PluginManager instances cannot be copy-assigned due to the nature of shared library handles and plugin instance ownership.

◆ operator=() [2/2]

PluginManager & fourdst::plugin::manager::PluginManager::operator= ( PluginManager && )
delete

Move assignment operator (deleted)

PluginManager instances cannot be move-assigned to maintain stability of plugin handles and prevent issues with library cleanup.

◆ unload()

void fourdst::plugin::manager::PluginManager::unload ( const std::string & plugin_name) const

Unload a plugin by name.

Removes the specified plugin from the manager, calls its destructor, and closes the associated shared library handle. If no plugin with the given name exists, this operation is a no-op.

Parameters
plugin_nameThe name of the plugin to unload
Exceptions
Neverthrows (safe to call with non-existent plugin names)
Note
After unloading, any pointers to the plugin instance become invalid
The plugin's destructor is guaranteed to be called before the library is closed

Member Data Documentation

◆ pimpl

std::unique_ptr<Impl> fourdst::plugin::manager::PluginManager::pimpl
private

Forward declaration for PIMPL implementation.

PIMPL pointer to hide implementation details


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