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

Revision 2575, 3.8 KB checked in by bittner, 17 years ago (diff)

big merge: preparation for havran ray caster, check if everything works

RevLine 
[1001]1#ifndef _ResourceManager_H__
2#define _ResourceManager_H__
3
4#include <vector>
5#include <string>
[2575]6#include <iterator>
7#include <fstream>
8#include <ostream>
9#include <iostream>
[1001]10
11//#include "Containers.h"
12#include "Mesh.h"
13#include "Material.h"
14
[2542]15
16
[1001]17namespace GtpVisibilityPreprocessor {
18
[2575]19using std::iterator;
20using std::map;
21 
[1001]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*/
32template <typename T> class ResourceManager
33{
[2575]34protected:
35  typedef map<unsigned int, T* > maptype;
[1001]36
[2575]37  /// the resource container
38  maptype mEntries;
39 
[1001]40public:
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
[1004]54        /** We also want full control over the delete.
55        */
[1001]56        static void DelSingleton()
57        {
58                DEL_PTR(sResourceManager);
59        }
60
61        /** Creates new entry with a unique id
62        */
63        T *CreateResource()
64        {
[1141]65                const unsigned int id = CreateUniqueId();
[1001]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        */
[1141]77        T *FindEntry(const unsigned int id) const
[1001]78        {
[2575]79          typename maptype::const_iterator mit = mEntries.find(id);
[1001]80
[2575]81          if (mit != mEntries.end())
82            {
83              return (*mit).second;
84            }
[1001]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        */
[1141]92        bool DestroyEntry(const unsigned int id)
[1001]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       
[2176]107        bool ExportEntries(const std::string &filename)
[1197]108        {
[2575]109          std::ofstream stream(filename.c_str(), std::ios::binary);
[1197]110       
111                if (!stream.is_open())
112                        return false;
[1001]113
[2575]114                typename std::map<unsigned int, T *>::const_iterator mit, mit_end = mEntries.end();
[1197]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
[1001]125        /** Returns size of resource container.
126        */
127        const int GetSize()
128        {
129                return (int)mEntries.size();
130        }
131
132protected:
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        {
[2575]144          typename std::map<unsigned int, T *>::iterator mit, mit_end = mEntries.end();
[1001]145
146                for (mit = mEntries.begin(); mit != mEntries.end(); ++ mit)
147                {
[1002]148                        //cout << "mesh: " << (*mit).first << " " << (*mit).second << endl;
[1001]149                        DEL_PTR((*mit).second);
150                }
151        }
152        /** Copy constructor.
153        */
154    ResourceManager<T>(const ResourceManager<T>& source)
155    {
156    }
[2542]157        /** Helper function returning unique id for resource.
158        */
[1141]159        static unsigned int CreateUniqueId()
[1001]160        {
161                return sCurrentId ++;
162        }
163
164protected:
165
166private:
167
168        static ResourceManager<T> *sResourceManager;
[1141]169        static unsigned int sCurrentId;
[1001]170};
171
172// only instance of the resource manager
173template <typename T> ResourceManager<T> *ResourceManager<T>::sResourceManager = NULL;
[1141]174template <typename T> unsigned int ResourceManager<T>::sCurrentId = 0;
[1001]175
176/// This type is responsible for creation and disposal the meshes
177typedef ResourceManager<Mesh> MeshManager;
178/// This type is esponsible for creation and disposal of the materials.
179typedef ResourceManager<Material> MaterialManager;
180
181}
182
183#endif
Note: See TracBrowser for help on using the repository browser.