1 | #ifndef _ResourceManager_H__
|
---|
2 | #define _ResourceManager_H__
|
---|
3 |
|
---|
4 | #include <vector>
|
---|
5 | #include <string>
|
---|
6 |
|
---|
7 | //#include "Containers.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 | #include "Material.h"
|
---|
10 |
|
---|
11 | namespace GtpVisibilityPreprocessor {
|
---|
12 |
|
---|
13 |
|
---|
14 | /** Resource manager controlling loading and destroying of resources.
|
---|
15 |
|
---|
16 | @note Resources must be created using the manager to assure that
|
---|
17 | they are destroyed properly, otherwise the developer has the
|
---|
18 | responsibility to destroy them himself.
|
---|
19 |
|
---|
20 | @note This class defines the pattern of singleton to assure that
|
---|
21 | there is always only one resource manager.
|
---|
22 | */
|
---|
23 | template <typename T> class ResourceManager
|
---|
24 | {
|
---|
25 |
|
---|
26 | public:
|
---|
27 |
|
---|
28 | /** Returns the resource manager as a singleton.
|
---|
29 | */
|
---|
30 | static ResourceManager<T> *GetSingleton()
|
---|
31 | {
|
---|
32 | if (!sResourceManager)
|
---|
33 | {
|
---|
34 | sResourceManager = new ResourceManager();
|
---|
35 | }
|
---|
36 |
|
---|
37 | return sResourceManager;
|
---|
38 | }
|
---|
39 |
|
---|
40 | static void DelSingleton()
|
---|
41 | {
|
---|
42 | DEL_PTR(sResourceManager);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /** Creates new entry with a unique id
|
---|
46 | */
|
---|
47 | T *CreateResource()
|
---|
48 | {
|
---|
49 | const int id = CreateUniqueId();
|
---|
50 |
|
---|
51 | T *resource = new T(id);
|
---|
52 | mEntries[id] = resource;
|
---|
53 |
|
---|
54 | return resource;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /** Searches for the mesh with the given name.
|
---|
59 | @returns mesh or NULL if mesh was not found.
|
---|
60 | */
|
---|
61 | T *FindEntry(const int id) const
|
---|
62 | {
|
---|
63 | std::map<int, T *>::const_iterator mit = mEntries.find(id);
|
---|
64 |
|
---|
65 | if (mit != mEntries.end())
|
---|
66 | {
|
---|
67 | return (*mit).second;
|
---|
68 | }
|
---|
69 |
|
---|
70 | return NULL;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Destroys mesh with the given name.
|
---|
74 | @returns true if the mesh was found, false if not
|
---|
75 | */
|
---|
76 | bool DestroyEntry(const int id)
|
---|
77 | {
|
---|
78 | if (!FindEntry(id))
|
---|
79 | {
|
---|
80 | T *resource = mEntries[id];
|
---|
81 |
|
---|
82 | mEntries.erase(id);
|
---|
83 | DEL_PTR(resource);
|
---|
84 |
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /** Returns size of resource container.
|
---|
93 | */
|
---|
94 | const int GetSize()
|
---|
95 | {
|
---|
96 | return (int)mEntries.size();
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected:
|
---|
100 |
|
---|
101 | /** Default constructor. The constructor is protected
|
---|
102 | to have control over instantiation using the
|
---|
103 | singleton pattern.
|
---|
104 | */
|
---|
105 | ResourceManager<T>()
|
---|
106 | {}
|
---|
107 |
|
---|
108 | /** Release resources.
|
---|
109 | */
|
---|
110 | ~ResourceManager<T>()
|
---|
111 | {
|
---|
112 | std::map<int, T *>::iterator mit, mit_end = mEntries.end();
|
---|
113 |
|
---|
114 | for (mit = mEntries.begin(); mit != mEntries.end(); ++ mit)
|
---|
115 | {
|
---|
116 | //cout << "mesh: " << (*mit).first << " " << (*mit).second << endl;
|
---|
117 | DEL_PTR((*mit).second);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Copy constructor.
|
---|
122 | */
|
---|
123 | ResourceManager<T>(const ResourceManager<T>& source)
|
---|
124 | {
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Helper function returning unique id for resource.
|
---|
128 | */
|
---|
129 | static int CreateUniqueId()
|
---|
130 | {
|
---|
131 | return sCurrentId ++;
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected:
|
---|
135 |
|
---|
136 | /// the resource container
|
---|
137 | std::map<int, T *> mEntries;
|
---|
138 |
|
---|
139 | private:
|
---|
140 |
|
---|
141 | static ResourceManager<T> *sResourceManager;
|
---|
142 | static int sCurrentId;
|
---|
143 | };
|
---|
144 |
|
---|
145 | // only instance of the resource manager
|
---|
146 | template <typename T> ResourceManager<T> *ResourceManager<T>::sResourceManager = NULL;
|
---|
147 | template <typename T> int ResourceManager<T>::sCurrentId = 0;
|
---|
148 |
|
---|
149 | /// This type is responsible for creation and disposal the meshes
|
---|
150 | typedef ResourceManager<Mesh> MeshManager;
|
---|
151 | /// This type is esponsible for creation and disposal of the materials.
|
---|
152 | typedef ResourceManager<Material> MaterialManager;
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | #endif
|
---|