]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Manager.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Manager.cxx
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 #include <cpPlugins/Manager.h>
6
7 #include <cstdlib>
8 #include <boost/filesystem.hpp>
9 #include <boost/tokenizer.hpp>
10 #include <cpPlugins/ProcessObject.h>
11
12 // -------------------------------------------------------------------------
13 namespace cpPlugins
14 {
15   bool                Manager::m_Configured = false;
16   Manager::TLibraries Manager::m_Libraries;
17   Manager::TPlugins   Manager::m_Plugins;
18 } // end namespace
19
20 // -------------------------------------------------------------------------
21 cpPlugins::Manager::
22 Manager( )
23 {
24 }
25
26 // -------------------------------------------------------------------------
27 cpPlugins::Manager::
28 ~Manager( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 void cpPlugins::Manager::
34 Print( std::ostream& out ) const
35 {
36   out
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 )
41   {
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;
47     out << std::endl;
48   } // end for
49   out << "----------------------------------------------";
50 }
51
52 // -------------------------------------------------------------------------
53 const cpPlugins::Manager::
54 TPlugins& cpPlugins::Manager::
55 GetPlugins( ) const
56 {
57   return( Self::m_Plugins );
58 }
59
60 // -------------------------------------------------------------------------
61 void cpPlugins::Manager::
62 Configure( )
63 {
64   static const std::string paths[] =
65   {
66     cpPlugins_ENV,
67     "LD_LIBRARY_PATH",
68     "PATH",
69     "Path",
70     "DYLD_LIBRARY_PATH"
71   };
72   static const unsigned int paths_count = 4;
73   if( !Self::m_Configured )
74   {
75     this->AddPath( "." );
76     for( unsigned int i = 0; i < paths_count; ++i )
77     {
78       const char* env_p = std::getenv( paths[ i ].c_str( ) );
79       if( env_p != NULL )
80       {
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 )
87           this->AddPath( t );
88       } // end if
89     } // end for
90     Self::m_Configured = true;
91   } // end if
92 }
93
94 // -------------------------------------------------------------------------
95 void cpPlugins::Manager::
96 AddPath( const std::string& p )
97 {
98   namespace bf = boost::filesystem;
99
100   try
101   {
102     bf::path cp = bf::canonical( bf::path( p ) );
103     if( bf::is_directory( cp ) )
104     {
105       bf::directory_iterator dIt( cp );
106       for( ; dIt != bf::directory_iterator( ); ++dIt )
107       {
108         if( dIt->path( ).extension( ).string( ) == cpPlugins_EXT )
109           this->AddFile( dIt->path( ).string( ) );
110       } // end for
111     }
112     else
113     {
114       if( cp.extension( ).string( ) == cpPlugins_EXT )
115         this->AddFile( cp.string( ) );
116     } // end if
117   }
118   catch( ... ) { }
119 }
120
121 // -------------------------------------------------------------------------
122 void cpPlugins::Manager::
123 AddFile( const std::string& f )
124 {
125   typedef TLibraries::value_type _TLib;
126   typedef TPlugins::value_type   _TPlg;
127   namespace bf = boost::filesystem;
128
129   try
130   {
131     bf::path cf = bf::canonical( bf::path( f ) );
132     if( bf::is_directory( cf ) )
133     {
134       bf::directory_iterator dIt( cf );
135       for( ; dIt != bf::directory_iterator( ); ++dIt )
136       {
137         if( dIt->path( ).extension( ).string( ) == cpPlugins_EXT )
138           this->AddFile( dIt->path( ).string( ) );
139       } // end for
140     }
141     else
142     {
143       if( cf.extension( ).string( ) == cpPlugins_EXT )
144       {
145         std::string n = cf.filename( ).string( );
146         if( Self::m_Libraries.find( n ) == Self::m_Libraries.end( ) )
147         {
148           TLibraries::iterator lIt =
149             Self::m_Libraries.insert(
150               _TLib( n, cpPlugins::Library( cf.string( ) ) )
151               ).first;
152           for( const std::string& f: lIt->second.GetContents( ) )
153             Self::m_Plugins.insert( _TPlg( f, n ) );
154         } // end if
155       } // end if
156     } // end if
157   }
158   catch( ... ) { }
159 }
160
161 // -------------------------------------------------------------------------
162 std::shared_ptr< cpPlugins::ProcessObject >  cpPlugins::Manager::
163 Create( const std::string& name )
164 {
165   TPlugins::iterator pIt = Self::m_Plugins.find( name );
166   if( pIt != Self::m_Plugins.end( ) )
167   {
168     TLibraries::iterator lIt = Self::m_Libraries.find( pIt->second );
169     if( lIt != Self::m_Libraries.end( ) )
170       return( lIt->second.Create( name ) );
171   } // end if
172   cpPluginsErrorMacro(
173     this, << "Could not create an object of type \"" << name << "\"."
174     );
175 }
176
177 // eof - $RCSfile$