SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
PluginLoader.cc
Go to the documentation of this file.
1 #include "PluginLoader.hh"
2 
3 #include <dlfcn.h>
4 
5 namespace slic {
6 
7 UserActionPlugin* PluginLoader::create(std::string pluginName, std::string libName) {
8 
9  // Open a handle to the specific dynamic lib.
10  void* handle = dlopen(libName.c_str(), RTLD_LAZY);
11 
12  // Plugin to be created.
13  UserActionPlugin* plugin = nullptr;
14 
15  // Is there a proper handle to the lib?
16  if (handle) {
17 
18  // Get a function pointer from the library to create the plugin object.
19  UserActionPlugin* (*createIt)();
20  createIt = (UserActionPlugin* (*)())dlsym(handle, std::string("create" + pluginName).c_str());
21 
22  // Did we get back a good function pointer from the lib?
23  if(createIt) {
24 
25  // Create the plugin object from the function pointer.
26  plugin = (UserActionPlugin*)createIt();
27 
28  // Was the plugin created successfully?
29  if (plugin) {
30  // Register the plugin handle for when it needs to be destroyed.
31  this->pluginHandles_[plugin] = handle;
32  } else {
33  // For some reason, the plugin could not be created by the lib function!
34  std::cerr << "[ PluginLoader ] : Failed to create plugin " << pluginName << " from lib "
35  << libName << "!!!" << std::endl;
36  throw std::runtime_error("Failed to create the plugin.");
37  }
38  } else {
39  // The create function was not found in the lib.
40  std::cerr << "[ PluginLoader ] : Failed to find create function for plugin " << pluginName << " in lib "
41  << libName << "!!!" << std::endl;
42  throw std::runtime_error("Failed to find library function for creating plugin.");
43  }
44  } else {
45  // The plugin lib could not be opened. Probably it doesn't exist!
46  std::cerr << "[ PluginLoader ] : Could not open " << libName << " plugin lib." << std::endl;
47  throw std::runtime_error("Plugin lib not found.");
48  }
49 
50  return plugin;
51 }
52 
54 
55  // Is the plugin argument non-null?
56  if (plugin) {
57 
58  // Get the lib handle for the plugin.
59  void* handle = this->pluginHandles_[plugin];
60 
61  // Do we have a lib handle for this plugin?
62  if (handle) {
63 
64  // Destroy the plugin by calling its destroy method from the lib.
65  void (*destroyIt)(UserActionPlugin*);
66  destroyIt = (void (*)(UserActionPlugin*))dlsym(handle, std::string("destroy" + plugin->getName()).c_str());
67  destroyIt(plugin);
68 
69  // Delete from plugin handles.
70  std::map<UserActionPlugin*, void*>::iterator it;
71  it = this->pluginHandles_.find(plugin);
72  if (it != pluginHandles_.end()) {
73  this->pluginHandles_.erase(it);
74  }
75  } else {
76  // No handle exists for this plugin. Maybe it wasn't registered with this PluginLoader?
77  std::cerr << "[ PluginLoader ] : Failed to find handle for " << plugin->getName() << " plugin!!!" << std::endl;
78  throw std::runtime_error("Failed to find handle for plugin.");
79  }
80  } else {
81  // Null argument is just ignored with a warning.
82  std::cerr << "[ PluginLoader ] : Ignoring null plugin argument. " << std::endl;
83  }
84 }
85 
86 }
UserActionPlugin * create(std::string pluginName, std::string libName)
Definition: PluginLoader.cc:7
std::map< UserActionPlugin *, void * > pluginHandles_
Definition: PluginLoader.hh:50
void destroy(UserActionPlugin *plugin)
Definition: PluginLoader.cc:53
User simulation plugin.
virtual std::string getName()=0
@ endl
Definition: LogStream.hh:26