MyGUI  3.0.1
MyGUI_ResourceHolder.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_RESOURCE_HOLDER_H__
24 #define __MYGUI_RESOURCE_HOLDER_H__
25 
26 #include "MyGUI_Prerequest.h"
27 #include "MyGUI_Enumerator.h"
28 
29 namespace MyGUI
30 {
31 
32  template <typename Type>
33  class /*MYGUI_EXPORT */ResourceHolder
34  {
35  public:
36  typedef std::map<std::string, Type*> MapResource;
38 
39  virtual ~ResourceHolder() { }
40  public:
42  bool isExist(const std::string& _name) const
43  {
44  return mResources.find(_name) != mResources.end();
45  }
46 
48  Type* findByName(const std::string& _name) const
49  {
50  typename MapResource::const_iterator item = mResources.find(_name);
51  return (item == mResources.end()) ? nullptr : item->second;
52  }
53 
55  Type* getByName(const std::string& _name, bool _throw = true) const
56  {
57  Type* result = findByName(_name);
58  MYGUI_ASSERT(result || !_throw, "Resource '" << _name << "' not found");
59  return result;
60  }
61 
62  bool remove(const std::string& _name)
63  {
64  typename MapResource::const_iterator item = mResources.find(_name);
65  if (item != mResources.end())
66  {
67  delete item->second;
68  mResources.erase(item->first);
69  return true;
70  }
71  return false;
72  }
73 
74  void clear()
75  {
76  for (typename MapResource::iterator item=mResources.begin(); item!=mResources.end(); ++item)
77  {
78  delete item->second;
79  }
80  mResources.clear();
81  }
82 
84  {
85  return EnumeratorPtr(mResources);
86  }
87 
88  size_t getCount() const { return mResources.size(); }
89 
90  protected:
92  };
93 
94 } // namespace MyGUI
95 
96 #endif // __MYGUI_RESOURCE_HOLDER_H__