Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
WireCell::PluginManager Class Reference

#include <PluginManager.h>

Public Member Functions

Pluginadd (const std::string &plugin_name, const std::string &libname="")
 Add a plugin. If libname is not given, try to derive it. More...
 
Pluginget (const std::string &plugin_name)
 
Pluginfind (const std::string &symbol_name)
 

Static Public Member Functions

static PluginManagerinstance ()
 

Private Member Functions

 PluginManager ()
 
 ~PluginManager ()
 

Private Attributes

Log::logptr_t l
 
std::map< std::string, Plugin * > m_plugins
 

Detailed Description

This is meant to be used from a WireCell::Singleton.

Definition at line 32 of file PluginManager.h.

Constructor & Destructor Documentation

WireCell::PluginManager::PluginManager ( )
private

Definition at line 91 of file PluginManager.cxx.

92  : l(Log::logger("sys"))
93 {
94 }
logptr_t logger(std::string name)
Definition: Logging.cxx:71
WireCell::PluginManager::~PluginManager ( )
private

Definition at line 95 of file PluginManager.cxx.

96 {
97  for (auto pit : m_plugins) {
98  delete pit.second;
99  pit.second = nullptr;
100  }
101 }
std::map< std::string, Plugin * > m_plugins
Definition: PluginManager.h:46

Member Function Documentation

WireCell::Plugin * WireCell::PluginManager::add ( const std::string plugin_name,
const std::string libname = "" 
)

Add a plugin. If libname is not given, try to derive it.

Definition at line 32 of file PluginManager.cxx.

34 {
35  Plugin* plugin = get(plugin_name);
36  if (plugin) {
37  l->debug("already have plugin {}", plugin_name);
38  return plugin;
39  }
40 
41  std::string exts[2] = {".so",".dylib"};
42  for (int ind=0; ind<2; ++ind) {
43  std::string ext = exts[ind];
44  string lname = "";
45  if (libname == "") {
46  lname = "lib";
47  lname += plugin_name;
48  lname += ext;
49  }
50  else {
51  lname = libname;
52  }
53  void* lib = dlopen(lname.c_str(), RTLD_NOW);
54  if (!lib) {
55  l->error("Failed to load {}: {}", lname, dlerror());
56  continue;
57  }
58 
59  m_plugins[plugin_name] = new Plugin(lib);
60  l->debug("loaded plugin #{} \"{}\" from library \"{}\": {}",
61  m_plugins.size(), plugin_name, lname,
62  (void*)m_plugins[plugin_name]);
63  return m_plugins[plugin_name];
64 
65  }
66  l->critical("no such plugin: \"{}\"", plugin_name);
67  THROW(IOError() << errmsg{"no such plugin: " + plugin_name});
68  return nullptr;
69 }
Thrown when an error involving accessing input or output has occurred.
Definition: Exceptions.h:46
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
std::map< std::string, Plugin * > m_plugins
Definition: PluginManager.h:46
#define THROW(e)
Definition: Exceptions.h:25
WireCell::Plugin * WireCell::PluginManager::find ( const std::string symbol_name)

Definition at line 80 of file PluginManager.cxx.

81 {
82  for (auto pit : m_plugins) {
83  Plugin* maybe = pit.second;
84  if (maybe->contains(symbol_name)) {
85  return maybe;
86  }
87  }
88  return nullptr;
89 }
bool contains(const std::string &symbol_name)
std::map< std::string, Plugin * > m_plugins
Definition: PluginManager.h:46
WireCell::Plugin * WireCell::PluginManager::get ( const std::string plugin_name)

Definition at line 71 of file PluginManager.cxx.

72 {
73  auto pit = m_plugins.find(plugin_name);
74  if (pit == m_plugins.end()) {
75  return nullptr;
76  }
77  return pit->second;
78 }
std::map< std::string, Plugin * > m_plugins
Definition: PluginManager.h:46
PluginManager & WireCell::PluginManager::instance ( )
static

Definition at line 26 of file PluginManager.cxx.

27 {
28  static PluginManager inst;
29  return inst;
30 }

Member Data Documentation

Log::logptr_t WireCell::PluginManager::l
private

Definition at line 33 of file PluginManager.h.

std::map<std::string, Plugin*> WireCell::PluginManager::m_plugins
private

Definition at line 46 of file PluginManager.h.


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