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