]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface.cxx
Bug smashed like a boss
[cpPlugins.git] / lib / cpPlugins / Interface.cxx
index 8b5e106e5791c29d697ab123f414c56359248818..6ffc0fe462ae5b033b3cf64f7fe1ba900b27399f 100644 (file)
@@ -6,13 +6,42 @@
 #  include <dlfcn.h>
 #endif // cpPlugins_SYS_WINDOWS
 #include <cpPlugins_dirent.h>
+#include <cpPlugins/cpPlugins_DynLibs.h>
 #include <algorithm>
 
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Interface::InterfacesCount = 0;
+
 // -------------------------------------------------------------------------
 cpPlugins::Interface::
 Interface( )
 {
   this->UpdatePaths( );
+
+  // Explicitly load all ITK and VTK
+  if( Self::InterfacesCount == 0 )
+  {
+    std::vector< std::string > libs;
+    cpPlugins::TokenizeString( libs, cpPlugins_DynLibs, ";" );
+    this->_AddInstancesLib( libs, cpPlugins_CompilationDir );
+    this->_AddInstancesLib( libs, cpPlugins_InstallationDir );
+
+    for( auto l = libs.begin( ); l != libs.end( ); ++l )
+    {
+      std::string error = "";
+      void* hnd = Self::_DLOpen( *l, error );
+      if( hnd == NULL || error != "" )
+        throw std::runtime_error(
+          std::string( "cpPlugins::Interface: Could not load library \"" ) +
+          *l +
+          std::string( "\": " ) +
+          error
+          );
+
+    } // rof
+
+  } // fi
+  Self::InterfacesCount++;
 }
 
 // -------------------------------------------------------------------------
@@ -20,6 +49,12 @@ cpPlugins::Interface::
 ~Interface( )
 {
   this->UnloadAll( );
+  Self::InterfacesCount--;
+  if( Self::InterfacesCount == 0 )
+  {
+    // TODO: unload vtk and itk
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
@@ -126,12 +161,14 @@ LoadPluginFile( const std::string& filename )
       );
 
   // Try to load the library
-  void* hnd = Self::_DLOpen( canonical );
-  if( hnd == NULL )
+  std::string error;
+  void* hnd = Self::_DLOpen( canonical, error );
+  if( hnd == NULL || error != "" )
     throw std::runtime_error(
       std::string( "cpPlugins::Interface: Could not load library \"" ) +
       filename +
-      std::string( "\"" )
+      std::string( "\": " ) +
+      error
       );
 
   // Get plugin name
@@ -264,16 +301,40 @@ GetPlugins( ) const
   return( res );
 }
 
+// -------------------------------------------------------------------------
+template< class _TList >
+void cpPlugins::Interface::
+_AddInstancesLib( _TList& libs, const std::string& path )
+{
+  DIR* dir;
+  struct dirent* ent;
+  if( ( dir = opendir( path.c_str( ) ) ) != NULL )
+  {
+    while( ( ent = readdir( dir ) ) != NULL )
+    {
+      std::string fname = path + std::string( "/" ) + ent->d_name;
+      if( fname.find( "cpPlugins_Instances_" ) != std::string::npos )
+        libs.push_back( fname );
+
+    } // elihw
+    closedir( dir );
+
+  } // fi
+}
+
 // -------------------------------------------------------------------------
 void* cpPlugins::Interface::
-_DLOpen( const std::string& fname )
+_DLOpen( const std::string& fname, std::string& error )
 {
   void* hnd = NULL;
 #ifdef cpPlugins_SYS_WINDOWS
   hnd = ::LoadLibraryA( fname.c_str( ) );
 #else // cpPlugins_SYS_WINDOWS
   hnd = dlopen( fname.c_str( ), RTLD_NOW | RTLD_GLOBAL );
-  dlerror( );
+  if( hnd == NULL )
+    error = dlerror( );
+  else
+    dlerror( );
 #endif // cpPlugins_SYS_WINDOWS
   return( hnd );
 }