MaCh3  2.8.0
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...
 
MaCh3ArgumentParser * get_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

MaCh3ArgumentParser * m_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_t > m_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 18 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 22 of file DynamicPlugin.hpp.

23  : 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 27 of file DynamicPlugin.hpp.

28  : m_handle(nullptr, &DynamicPlugin::dlcloser),
29  m_plugin(nullptr, nullptr){
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  try{
37  // Fix API compatibility: reject legacy/mismatched plugins before calling their factory.
38  auto version = reinterpret_cast<plugin_api_version_t>(dlsym(m_handle.get(), "plugin_api_version"));
39  if (!version) {
40  throw std::runtime_error("Missing plugin_api_version function; rebuild the plugin with the current MaCh3 SDK");
41  }
42  const auto plugin_version = version();
43  if (plugin_version != PLUGIN_API_VERSION) {
44  throw std::runtime_error("Plugin API version " + std::to_string(plugin_version) +
45  " does not match host version " + std::to_string(PLUGIN_API_VERSION) +
46  "; rebuild the plugin with the current MaCh3 SDK");
47  }
48  }
49  catch (const std::exception& err){
50  // Fix plugin lifetime: translate exceptions before unloading plugin-defined exception code.
51  throw std::runtime_error("Cannot initialise plugin '" + std::string(sofile) + "': " + err.what());
52  }
53  catch (...){
54  throw std::runtime_error("Cannot initialise plugin '" + std::string(sofile) + "': unknown exception");
55  }
56 
57  auto create = reinterpret_cast<create_plugin_t>(dlsym(m_handle.get(), "create_plugin"));
58  auto destroy = reinterpret_cast<destroy_plugin_t>(dlsym(m_handle.get(), "destroy_plugin"));
59 
60  if (!create || !destroy) {
61  std::cerr << "Invalid plugin: " << sofile << "\n";
62  throw std::runtime_error("Invalid plugin - missing create_plugin or destroy_plugin");
63  }
64 
65  try{
66  m_plugin = std::unique_ptr<IPlugin, destroy_plugin_t>(create(), destroy);
67  if (!m_plugin){
68  throw std::runtime_error("Plugin factory returned a null pointer");
69  }
70  }
71  catch (const std::exception& err){
72  throw std::runtime_error("Error instantiating plugin '" + std::string(sofile) + "': " + err.what());
73  }
74  catch (...){
75  std::cerr << "Error instantiating plugin: " << sofile << "\n";
76  throw std::runtime_error("Error instantiating plugin '" + std::string(sofile) + "': unknown exception");
77  }
78 
79  try{
80  m_parser = m_plugin->get_parser();
81  if (!m_parser){
82  throw std::runtime_error("Plugin returned a null parser");
83  }
84  }
85  catch (const std::exception& err){
86  // Fix plugin lifetime: translate exceptions before unloading plugin-defined exception code.
87  throw std::runtime_error("Error retrieving parser '" + std::string(sofile) + "': " + err.what());
88  }
89  catch (...){
90  std::cerr<< "Error retrieving parser" << "\n";
91  throw std::runtime_error("Error retrieving parser '" + std::string(sofile) + "': unknown exception");
92  }
93  }
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.
constexpr std::uint32_t PLUGIN_API_VERSION
Fix API compatibility: increment when the plugin/parser contract changes incompatibly.
Definition: plugin.hpp:12
std::uint32_t(* plugin_api_version_t)()
Function pointer type for the plugin API-version query.
Definition: plugin.hpp:57
M3::IPlugin *(* create_plugin_t)()
Function pointer type for the plugin factory.
Definition: plugin.hpp:60
void(* destroy_plugin_t)(M3::IPlugin *)
Function pointer type for plugin destructor function.
Definition: plugin.hpp:64

◆ ~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 113 of file DynamicPlugin.hpp.

113  {
114  if (h) dlclose(h);
115  }

◆ 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 105 of file DynamicPlugin.hpp.

105  {
106  return m_parser;
107  }

◆ Run()

int M3::DynamicPlugin::Run ( )
inlineoverridevirtual

Run the plugin's main functionality.

Returns
Exit code from the plugin

Implements M3::IPlugin.

Definition at line 97 of file DynamicPlugin.hpp.

97  {
98  return m_plugin->Run();
99  }

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 118 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 116 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 119 of file DynamicPlugin.hpp.


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