cPluginManager.cpp

00001 #include "../Headers/cPluginManager.h"
00002 #include "../Headers/cUtils.h"
00003 #include "../include/cAudioPlatform.h"
00004 #include "../include/cAudioDefines.h"
00005 #include "../include/ILogger.h"
00006 
00007 namespace cAudio
00008 {
00009 
00010 typedef IAudioPlugin* (*GetPluginModule)(const char* version);
00011 
00012 cPluginManager::cPluginManager()
00013 {
00014         autoLoadPlugins();
00015 }
00016 
00017 cPluginManager::~cPluginManager()
00018 {
00019         std::map<IAudioPlugin*, DYNLIB_HANDLE>::iterator it;
00020         for(it = DynamicallyLoadedPlugins.begin(); it != DynamicallyLoadedPlugins.end(); it++)
00021         {
00022                 //Found a plugin we loaded from the filesystem, unload it and delete the plugin
00023                 it->first->drop();
00024                 if(DYNLIB_UNLOAD(it->second))
00025                 {
00026                         //Could be an error, not reporting it for now
00027                 }
00028         }
00029 }
00030 
00031 bool cPluginManager::installPlugin(IAudioPlugin* plugin, const char* name)
00032 {
00033         if(plugin)
00034         {
00035                 std::string theName = safeCStr(name);
00036                 if(theName.empty())
00037                         theName = plugin->getPluginName();
00038 
00039                 if(plugin->installPlugin(getLogger()))
00040                 {
00041                         RegisteredPlugins[theName] = plugin;
00042                         return true;
00043                 }
00044         }
00045         return false;
00046 }
00047 
00048 bool cPluginManager::installPlugin(const char* filename, const char* name)
00049 {
00050         DYNLIB_HANDLE m_hInst = DYNLIB_LOAD(filename);
00051         if(m_hInst)
00052         {
00053                 GetPluginModule moduleFunc = (GetPluginModule)DYNLIB_GETSYM(m_hInst, "GetPluginModule");
00054 
00055                 if(moduleFunc)
00056                 {
00057                         IAudioPlugin* plugin = moduleFunc(CAUDIO_VERSION);
00058 
00059                         if(plugin)
00060                         {
00061                                 DynamicallyLoadedPlugins[plugin] = m_hInst;
00062 
00063                                 return installPlugin(plugin, name);
00064                         }
00065                 }
00066         }
00067         return false;
00068 }
00069 
00070 bool cPluginManager::checkForPlugin(const char* name)
00071 {
00072         return (RegisteredPlugins.find(name) != RegisteredPlugins.end());
00073 }
00074 
00075 IAudioPlugin* cPluginManager::getPlugin(const char* name)
00076 {
00077         if(RegisteredPlugins.find(name) != RegisteredPlugins.end())
00078         {
00079                 return RegisteredPlugins[name];
00080         }
00081         return NULL;
00082 }
00083 
00084 unsigned int cPluginManager::getPluginCount()
00085 {
00086         return RegisteredPlugins.size();
00087 }
00088 
00089 std::vector<IAudioPlugin*> cPluginManager::getPluginList()
00090 {
00091         std::vector<IAudioPlugin*> list;
00092         std::map<std::string, IAudioPlugin*>::iterator it;
00093         for(it = RegisteredPlugins.begin(); it != RegisteredPlugins.end(); it++)
00094         {
00095                 list.push_back(it->second);
00096         }
00097         return list;
00098 }
00099 
00100 void cPluginManager::uninstallPlugin(IAudioPlugin* plugin)
00101 {
00102         if(plugin)
00103         {
00104                 std::map<std::string, IAudioPlugin*>::iterator it;
00105                 for(it = RegisteredPlugins.begin(); it != RegisteredPlugins.end(); it++)
00106                 {
00107                         if(it->second == plugin)
00108                         {
00109                                 RegisteredPlugins.erase(it->first);
00110                                 break;
00111                         }
00112                 }
00113 
00114                 std::map<IAudioPlugin*, DYNLIB_HANDLE>::iterator it2 = DynamicallyLoadedPlugins.find(plugin);
00115                 if(it2 != DynamicallyLoadedPlugins.end())
00116                 {
00117                         //Found a plugin we loaded from the filesystem, unload it and delete the plugin
00118                         if(DYNLIB_UNLOAD(it2->second))
00119                         {
00120                                 //Could be an error, not reporting it for now
00121                         }
00122                         DynamicallyLoadedPlugins.erase(it2->first);
00123                         delete plugin;
00124                 }
00125         }
00126 }
00127 
00128 void cPluginManager::uninstallPlugin(const char* name)
00129 {
00130         if(RegisteredPlugins.find(name) != RegisteredPlugins.end())
00131         {
00132                 uninstallPlugin(RegisteredPlugins[name]);
00133         }
00134 }
00135 
00136 void cPluginManager::autoLoadPlugins()
00137 {
00138         std::vector<std::string> fileList = getFilesInDirectory(".");
00139         for(int i=0; i<fileList.size(); ++i)
00140         {
00141                 if(fileList[i].substr(0, 4) == "cAp_")
00142                 {
00143 #ifdef CAUDIO_PLATFORM_WIN
00144                         if(fileList[i].substr(fileList[i].length()-4, 4) == ".dll")
00145 #endif
00146 #ifdef CAUDIO_PLATFORM_LINUX
00147                         if(fileList[i].substr(fileList[i].length()-3, 3) == ".so")
00148 #endif
00149 #ifdef CAUDIO_PLATFORM_MAC
00150                         if(fileList[i].substr(fileList[i].length()-6, 6) == ".dylib")
00151 #endif
00152                         {
00153                                 //Found a plugin, load it
00154                                 installPlugin(std::string("./" + fileList[i]).c_str(), NULL);
00155                         }
00156                 }
00157         }
00158 }
00159 
00160 CAUDIO_API IPluginManager* getPluginManager()
00161 {
00162         return cPluginManager::Instance();
00163 }
00164 
00165 };
 All Classes Namespaces Functions Variables Enumerations

Generated on Sat Feb 20 22:55:09 2010 for cAudio by  doxygen 1.6.2