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