MyGUI  3.0.3
MyGUI_ResourceHolder.h
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 #ifndef __MYGUI_RESOURCE_HOLDER_H__
00024 #define __MYGUI_RESOURCE_HOLDER_H__
00025 
00026 #include "MyGUI_Prerequest.h"
00027 #include "MyGUI_Enumerator.h"
00028 
00029 namespace MyGUI
00030 {
00031 
00032     template <typename Type>
00033     class /*MYGUI_EXPORT */ResourceHolder
00034     {
00035     public:
00036         typedef std::map<std::string, Type*> MapResource;
00037         typedef Enumerator<MapResource> EnumeratorPtr;
00038 
00039         virtual ~ResourceHolder() { }
00040     public:
00042         bool isExist(const std::string& _name) const
00043         {
00044             return mResources.find(_name) != mResources.end();
00045         }
00046 
00048         Type* findByName(const std::string& _name) const
00049         {
00050             typename MapResource::const_iterator item = mResources.find(_name);
00051             return (item == mResources.end()) ? nullptr : item->second;
00052         }
00053 
00055         Type* getByName(const std::string& _name, bool _throw = true) const
00056         {
00057             Type* result = findByName(_name);
00058             MYGUI_ASSERT(result || !_throw, "Resource '" << _name << "' not found");
00059             return result;
00060         }
00061 
00062         bool remove(const std::string& _name)
00063         {
00064             typename MapResource::const_iterator item = mResources.find(_name);
00065             if (item != mResources.end())
00066             {
00067                 delete item->second;
00068                 mResources.erase(item->first);
00069                 return true;
00070             }
00071             return false;
00072         }
00073 
00074         void clear()
00075         {
00076             for (typename MapResource::iterator item=mResources.begin(); item!=mResources.end(); ++item)
00077             {
00078                 delete item->second;
00079             }
00080             mResources.clear();
00081         }
00082 
00083         EnumeratorPtr getEnumerator()
00084         {
00085             return EnumeratorPtr(mResources);
00086         }
00087 
00088         size_t getCount() const { return mResources.size(); }
00089 
00090     protected:
00091         MapResource mResources;
00092     };
00093 
00094 } // namespace MyGUI
00095 
00096 #endif // __MYGUI_RESOURCE_HOLDER_H__