MaCh3  2.8.0
Reference Guide
DynamicPlugin.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 #include <dlfcn.h>
6 #include <iostream>
7 #include <memory>
8 #include <stdexcept>
9 #include <string>
10 #include "CLI/API/plugin.hpp"
11 
12 namespace M3{
18 class DynamicPlugin: public IPlugin{
19  public:
22  DynamicPlugin(const std::string& sofile)
23  : DynamicPlugin(sofile.c_str()) {}
24 
27  DynamicPlugin(const char* sofile)
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  }
94 
97  int Run() override {
98  return m_plugin->Run();
99  }
100 
106  return m_parser;
107  }
108 
110  virtual ~DynamicPlugin() = default;
111 
112  private:
113  static void dlcloser(void* h) {
114  if (h) dlclose(h);
115  }
117  // using dlcloser rather than built in dlclose to avoid gcc warning about casting function pointer to void pointer
118  std::unique_ptr<void, decltype(&dlcloser)> m_handle;
119  std::unique_ptr<IPlugin, destroy_plugin_t> m_plugin;
120 };
121 }
Manages the lifecycle of a dynamically loaded plugin.
MaCh3ArgumentParser * get_parser() override
Get the argument parser for this plugin.
virtual ~DynamicPlugin()=default
Destructor closes the shared library handle.
DynamicPlugin(const char *sofile)
Constructor that loads a plugin from a shared library file.
std::unique_ptr< IPlugin, destroy_plugin_t > m_plugin
Smart pointer to plugin with custom deleter.
int Run() override
Run the plugin's main functionality.
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.
DynamicPlugin(const std::string &sofile)
Constructor that loads a plugin from a shared library file.
Interface for MaCh3 plugins and modules.
Definition: plugin.hpp:19
Extended ArgumentParser with MaCh3-specific functionality.
Definition: m3argparse.hpp:17
Main namespace for MaCh3 software.
constexpr std::uint32_t PLUGIN_API_VERSION
Fix API compatibility: increment when the plugin/parser contract changes incompatibly.
Definition: plugin.hpp:12
Plugin interface and registration macros for MaCh3.
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