[1001] | 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 |
|
---|
[2542] | 11 |
|
---|
| 12 |
|
---|
[1001] | 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 |
|
---|
[1004] | 42 | /** We also want full control over the delete.
|
---|
| 43 | */
|
---|
[1001] | 44 | static void DelSingleton()
|
---|
| 45 | {
|
---|
| 46 | DEL_PTR(sResourceManager);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /** Creates new entry with a unique id
|
---|
| 50 | */
|
---|
| 51 | T *CreateResource()
|
---|
| 52 | {
|
---|
[1141] | 53 | const unsigned int id = CreateUniqueId();
|
---|
[1001] | 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 | */
|
---|
[1141] | 65 | T *FindEntry(const unsigned int id) const
|
---|
[1001] | 66 | {
|
---|
[1141] | 67 | std::map<unsigned int, T *>::const_iterator mit = mEntries.find(id);
|
---|
[1001] | 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 | */
|
---|
[1141] | 80 | bool DestroyEntry(const unsigned int id)
|
---|
[1001] | 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 |
|
---|
[2176] | 95 | bool ExportEntries(const std::string &filename)
|
---|
[1197] | 96 | {
|
---|
| 97 | ofstream stream(filename.c_str(), ios::binary);
|
---|
| 98 |
|
---|
| 99 | if (!stream.is_open())
|
---|
| 100 | return false;
|
---|
[1001] | 101 |
|
---|
[1197] | 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 |
|
---|
[1001] | 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 | {
|
---|
[1141] | 132 | std::map<unsigned int, T *>::iterator mit, mit_end = mEntries.end();
|
---|
[1001] | 133 |
|
---|
| 134 | for (mit = mEntries.begin(); mit != mEntries.end(); ++ mit)
|
---|
| 135 | {
|
---|
[1002] | 136 | //cout << "mesh: " << (*mit).first << " " << (*mit).second << endl;
|
---|
[1001] | 137 | DEL_PTR((*mit).second);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | /** Copy constructor.
|
---|
| 141 | */
|
---|
| 142 | ResourceManager<T>(const ResourceManager<T>& source)
|
---|
| 143 | {
|
---|
| 144 | }
|
---|
[2542] | 145 | /** Helper function returning unique id for resource.
|
---|
| 146 | */
|
---|
[1141] | 147 | static unsigned int CreateUniqueId()
|
---|
[1001] | 148 | {
|
---|
| 149 | return sCurrentId ++;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | protected:
|
---|
| 153 |
|
---|
| 154 | /// the resource container
|
---|
[1141] | 155 | std::map<unsigned int, T *> mEntries;
|
---|
[1001] | 156 |
|
---|
| 157 | private:
|
---|
| 158 |
|
---|
| 159 | static ResourceManager<T> *sResourceManager;
|
---|
[1141] | 160 | static unsigned int sCurrentId;
|
---|
[1001] | 161 | };
|
---|
| 162 |
|
---|
| 163 | // only instance of the resource manager
|
---|
| 164 | template <typename T> ResourceManager<T> *ResourceManager<T>::sResourceManager = NULL;
|
---|
[1141] | 165 | template <typename T> unsigned int ResourceManager<T>::sCurrentId = 0;
|
---|
[1001] | 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
|
---|