pluginRegistry.cpp

00001 
00002 /* Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 #include "pluginRegistry.h"
00018 #include "compressor.h" 
00019 #include "global.h"
00020 
00021 
00022 #include <eq/base/fileSearch.h> 
00023 #include <eq/base/dso.h> 
00024 #include <vector>
00025 
00026 namespace eq
00027 {
00028 
00029 
00030 void PluginRegistry::init()
00031 {
00032     EQASSERT( _compressors.empty( ));
00033 
00034     // add self
00035     _initCompressor( "" );
00036     EQASSERT( _compressors.size() == 1 );
00037 
00038     // search all plugin directories for compressor DSOs
00039     const StringVector& directories = Global::getPluginDirectories();
00040 
00041     // for each directory
00042     for( StringVector::const_iterator i = directories.begin();
00043          i != directories.end(); ++i )
00044     {
00045         const std::string& directory = *i;
00046         EQINFO << "Searching compressors in " << directory << std::endl;
00047 
00048         // search the number of files in the director
00049 #ifdef WIN32
00050         StringVector files = base::fileSearch( directory, 
00051                                                 "EqualizerCompressor*.dll" );
00052         const char DIRSEP = '\\';
00053 #elif defined (Darwin)
00054         StringVector files = base::fileSearch( directory, 
00055                                                 "libeqCompressor*dylib" );
00056         const char DIRSEP = '/';
00057 #else
00058         StringVector files = base::fileSearch( directory,
00059                                                 "libeqCompressor*so" );
00060         const char DIRSEP = '/';
00061 #endif
00062         
00063         // for each file in the directory
00064         for( StringVector::const_iterator j = files.begin();
00065              j != files.end(); ++j )
00066         {
00067             // build path + name of library
00068             const std::string libraryName = 
00069                 directory.empty() ? *j : directory + DIRSEP + *j;
00070             _initCompressor( libraryName );
00071         }
00072     }
00073 }
00074 
00075 void PluginRegistry::_initCompressor( const std::string& filename )
00076 {
00077     Compressor* compressor = new Compressor(); 
00078     bool add = compressor->init( filename );
00079 
00080     const CompressorInfoVector& infos = compressor->getInfos();
00081     if( infos.empty( ))
00082         add = false;
00083             
00084     for( CompressorVector::const_iterator i = _compressors.begin();
00085          add && i != _compressors.end(); ++i )
00086     {
00087         const CompressorInfoVector& infos2 = (*i)->getInfos();
00088         // Simple test to avoid using the same dll twice
00089         if( infos.front().name == infos2.front().name )
00090             add = false;
00091     }
00092 
00093     if( add )
00094     {
00095         _compressors.push_back( compressor );
00096         EQINFO << "Found compressor " << filename << " @" << (void*)compressor
00097                << " providing " << compressor->getInfos().size() << " engines"
00098                << std::endl;
00099     }
00100     else
00101         delete compressor;
00102 }
00103 
00104 void PluginRegistry::exit()
00105 {
00106     for( CompressorVector::const_iterator i = _compressors.begin(); 
00107          i != _compressors.end(); ++i )
00108     {
00109         Compressor* compressor = *i;
00110         compressor->exit();
00111         delete compressor;
00112     }
00113 
00114     _compressors.clear();
00115 }
00116 
00117 Compressor* PluginRegistry::findCompressor( const uint32_t name )
00118 {
00119 
00120     for( CompressorVector::const_iterator i = _compressors.begin(); 
00121          i != _compressors.end(); ++i )
00122     {
00123         Compressor* compressor = *i;
00124         if ( compressor->implementsType( name ))
00125             return compressor;
00126     }
00127 
00128     return 0;
00129 }
00130 
00131 const CompressorVector& PluginRegistry::getCompressors() const
00132 {
00133     return _compressors;
00134 }
00135 
00136 
00137 }
Generated on Mon Aug 10 18:58:40 2009 for Equalizer 0.9 by  doxygen 1.5.8