• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_PluginManager.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009     
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014     
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019     
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_PluginManager.h"
00025 #include "MyGUI_DynLibManager.h"
00026 #include "MyGUI_Common.h"
00027 
00028 namespace MyGUI
00029 {
00030     const std::string XML_TYPE("Plugin");
00031 
00032     MYGUI_INSTANCE_IMPLEMENT(PluginManager);
00033 
00034     void PluginManager::initialise()
00035     {
00036         MYGUI_ASSERT(false == mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
00037         MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
00038 
00039         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &PluginManager::_load);
00040 
00041         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
00042         mIsInitialise = true;
00043     }
00044 
00045     void PluginManager::shutdown()
00046     {
00047         if (false == mIsInitialise) return;
00048         MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
00049 
00050         unloadAllPlugins();
00051         ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00052 
00053         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
00054         mIsInitialise = false;
00055     }
00056 
00057     void PluginManager::loadPlugin(const std::string& _file)
00058     {
00059         // check initialise
00060         MYGUI_ASSERT(mIsInitialise, INSTANCE_TYPE_NAME << "used but not initialised");
00061         // Load plugin library
00062         DynLib* lib = DynLibManager::getInstance().load( _file );
00063         // Store for later unload
00064         mLibs[_file] = lib;
00065 
00066         // Call startup function
00067         DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib->getSymbol("dllStartPlugin");
00068 
00069         /*Assert(pFunc, Exception::ERR_ITEM_NOT_FOUND, "Cannot find symbol dllStartPlugin in library " + fileName,
00070             "PluginManager::loadPlugin");*/
00071 
00072         MYGUI_ASSERT(nullptr != pFunc, INSTANCE_TYPE_NAME << "Cannot find symbol 'dllStartPlugin' in library " << _file);
00073 
00074         // This must call installPlugin
00075         pFunc();
00076     }
00077 
00078     void PluginManager::unloadPlugin(const std::string& _file)
00079     {
00080         // check initialise
00081         MYGUI_ASSERT(mIsInitialise, INSTANCE_TYPE_NAME << "used but not initialised");
00082 
00083         DynLibList::iterator it = mLibs.find(_file);
00084         if (it != mLibs.end())
00085         {
00086             // Call plugin shutdown
00087             DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)(*it).second->getSymbol("dllStopPlugin");
00088 
00089             MYGUI_ASSERT(nullptr != pFunc, INSTANCE_TYPE_NAME << "Cannot find symbol 'dllStopPlugin' in library " << _file);
00090 
00091             // this must call uninstallPlugin
00092             pFunc();
00093             // Unload library (destroyed by DynLibManager)
00094             DynLibManager::getInstance().unload((*it).second);
00095             mLibs.erase(it);
00096         }
00097     }
00098 
00099     bool PluginManager::load(const std::string& _file/*, const std::string& _group*/)
00100     {
00101         return ResourceManager::getInstance()._loadImplement(_file, /*_group, */true, XML_TYPE, INSTANCE_TYPE_NAME);
00102     }
00103 
00104     void PluginManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00105     {
00106         xml::ElementEnumerator node = _node->getElementEnumerator();
00107         std::string source;
00108         while (node.next("path"))
00109         {
00110             if (node->findAttribute("source", source)) loadPlugin(source);
00111         }
00112     }
00113 
00114     void PluginManager::installPlugin(IPlugin* _plugin)
00115     {
00116         // check initialise
00117         MYGUI_ASSERT(mIsInitialise, INSTANCE_TYPE_NAME << "used but not initialised");
00118 
00119         MYGUI_LOG(Info, "Installing plugin: " << _plugin->getName());
00120 
00121         mPlugins.insert(_plugin);
00122         _plugin->install();
00123 
00124         _plugin->initialize();
00125 
00126         MYGUI_LOG(Info, "Plugin successfully installed");
00127     }
00128 
00129     void PluginManager::uninstallPlugin(IPlugin* _plugin)
00130     {
00131         // check initialise
00132         MYGUI_ASSERT(mIsInitialise, INSTANCE_TYPE_NAME << "used but not initialised");
00133 
00134         MYGUI_LOG(Info, "Uninstalling plugin: " << _plugin->getName());
00135         PluginList::iterator it = mPlugins.find(_plugin);
00136         if (it != mPlugins.end())
00137         {
00138             _plugin->shutdown();
00139             _plugin->uninstall();
00140             mPlugins.erase(it);
00141         }
00142         MYGUI_LOG(Info, "Plugin successfully uninstalled");
00143     }
00144 
00145     void PluginManager::unloadAllPlugins()
00146     {
00147         while (false == mLibs.empty())
00148             unloadPlugin((*mLibs.begin()).first);
00149     }
00150 
00151 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1