# 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++;
}
// -------------------------------------------------------------------------
~Interface( )
{
this->UnloadAll( );
+ Self::InterfacesCount--;
+ if( Self::InterfacesCount == 0 )
+ {
+ // TODO: unload vtk and itk
+
+ } // fi
}
// -------------------------------------------------------------------------
);
// 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
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 );
}