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

Revision 2176, 3.6 KB checked in by mattausch, 18 years ago (diff)

removed using namespace std from .h

RevLine 
[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
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
[1004]40        /** We also want full control over the delete.
41        */
[1001]42        static void DelSingleton()
43        {
44                DEL_PTR(sResourceManager);
45        }
46
47        /** Creates new entry with a unique id
48        */
49        T *CreateResource()
50        {
[1141]51                const unsigned int id = CreateUniqueId();
[1001]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        */
[1141]63        T *FindEntry(const unsigned int id) const
[1001]64        {
[1141]65                std::map<unsigned int, T *>::const_iterator mit = mEntries.find(id);
[1001]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        */
[1141]78        bool DestroyEntry(const unsigned int id)
[1001]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       
[2176]93        bool ExportEntries(const std::string &filename)
[1197]94        {
95                ofstream stream(filename.c_str(), ios::binary);
96       
97                if (!stream.is_open())
98                        return false;
[1001]99
[1197]100                std::map<unsigned int, T *>::const_iterator mit, mit_end = mEntries.end();
101
102                for (mit = mEntries.begin(); mit != mit_end; ++ mit)
103                {
104                        int id = (*mit).first;
105                        stream.write(reinterpret_cast<char *>(&id), sizeof(int));
106                }
107
108                return true;
109        }
110
[1001]111        /** Returns size of resource container.
112        */
113        const int GetSize()
114        {
115                return (int)mEntries.size();
116        }
117
118protected:
119
120        /** Default constructor. The constructor is protected
121                to have control over instantiation using the
122                singleton pattern.
123        */
124        ResourceManager<T>()
125        {}
126
127        /** Release resources.
128        */
129        ~ResourceManager<T>()
130        {
[1141]131                std::map<unsigned int, T *>::iterator mit, mit_end = mEntries.end();
[1001]132
133                for (mit = mEntries.begin(); mit != mEntries.end(); ++ mit)
134                {
[1002]135                        //cout << "mesh: " << (*mit).first << " " << (*mit).second << endl;
[1001]136                        DEL_PTR((*mit).second);
137                }
138        }
139 
140        /** Copy constructor.
141        */
142    ResourceManager<T>(const ResourceManager<T>& source)
143    {
144    }
145
146         /** Helper function returning unique id for resource.
147         */
[1141]148        static unsigned int CreateUniqueId()
[1001]149        {
150                return sCurrentId ++;
151        }
152
153protected:
154
155        /// the resource container
[1141]156        std::map<unsigned int, T *> mEntries;
[1001]157
158private:
159
160        static ResourceManager<T> *sResourceManager;
[1141]161        static unsigned int sCurrentId;
[1001]162};
163
164// only instance of the resource manager
165template <typename T> ResourceManager<T> *ResourceManager<T>::sResourceManager = NULL;
[1141]166template <typename T> unsigned int ResourceManager<T>::sCurrentId = 0;
[1001]167
168/// This type is responsible for creation and disposal the meshes
169typedef ResourceManager<Mesh> MeshManager;
170/// This type is esponsible for creation and disposal of the materials.
171typedef ResourceManager<Material> MaterialManager;
172
173}
174
175#endif
Note: See TracBrowser for help on using the repository browser.