source: GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h @ 1004

Revision 1004, 3.1 KB checked in by mattausch, 18 years ago (diff)

environment as a singleton

Line 
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
11namespace 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*/
23template <typename T> class ResourceManager
24{
25
26public:
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        /** We also want full control over the delete.
41        */
42        static void DelSingleton()
43        {
44                DEL_PTR(sResourceManager);
45        }
46
47        /** Creates new entry with a unique id
48        */
49        T *CreateResource()
50        {
51                const int id = CreateUniqueId();
52
53                T *resource = new T(id);
54                mEntries[id] = resource;
55               
56                return resource;
57        }
58
59       
60        /** Searches for the mesh with the given name.
61                @returns mesh or NULL if mesh was not found.
62        */
63        T *FindEntry(const int id) const
64        {
65                std::map<int, T *>::const_iterator mit = mEntries.find(id);
66
67                if (mit != mEntries.end())
68                {
69                        return (*mit).second;
70                }
71
72                return NULL;
73        }
74
75        /** Destroys mesh with the given name.
76                @returns true if the mesh was found, false if not
77        */
78        bool DestroyEntry(const int id)
79        {       
80                if (!FindEntry(id))
81                {
82                        T *resource = mEntries[id];
83
84                        mEntries.erase(id);
85                        DEL_PTR(resource);
86
87                        return true;
88                }
89               
90                return false;
91        }
92       
93
94        /** Returns size of resource container.
95        */
96        const int GetSize()
97        {
98                return (int)mEntries.size();
99        }
100
101protected:
102
103        /** Default constructor. The constructor is protected
104                to have control over instantiation using the
105                singleton pattern.
106        */
107        ResourceManager<T>()
108        {}
109
110        /** Release resources.
111        */
112        ~ResourceManager<T>()
113        {
114                std::map<int, T *>::iterator mit, mit_end = mEntries.end();
115
116                for (mit = mEntries.begin(); mit != mEntries.end(); ++ mit)
117                {
118                        //cout << "mesh: " << (*mit).first << " " << (*mit).second << endl;
119                        DEL_PTR((*mit).second);
120                }
121        }
122 
123        /** Copy constructor.
124        */
125    ResourceManager<T>(const ResourceManager<T>& source)
126    {
127    }
128
129         /** Helper function returning unique id for resource.
130         */
131        static int CreateUniqueId()
132        {
133                return sCurrentId ++;
134        }
135
136protected:
137
138        /// the resource container
139        std::map<int, T *> mEntries;
140
141private:
142
143        static ResourceManager<T> *sResourceManager;
144        static int sCurrentId;
145};
146
147// only instance of the resource manager
148template <typename T> ResourceManager<T> *ResourceManager<T>::sResourceManager = NULL;
149template <typename T> int ResourceManager<T>::sCurrentId = 0;
150
151/// This type is responsible for creation and disposal the meshes
152typedef ResourceManager<Mesh> MeshManager;
153/// This type is esponsible for creation and disposal of the materials.
154typedef ResourceManager<Material> MaterialManager;
155
156}
157
158#endif
Note: See TracBrowser for help on using the repository browser.