Field3D
PluginLoader Class Reference

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

#include <PluginLoader.h>

List of all members.

Public Member Functions

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

Static Public Member Functions

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

Static Private Attributes

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

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.

{

}
PluginLoader::~PluginLoader ( )

Destructor.

Definition at line 156 of file PluginLoader.cpp.

{

}

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().

{
  // Get environment variable
  char *cptr = getenv("FIELD3D_DSO_PATH");
  if (!cptr)
    return;

  std::string path = cptr;
  
  // Split paths
  std::vector<std::string> paths;
  const std::string delimiters = ":";
  
  tokenize(path, delimiters, paths);

  // For each path
  for (unsigned int i = 0; i < paths.size(); i++) {

    // List the contents of the directory
    std::vector<std::string> sos;
    if (!getDirSos(sos,paths[i])) {
      continue;
    }
    
    // Open each file
    for (unsigned int j = 0; j < sos.size(); j++) {
      std::string sofile = sos[j];
      
      //First check to see if a plugin of the same name has already been loaded
      const std::string pathDelimiter = "/";
      std::vector<std::string> pluginName;
      tokenize(sofile, pathDelimiter, pluginName);

      bool pluginAlreadyLoaded = false;

      for (unsigned int i = 0; i < ms_pluginsLoaded.size(); i++) {
        if (pluginName.size() > 0) {
          if (ms_pluginsLoaded[i] == pluginName[pluginName.size() - 1]) {
            //This plugin has been loaded so look for another one
            //std::cout << ms_pluginsLoaded[i] << " is already loaded\n";
            pluginAlreadyLoaded = true;
            break;
          } 
        }
      }
    
      if (pluginAlreadyLoaded) {
        continue;
      }
      
      if (pluginName.size() > 0) {
        std::string lastName = pluginName[pluginName.size() -1];
        ms_pluginsLoaded.push_back(lastName);
      }
      

      // Attempt to load .so file
      void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
      if (!handle) {
        std::cout <<
          "Field3D Plugin loader: failed to load plugin: " << dlerror() << "\n";
        continue;
      }

      // Determine plugin type by looking for one of:
      //   registerField3DPlugin()

      int (*fptr)(ClassFactory &) = NULL;
      fptr = (int (*)(ClassFactory&))
        dlsym(handle,"registerField3DPlugin");
      std::string msg = "Initialized Field3D Plugin " + sofile;

      char *dlsymError = dlerror();
      if (!dlsymError) {
        if (fptr) {
          // Call the registration function
          int res = (*fptr)(ClassFactory::singleton());
          if (!res) {
            Msg::print(Msg::SevWarning,
                      "failed to init Field3D plugin " + sofile);
          } else {
            Msg::print(msg);
          }
        }
      } else {
        char *debugEnvVar = getenv("FIELD3D_DEBUG");
        if (debugEnvVar) {
          // debug env var exist, so print warning
          Msg::print(Msg::SevWarning,
                     "Field3D plugin loader: failed to load "
                     "the symbol registerField3DPlugin");
        }
      }
    }
  }
}

Member Data Documentation

FIELD3D_NAMESPACE_OPEN std::vector< std::string > PluginLoader::ms_pluginsLoaded [static, private]

List of plugins loaded.

Definition at line 96 of file PluginLoader.h.


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