Field3D
PluginLoader Class Reference

This class provides methods for loading Field plugins from disk. More...

#include <PluginLoader.h>

Public Member Functions

 PluginLoader ()
 Default constructor. More...
 
 ~PluginLoader ()
 Destructor. More...
 

Static Public Member Functions

static void loadPlugins ()
 Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds. More...
 

Static Private Attributes

static std::vector< std::string > ms_pluginsLoaded
 List of plugins loaded. More...
 

Detailed Description

This class provides methods for loading Field plugins from disk.

Todo:
Look into adding maya-style single-plugin load and unload functions

Definition at line 68 of file PluginLoader.h.

Constructor & Destructor Documentation

PluginLoader::PluginLoader ( )

Default constructor.

Definition at line 149 of file PluginLoader.cpp.

150 {
151 
152 }
PluginLoader::~PluginLoader ( )

Destructor.

Definition at line 156 of file PluginLoader.cpp.

157 {
158 
159 }

Member Function Documentation

void PluginLoader::loadPlugins ( )
static

Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds.

Definition at line 163 of file PluginLoader.cpp.

References getDirSos(), Msg::print(), Msg::SevWarning, and ClassFactory::singleton().

Referenced by ClassFactory::ClassFactory().

164 {
165  // Get environment variable
166  char *cptr = getenv("FIELD3D_DSO_PATH");
167  if (!cptr)
168  return;
169 
170  std::string path = cptr;
171 
172  // Split paths
173  std::vector<std::string> paths;
174  const std::string delimiters = ":";
175 
176  tokenize(path, delimiters, paths);
177 
178  // For each path
179  for (unsigned int i = 0; i < paths.size(); i++) {
180 
181  // List the contents of the directory
182  std::vector<std::string> sos;
183  if (!getDirSos(sos,paths[i])) {
184  continue;
185  }
186 
187  // Open each file
188  for (unsigned int j = 0; j < sos.size(); j++) {
189  std::string sofile = sos[j];
190 
191  //First check to see if a plugin of the same name has already been loaded
192  const std::string pathDelimiter = "/";
193  std::vector<std::string> pluginName;
194  tokenize(sofile, pathDelimiter, pluginName);
195 
196  bool pluginAlreadyLoaded = false;
197 
198  for (unsigned int i = 0; i < ms_pluginsLoaded.size(); i++) {
199  if (pluginName.size() > 0) {
200  if (ms_pluginsLoaded[i] == pluginName[pluginName.size() - 1]) {
201  //This plugin has been loaded so look for another one
202  //std::cout << ms_pluginsLoaded[i] << " is already loaded\n";
203  pluginAlreadyLoaded = true;
204  break;
205  }
206  }
207  }
208 
209  if (pluginAlreadyLoaded) {
210  continue;
211  }
212 
213  if (pluginName.size() > 0) {
214  std::string lastName = pluginName[pluginName.size() -1];
215  ms_pluginsLoaded.push_back(lastName);
216  }
217 
218 
219  // Attempt to load .so file
220  void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
221  if (!handle) {
222  std::cout <<
223  "Field3D Plugin loader: failed to load plugin: " << dlerror() << "\n";
224  continue;
225  }
226 
227  // Determine plugin type by looking for one of:
228  // registerField3DPlugin()
229 
230  int (*fptr)(ClassFactory &) = NULL;
231  fptr = (int (*)(ClassFactory&))
232  dlsym(handle,"registerField3DPlugin");
233  std::string msg = "Initialized Field3D Plugin " + sofile;
234 
235  char *dlsymError = dlerror();
236  if (!dlsymError) {
237  if (fptr) {
238  // Call the registration function
239  int res = (*fptr)(ClassFactory::singleton());
240  if (!res) {
242  "failed to init Field3D plugin " + sofile);
243  } else {
244  Msg::print(msg);
245  }
246  }
247  } else {
248  char *debugEnvVar = getenv("FIELD3D_DEBUG");
249  if (debugEnvVar) {
250  // debug env var exist, so print warning
252  "Field3D plugin loader: failed to load "
253  "the symbol registerField3DPlugin");
254  }
255  }
256  }
257  }
258 }
static ClassFactory & singleton()
}
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
static std::vector< std::string > ms_pluginsLoaded
List of plugins loaded.
Definition: PluginLoader.h:96
bool getDirSos(std::vector< std::string > &sos, std::string &dir)

Member Data Documentation

FIELD3D_NAMESPACE_OPEN std::vector< std::string > PluginLoader::ms_pluginsLoaded
staticprivate

List of plugins loaded.

Definition at line 96 of file PluginLoader.h.


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