MyGUI  3.2.0
MyGUI_DynLibManager.cpp
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #include "MyGUI_Precompiled.h"
00023 #include "MyGUI_DynLibManager.h"
00024 #include "MyGUI_Gui.h"
00025 #include "MyGUI_WidgetManager.h"
00026 
00027 namespace MyGUI
00028 {
00029 
00030     template <> DynLibManager* Singleton<DynLibManager>::msInstance = nullptr;
00031     template <> const char* Singleton<DynLibManager>::mClassTypeName("DynLibManager");
00032 
00033     DynLibManager::DynLibManager() :
00034         mIsInitialise(false)
00035     {
00036     }
00037 
00038     void DynLibManager::initialise()
00039     {
00040         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00041         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00042 
00043         Gui::getInstance().eventFrameStart += newDelegate(this, &DynLibManager::notifyEventFrameStart);
00044 
00045         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00046         mIsInitialise = true;
00047     }
00048 
00049     void DynLibManager::shutdown()
00050     {
00051         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00052         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00053 
00054         unloadAll();
00055 
00056         Gui::getInstance().eventFrameStart -= newDelegate(this, &DynLibManager::notifyEventFrameStart);
00057         _unloadDelayDynLibs();
00058 
00059         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00060         mIsInitialise = false;
00061     }
00062 
00063     DynLib* DynLibManager::load(const std::string& fileName)
00064     {
00065         StringDynLibMap::iterator it = mLibsMap.find(fileName);
00066 
00067         if (it != mLibsMap.end())
00068         {
00069             return it->second;
00070         }
00071 
00072         DynLib* pLib = new DynLib(fileName);
00073         if (!pLib->load())
00074         {
00075             delete pLib;
00076             return 0;
00077         }
00078 
00079         mLibsMap[fileName] = pLib;
00080         return pLib;
00081     }
00082 
00083     void DynLibManager::unload(DynLib* library)
00084     {
00085         StringDynLibMap::iterator it = mLibsMap.find(library->getName());
00086 
00087         if (it != mLibsMap.end())
00088             mLibsMap.erase(it);
00089 
00090         mDelayDynLib.push_back(library);
00091     }
00092 
00093     void DynLibManager::unloadAll()
00094     {
00095         // unload and delete resources
00096         for (StringDynLibMap::iterator it = mLibsMap.begin(); it != mLibsMap.end(); ++it)
00097         {
00098             mDelayDynLib.push_back(it->second);
00099         }
00100         // Empty the list
00101         mLibsMap.clear();
00102     }
00103 
00104     void DynLibManager::notifyEventFrameStart(float _time)
00105     {
00106         _unloadDelayDynLibs();
00107     }
00108 
00109     void DynLibManager::_unloadDelayDynLibs()
00110     {
00111         if (!mDelayDynLib.empty())
00112         {
00113             WidgetManager* manager = WidgetManager::getInstancePtr();
00114             if (manager != nullptr)
00115                 manager->_deleteDelayWidgets();
00116 
00117             for (VectorDynLib::iterator entry = mDelayDynLib.begin(); entry != mDelayDynLib.end(); ++entry)
00118             {
00119                 (*entry)->unload();
00120                 delete (*entry);
00121             }
00122             mDelayDynLib.clear();
00123         }
00124     }
00125 
00126 } // namespace MyGUI