1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
5 #include <cpPlugins/Manager.h>
8 #include <boost/filesystem.hpp>
9 #include <boost/tokenizer.hpp>
10 #include <cpPlugins/ProcessObject.h>
12 // -------------------------------------------------------------------------
15 bool Manager::m_Configured = false;
16 Manager::TLibraries Manager::m_Libraries;
17 Manager::TPlugins Manager::m_Plugins;
20 // -------------------------------------------------------------------------
26 // -------------------------------------------------------------------------
32 // -------------------------------------------------------------------------
33 void cpPlugins::Manager::
34 Print( std::ostream& out ) const
37 << "----------------------------------------------" << std::endl
38 << " *** cpPlugins::Interface::Manager ***" << std::endl
39 << " * Reachable plugins:" << std::endl << std::endl;
40 for( const TLibraries::value_type& l: this->m_Libraries )
42 out << " + " << l.first << std::endl;
43 out << " --> Library:" << l.second.GetPath( ) << std::endl;
44 out << " --> Loadable objects:" << std::endl;
45 for( const std::string& o: l.second.GetContents( ) )
46 out << " > " << o << std::endl;
49 out << "----------------------------------------------";
52 // -------------------------------------------------------------------------
53 const cpPlugins::Manager::
54 TPlugins& cpPlugins::Manager::
57 return( Self::m_Plugins );
60 // -------------------------------------------------------------------------
61 void cpPlugins::Manager::
64 static const std::string paths[] =
72 static const unsigned int paths_count = 4;
73 if( !Self::m_Configured )
76 for( unsigned int i = 0; i < paths_count; ++i )
78 const char* env_p = std::getenv( paths[ i ].c_str( ) );
81 std::string str = env_p;
82 typedef boost::char_separator< char > _TSep;
83 typedef boost::tokenizer< _TSep > _TTok;
84 _TSep sep{ cpPlugins_ENV_SEP };
85 _TTok tokens{ str, sep };
86 for( const std::string& t: tokens )
90 Self::m_Configured = true;
94 // -------------------------------------------------------------------------
95 void cpPlugins::Manager::
96 AddPath( const std::string& p )
98 namespace bf = boost::filesystem;
102 bf::path cp = bf::canonical( bf::path( p ) );
103 if( bf::is_directory( cp ) )
105 bf::directory_iterator dIt( cp );
106 for( ; dIt != bf::directory_iterator( ); ++dIt )
108 if( dIt->path( ).extension( ).string( ) == cpPlugins_EXT )
109 this->AddFile( dIt->path( ).string( ) );
114 if( cp.extension( ).string( ) == cpPlugins_EXT )
115 this->AddFile( cp.string( ) );
121 // -------------------------------------------------------------------------
122 void cpPlugins::Manager::
123 AddFile( const std::string& f )
125 typedef TLibraries::value_type _TLib;
126 typedef TPlugins::value_type _TPlg;
127 namespace bf = boost::filesystem;
131 bf::path cf = bf::canonical( bf::path( f ) );
132 if( bf::is_directory( cf ) )
134 bf::directory_iterator dIt( cf );
135 for( ; dIt != bf::directory_iterator( ); ++dIt )
137 if( dIt->path( ).extension( ).string( ) == cpPlugins_EXT )
138 this->AddFile( dIt->path( ).string( ) );
143 if( cf.extension( ).string( ) == cpPlugins_EXT )
145 std::string n = cf.filename( ).string( );
146 if( Self::m_Libraries.find( n ) == Self::m_Libraries.end( ) )
148 TLibraries::iterator lIt =
149 Self::m_Libraries.insert(
150 _TLib( n, cpPlugins::Library( cf.string( ) ) )
152 for( const std::string& f: lIt->second.GetContents( ) )
153 Self::m_Plugins.insert( _TPlg( f, n ) );
161 // -------------------------------------------------------------------------
162 std::shared_ptr< cpPlugins::ProcessObject > cpPlugins::Manager::
163 Create( const std::string& name )
165 TPlugins::iterator pIt = Self::m_Plugins.find( name );
166 if( pIt != Self::m_Plugins.end( ) )
168 TLibraries::iterator lIt = Self::m_Libraries.find( pIt->second );
169 if( lIt != Self::m_Libraries.end( ) )
170 return( lIt->second.Create( name ) );
173 this, << "Could not create an object of type \"" << name << "\"."