1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
5 #include <cpPlugins/Library.h>
9 #ifdef cpPlugins_OS_Windows
10 #else // cpPlugins_OS_Windows
12 #endif // cpPlugins_OS_Windows
13 #include <boost/filesystem/operations.hpp>
14 #include <cpPlugins/ProcessObject.h>
16 // -------------------------------------------------------------------------
18 Library( const std::string& fname )
23 namespace bf = boost::filesystem;
25 // Set and check (via exceptions) related files
26 bf::path cname = bf::canonical( fname ).string( );
27 std::string name = cname.filename( ).string( );
30 << cname.parent_path( ).string( )
31 << bf::path::preferred_separator
32 << cpPlugins_LIB_PREFIX
33 << name.substr( 0, name.size( ) - cname.extension( ).string( ).size( ) )
35 this->m_Path = bf::canonical( n.str( ) ).string( );
36 this->m_Name = cname.string( );
38 // Load library contents
39 std::ifstream in( this->m_Name.c_str( ) );
42 << "Could not load plugins file \"" << this->m_Name << "\""
44 typedef std::istreambuf_iterator< char > _TDIt;
45 std::istringstream str( std::string( ( _TDIt( in ) ), _TDIt( ) ) );
46 std::string buffer = str.str( );
48 str = std::istringstream( buffer );
50 while( std::getline( str, line ) )
52 this->m_Contents.insert( line );
55 // -------------------------------------------------------------------------
61 // -------------------------------------------------------------------------
62 bool cpPlugins::Library::
63 Provides( const std::string& fname ) const
65 return( this->m_Contents.find( fname ) != this->m_Contents.end( ) );
68 // -------------------------------------------------------------------------
69 const cpPlugins::Library::
70 TStringSet& cpPlugins::Library::
73 return( this->m_Contents );
76 // -------------------------------------------------------------------------
77 std::shared_ptr< cpPlugins::ProcessObject > cpPlugins::Library::
78 Create( const std::string& name )
80 typedef std::shared_ptr< cpPlugins::ProcessObject > _TPtr;
82 if( this->m_Handle == NULL )
83 this->_LoadLibrary( );
84 _TPtr* ptr = reinterpret_cast< _TPtr* >( this->_CreateObject( name ) );
88 // -------------------------------------------------------------------------
89 const std::string& cpPlugins::Library::
92 return( this->m_Path );
95 // -------------------------------------------------------------------------
96 void cpPlugins::Library::
99 #ifdef cpPlugins_OS_Windows
100 #else // cpPlugins_OS_Windows
101 this->m_Handle = dlopen( this->m_Path.c_str( ), RTLD_LAZY );
102 if( this->m_Handle == NULL )
103 cpPluginsErrorMacro( << dlerror( ) );
104 #endif // cpPlugins_OS_Windows
107 // -------------------------------------------------------------------------
108 void* cpPlugins::Library::
109 _CreateObject( const std::string& name )
111 if( this->m_Handle != NULL )
113 #ifdef cpPlugins_OS_Windows
114 #else // cpPlugins_OS_Windows
115 void* hnd = dlsym( this->m_Handle, "cpPlugins_Create" );
117 cpPluginsErrorMacro( << dlerror( ) );
118 typedef void* (_TFunc)( const char* );
119 return( ( reinterpret_cast< _TFunc* >( hnd ) )( name.c_str( ) ) );
120 #endif // cpPlugins_OS_Windows