Agnostic plugin loader.
Introduction to plugins in C++
- Todo:
- Handle (multiple git repos in) MetaInfo (#99)
Introduction to plugins in C++
- Todo:
- Handle (multiple git repos in) MetaInfo (#99)
#include <PluginLoader.h>
◆ PluginLoader() [1/4]
Standard constructor.
Runs dlopen()
and dlsym()
, creates T, and throws if anything looks bad.
- Note
- Use the
DARWIN_EXPORT_PLUGIN()
macro to provide the function used by dlsym
to instantiate the plugin.
- Note
dlopen
checks that the plugin exists.
- Parameters
-
file | path to .so or .dylib file |
63 cout <<
"PluginLoader: Loading plugin from " <<
file << endl;
68 string what =
file.string() +
" could not be loaded"s;
69 const char * error = dlerror();
71 what +=
": "s + error;
72 BOOST_THROW_EXCEPTION( invalid_argument(what) );
75 typedef vector<unique_ptr<IPlugin>> * create_t();
78 string what =
"`CreatePlugin` could not be found"s;
79 const char * error = dlerror();
81 what +=
": "s + error;
82 BOOST_THROW_EXCEPTION( runtime_error(what) );
85 if (iplugins ==
nullptr)
86 BOOST_THROW_EXCEPTION( runtime_error(
"`CreatePlugin` returns a nullptr") );
90 catch (boost::exception& e) {
◆ PluginLoader() [2/4]
Move constructor.
99 ,
plugins(std::move(other.plugins))
100 ,
handle(std::move(other.handle))
105 cout <<
"PluginLoader: Moving plugin from " <<
file << endl;
108 other.plugins.clear();
109 other.handle =
nullptr;
◆ ~PluginLoader() [1/2]
Closes the plugin handle.
119 cout <<
"PluginLoader: Closing plugin from " <<
file << endl;
124 int code = dlclose(
handle);
127 <<
" closed with error " << code <<
def <<
'\n';
◆ PluginLoader() [3/4]
Standard constructor.
Runs dlopen()
and dlsym()
, creates T, and throws if anything looks bad.
- Note
- Use the
DARWIN_EXPORT_PLUGIN()
macro to provide the function used by dlsym
to instantiate the plugin.
- Note
dlopen
checks that the plugin exists.
- Parameters
-
file | path to .so or .dylib file |
63 cout <<
"PluginLoader: Loading plugin from " <<
file << endl;
68 string what =
file.string() +
" could not be loaded"s;
69 const char * error = dlerror();
71 what +=
": "s + error;
72 BOOST_THROW_EXCEPTION( invalid_argument(what) );
75 typedef vector<unique_ptr<IPlugin>> * create_t();
78 string what =
"`CreatePlugin` could not be found"s;
79 const char * error = dlerror();
81 what +=
": "s + error;
82 BOOST_THROW_EXCEPTION( runtime_error(what) );
85 if (iplugins ==
nullptr)
86 BOOST_THROW_EXCEPTION( runtime_error(
"`CreatePlugin` returns a nullptr") );
90 catch (boost::exception& e) {
◆ PluginLoader() [4/4]
Move constructor.
99 ,
plugins(std::move(other.plugins))
100 ,
handle(std::move(other.handle))
105 cout <<
"PluginLoader: Moving plugin from " <<
file << endl;
108 other.plugins.clear();
109 other.handle =
nullptr;
◆ ~PluginLoader() [2/2]
Closes the plugin handle.
119 cout <<
"PluginLoader: Closing plugin from " <<
file << endl;
124 int code = dlclose(
handle);
127 <<
" closed with error " << code <<
def <<
'\n';
◆ Get() [1/2]
Access to plugin's raw pointers.
139 auto convert = [
this](IPlugin *
plugin) ->
P * {
140 auto ptr =
dynamic_cast<P*
>(
plugin);
141 if (ptr ==
nullptr) {
142 string what =
"Invalid plugin type for ";
144 BOOST_THROW_EXCEPTION( logic_error(what) );
148 return plugins | views::transform(&unique_ptr<IPlugin>::get)
149 | views::transform(convert);
◆ Get() [2/2]
Access to plugin's raw pointers.
139 auto convert = [
this](IPlugin *
plugin) ->
P * {
140 auto ptr =
dynamic_cast<P*
>(
plugin);
141 if (ptr ==
nullptr) {
142 string what =
"Invalid plugin type for ";
144 BOOST_THROW_EXCEPTION( logic_error(what) );
148 return plugins | views::transform(&unique_ptr<IPlugin>::get)
149 | views::transform(convert);
◆ Which() [1/2]
std::filesystem::path Which |
( |
| ) |
const |
|
inline |
◆ Which() [2/2]
std::filesystem::path Which |
( |
| ) |
const |
|
inline |
◆ file
std::filesystem::path file |
|
private |
◆ handle
pointer to object return by dlopen()
◆ plugins
std::vector< std::unique_ptr< IPlugin > > plugins |
|
private |
pointers to function return from the function called by dlopen
◆ steering
The documentation for this class was generated from the following file:
- /builds/cms-analysis/general/DasAnalysisSystem/Core/Installer/Darwin/interface/PluginLoader.h