MaCh3  2.6.1
Reference Guide
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
M3::DynamicPlugin Class Reference

Manages the lifecycle of a dynamically loaded plugin. More...

#include <CLI/DynamicPlugin.hpp>

Inheritance diagram for M3::DynamicPlugin:
[legend]
Collaboration diagram for M3::DynamicPlugin:
[legend]

Public Member Functions

 DynamicPlugin (const std::string &sofile)
 Constructor that loads a plugin from a shared library file. More...
 
 DynamicPlugin (const char *sofile)
 Constructor that loads a plugin from a shared library file. More...
 
int Run () override
 Run the plugin's main functionality. More...
 
MaCh3ArgumentParserget_parser () override
 Get the argument parser for this plugin. More...
 
virtual ~DynamicPlugin ()=default
 Destructor closes the shared library handle. More...
 
- Public Member Functions inherited from M3::IPlugin
virtual ~IPlugin ()=default
 

Static Private Member Functions

static void dlcloser (void *h)
 

Private Attributes

MaCh3ArgumentParserm_parser = nullptr
 Pointer to the plugin's argument parser, owned by the shared library. More...
 
std::unique_ptr< void, decltype(&dlcloser)> m_handle
 Handle to the loaded shared library. More...
 
std::unique_ptr< IPlugin, destroy_plugin_tm_plugin
 Smart pointer to plugin with custom deleter. More...
 

Detailed Description

Manages the lifecycle of a dynamically loaded plugin.

This class wraps a plugin loaded from a shared library, maintaining the library handle and providing cleanup functionality using smart pointers.

Definition at line 16 of file DynamicPlugin.hpp.

Constructor & Destructor Documentation

◆ DynamicPlugin() [1/2]

M3::DynamicPlugin::DynamicPlugin ( const std::string &  sofile)
inline

Constructor that loads a plugin from a shared library file.

Parameters
sofilePath to the shared library file (.so)

Definition at line 20 of file DynamicPlugin.hpp.

21  : DynamicPlugin(sofile.c_str()) {}
DynamicPlugin(const std::string &sofile)
Constructor that loads a plugin from a shared library file.

◆ DynamicPlugin() [2/2]

M3::DynamicPlugin::DynamicPlugin ( const char *  sofile)
inline

Constructor that loads a plugin from a shared library file.

Parameters
sofilePath to the shared library file (.so)

Definition at line 25 of file DynamicPlugin.hpp.

26  : m_handle(nullptr, &DynamicPlugin::dlcloser),
27  m_plugin(nullptr, nullptr){
28 
29 
30  m_handle.reset(dlopen(sofile, RTLD_NOW));
31  if (!m_handle) {
32  std::cerr << "dlopen failed - shared library '"<< sofile << "' not loaded: " << dlerror() << "\n";
33  throw std::runtime_error("dlopen failed");
34  }
35 
36  auto create = reinterpret_cast<create_plugin_t>(dlsym(m_handle.get(), "create_plugin"));
37  auto destroy = reinterpret_cast<destroy_plugin_t>(dlsym(m_handle.get(), "destroy_plugin"));
38 
39  if (!create || !destroy) {
40  std::cerr << "Invalid plugin: " << sofile << "\n";
41  m_handle.reset(nullptr);
42  throw std::runtime_error("Invalid plugin - missing create_plugin or destroy_plugin");
43  }
44 
45  try{
46  m_plugin = std::unique_ptr<IPlugin, destroy_plugin_t>(create(), destroy);
47  if (!m_plugin){
48  throw std::runtime_error("null pointer");
49  }
50  }
51  catch (...){
52  std::cerr << "Error instantiating plugin: " << sofile << "\n";
53  m_handle.reset(nullptr);
54  throw std::runtime_error("Error instantiating plugin");
55  }
56 
57  try{
58  m_parser = m_plugin->get_parser();
59  if (!m_parser){
60  throw std::runtime_error("null pointer");
61  }
62  }
63  catch(...){
64  std::cerr<< "Error retrieving parser" << "\n";
65  m_plugin.reset(nullptr);
66  m_handle.reset(nullptr);
67  throw std::runtime_error("Error retrieving parser");
68  }
69  }
std::unique_ptr< IPlugin, destroy_plugin_t > m_plugin
Smart pointer to plugin with custom deleter.
static void dlcloser(void *h)
MaCh3ArgumentParser * m_parser
Pointer to the plugin's argument parser, owned by the shared library.
std::unique_ptr< void, decltype(&dlcloser)> m_handle
Handle to the loaded shared library.
M3::IPlugin *(* create_plugin_t)()
Function pointer type for plugin factory function.
Definition: plugin.hpp:58
void(* destroy_plugin_t)(M3::IPlugin *)
Function pointer type for plugin destructor function.
Definition: plugin.hpp:62

◆ ~DynamicPlugin()

virtual M3::DynamicPlugin::~DynamicPlugin ( )
virtualdefault

Destructor closes the shared library handle.

Member Function Documentation

◆ dlcloser()

static void M3::DynamicPlugin::dlcloser ( void *  h)
inlinestaticprivate

Definition at line 90 of file DynamicPlugin.hpp.

90  {
91  if (h) dlclose(h);
92  }

◆ get_parser()

MaCh3ArgumentParser* M3::DynamicPlugin::get_parser ( )
inlineoverridevirtual

Get the argument parser for this plugin.

Returns
Pointer to the plugin's argument parser

pointer is owned by the shared library and will be deleted when the library is unloaded

Implements M3::IPlugin.

Definition at line 81 of file DynamicPlugin.hpp.

81  {
82  return m_parser;
83  }

◆ Run()

int M3::DynamicPlugin::Run ( )
inlineoverridevirtual

Run the plugin's main functionality.

Returns
Exit code from the plugin

Implements M3::IPlugin.

Definition at line 73 of file DynamicPlugin.hpp.

73  {
74  return m_plugin->Run();
75  }

Member Data Documentation

◆ m_handle

std::unique_ptr<void, decltype(&dlcloser)> M3::DynamicPlugin::m_handle
private

Handle to the loaded shared library.

Definition at line 95 of file DynamicPlugin.hpp.

◆ m_parser

MaCh3ArgumentParser* M3::DynamicPlugin::m_parser = nullptr
private

Pointer to the plugin's argument parser, owned by the shared library.

Definition at line 93 of file DynamicPlugin.hpp.

◆ m_plugin

std::unique_ptr<IPlugin, destroy_plugin_t> M3::DynamicPlugin::m_plugin
private

Smart pointer to plugin with custom deleter.

Definition at line 96 of file DynamicPlugin.hpp.


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