MyGUI  3.2.1
MyGUI_DynLibManager.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_DynLibManager.h"
00009 #include "MyGUI_Gui.h"
00010 #include "MyGUI_WidgetManager.h"
00011 
00012 namespace MyGUI
00013 {
00014 
00015     template <> DynLibManager* Singleton<DynLibManager>::msInstance = nullptr;
00016     template <> const char* Singleton<DynLibManager>::mClassTypeName = "DynLibManager";
00017 
00018     DynLibManager::DynLibManager() :
00019         mIsInitialise(false)
00020     {
00021     }
00022 
00023     void DynLibManager::initialise()
00024     {
00025         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00026         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00027 
00028         Gui::getInstance().eventFrameStart += newDelegate(this, &DynLibManager::notifyEventFrameStart);
00029 
00030         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00031         mIsInitialise = true;
00032     }
00033 
00034     void DynLibManager::shutdown()
00035     {
00036         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00037         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00038 
00039         unloadAll();
00040 
00041         Gui::getInstance().eventFrameStart -= newDelegate(this, &DynLibManager::notifyEventFrameStart);
00042         _unloadDelayDynLibs();
00043 
00044         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00045         mIsInitialise = false;
00046     }
00047 
00048     DynLib* DynLibManager::load(const std::string& fileName)
00049     {
00050         StringDynLibMap::iterator it = mLibsMap.find(fileName);
00051 
00052         if (it != mLibsMap.end())
00053         {
00054             return it->second;
00055         }
00056 
00057         DynLib* pLib = new DynLib(fileName);
00058         if (!pLib->load())
00059         {
00060             delete pLib;
00061             return 0;
00062         }
00063 
00064         mLibsMap[fileName] = pLib;
00065         return pLib;
00066     }
00067 
00068     void DynLibManager::unload(DynLib* library)
00069     {
00070         StringDynLibMap::iterator it = mLibsMap.find(library->getName());
00071 
00072         if (it != mLibsMap.end())
00073             mLibsMap.erase(it);
00074 
00075         mDelayDynLib.push_back(library);
00076     }
00077 
00078     void DynLibManager::unloadAll()
00079     {
00080         // unload and delete resources
00081         for (StringDynLibMap::iterator it = mLibsMap.begin(); it != mLibsMap.end(); ++it)
00082         {
00083             mDelayDynLib.push_back(it->second);
00084         }
00085         // Empty the list
00086         mLibsMap.clear();
00087     }
00088 
00089     void DynLibManager::notifyEventFrameStart(float _time)
00090     {
00091         _unloadDelayDynLibs();
00092     }
00093 
00094     void DynLibManager::_unloadDelayDynLibs()
00095     {
00096         if (!mDelayDynLib.empty())
00097         {
00098             WidgetManager* manager = WidgetManager::getInstancePtr();
00099             if (manager != nullptr)
00100                 manager->_deleteDelayWidgets();
00101 
00102             for (VectorDynLib::iterator entry = mDelayDynLib.begin(); entry != mDelayDynLib.end(); ++entry)
00103             {
00104                 (*entry)->unload();
00105                 delete (*entry);
00106             }
00107             mDelayDynLib.clear();
00108         }
00109     }
00110 
00111 } // namespace MyGUI