Subversion Repositories gelsvn

Rev

Rev 129 | Rev 595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
448 jab 1
#ifndef __UTIL_RESOURCE_MANAGER_H
2
#define __UTIL_RESOURCE_MANAGER_H
39 bj 3
 
56 jab 4
#include <cassert>
39 bj 5
#include <string>
6
#include <list>
7
#include <typeinfo>
8
 
9
#define CLEAN_SHUTDOWN 1
10
 
56 jab 11
namespace Util
39 bj 12
{
13
 
14
	typedef unsigned char FlagByte;
15
	const FlagByte REMOVE_WHEN_UNUSED = 0x01;
16
	const FlagByte STATIC_RESOURCE    = 0x02;
17
 
89 jab 18
	/** \brief This class template represents a resource record. 
19
 
20
	    There is a pointer to 
39 bj 21
			the actual resource (of type RES) and a usage counter which should never
22
			be less than 0. */
23
	template<class RES>
24
	class ResourceRecord
25
	{
26
		/// Name of the resource (const)
27
    const std::string name;
28
 
29
		/// A pointer to the resource (the pointer not the pointee is const)
30
		RES * const res;
31
 
32
		/// Usage counter.
33
		int usage;
34
 
35
		/// Flags
36
		FlagByte flags;
37
 
38
	public:
39
 
40
		/// Construct a null record.
41
		ResourceRecord(): res(0), usage(0), flags(0) {}
42
 
43
		/// Construct a resource record with a name and a pointer.
44
		ResourceRecord(const std::string& _name, RES* _res, bool static_res=false): 
45
			name(_name), 
46
			res(_res), 
47
			usage(0), 
48
			flags(static_res ? STATIC_RESOURCE : 0) 
49
		{
50
			assert(res != 0);
51
		}
52
 
53
		~ResourceRecord() 
54
		{
55
#if CLEAN_SHUTDOWN
56
			assert(usage==0);		
57
#endif
58
		}
59
 
60
		void erase_resource()
61
		{
62
			assert(usage==0);
63
			if(!(flags&STATIC_RESOURCE)) 
64
				{
65
					delete res;
66
				}
67
		}
68
 
69
		/// Increment the usage counter
70
		void increment_usage() 
71
		{
72
			assert(usage>=0);
73
			++usage;
74
		}
75
 
56 jab 76
		/// Decrement the usage counter. assert that counter is >0
39 bj 77
		void decrement_usage() 
78
		{
79
			assert(usage>0);
80
			--usage;
81
		}
82
 
83
		/// Return the usage count (mostly for debugging)
84
		int get_usage() const { return usage; }
85
 
86
		/// Get the name of a resource
87
		const std::string& get_name() const {return name;}
88
 
89
		/// Get a pointer to the resource (const pointer)
90
		RES * const get_ptr() 
91
		{
92
			return res;
93
		}
94
 
95
		/// Get a resource pointer (const pointer and pointee)
96
		const RES * const get_ptr() const 
97
		{
98
			return res;
99
		}
100
 
101
		void remove_when_unused() 
102
		{
103
/* 			assert(!(flags&STATIC_RESOURCE)); */
104
/* 			if(!(flags&STATIC_RESOURCE)) */
105
				flags = flags|REMOVE_WHEN_UNUSED;
106
		}
107
 
108
		FlagByte get_flags() const { return flags;}
109
 
110
	};
111
 
112
 
113
	template<class RES> class ResourceManager;
114
 
115
 
89 jab 116
	/** \brief Template for a pointer to a reference counted resource.
117
 
118
      The ResourcePtr class template is a template for a reference
39 bj 119
			counted resource pointer. It is a smart pointer that actually points
120
			to a ResourceRecord and not to the actual resource. Since the record
121
			contains a reference count, we can increase the reference count when
122
			the pointer is copied and decrease it in the destructor. Only the
123
			ResourceManager can create ResourcePtr's directly from a raw
124
			ResourceRecord. */
125
	template<class RES>
126
	class ResourcePtr
127
	{
128
		typedef ResourceRecord<RES> RR;
129
		typedef std::list<RR> RRList;
130
		typedef typename RRList::iterator RRListIter;
131
 
129 jab 132
		static RRListIter get_null_rrlist_iter()
133
		{
134
			static RRList l;
135
			return l.end();
136
		}
137
 
138
#define NULL_RRLIST_ITER ResourcePtr<RES>::get_null_rrlist_iter() 		
139
 
39 bj 140
		friend class ResourceManager<RES>;
141
 
129 jab 142
	private:		
39 bj 143
		RRListIter rr;
144
		RES* res;
145
 
146
		/** Explicit constructor with value. The constructor can only be called
147
				by a friend, i.e. the resource manager. */
148
		explicit ResourcePtr(const RRListIter& _rr): rr(_rr), res(0)
149
		{
129 jab 150
			if(rr != NULL_RRLIST_ITER)
39 bj 151
				{
152
					rr->increment_usage();
153
					res = rr->get_ptr();
154
				}
155
		}
156
 
157
		void decrement_usage();
158
 
159
	public:
160
 
129 jab 161
		ResourcePtr(): rr(NULL_RRLIST_ITER), res(0) {}
39 bj 162
 
163
		ResourcePtr(const ResourcePtr& r2): rr(r2.rr), res(0)
164
		{
129 jab 165
			if(rr != NULL_RRLIST_ITER)
39 bj 166
				{
167
					rr->increment_usage();
168
					res = rr->get_ptr();
169
				}
170
		}
171
 
172
		const ResourcePtr& operator=(const ResourcePtr& r2) 
173
		{
174
			// Guard against self-assignment
175
			if (r2.rr != this->rr)
176
				{
129 jab 177
					if(rr != NULL_RRLIST_ITER)	decrement_usage();
39 bj 178
 
179
					rr  = r2.rr;
180
					res = 0;
181
 
129 jab 182
					if(rr != NULL_RRLIST_ITER)
39 bj 183
						{
184
							res = rr->get_ptr();
185
							rr->increment_usage();
186
						}
187
				}
188
			return *this;
189
		}
190
 
191
		~ResourcePtr() {decrement_usage();}
192
 
193
		RES& operator*() const 
194
		{
129 jab 195
			assert(rr != NULL_RRLIST_ITER);
39 bj 196
			assert(res != 0);
197
			return *res;
198
		}
199
 
200
		RES* const operator->() const 
201
		{
129 jab 202
			assert(rr != NULL_RRLIST_ITER);
39 bj 203
			assert(res !=0);
204
			return res;
205
		}
206
 
207
		RES* const get_raw_ptr() const 
208
		{
129 jab 209
			assert(rr != NULL_RRLIST_ITER);
39 bj 210
			assert(res != 0);
211
			return res;
212
		}
213
 
214
		int usage() const
215
		{
129 jab 216
			if(rr != NULL_RRLIST_ITER)
39 bj 217
				return rr->get_usage();
218
			return -1;
219
		}
220
 
129 jab 221
		bool is_valid() const {return rr != NULL_RRLIST_ITER;}
39 bj 222
 
223
		void relinquish_resource()
224
		{
225
			ResourcePtr p;
226
			*this = p;
227
		}
228
 
229
		/** Calling this function sets the REMOVE_WHEN_UNUSED flag. If this 
230
				flag is set, the resource record is removed from the manager */
231
		void remove_when_unused() {	rr->remove_when_unused(); }
232
	};
233
 
234
 
89 jab 235
	/** \brief Resource manager class.
236
 
237
      The ResourceManager is a singleton, and it uses Scott Meyers construct
39 bj 238
			on first use idiom, i.e. the static function get_instance() will
239
			construct it when first called. There may be a problem with this
240
			idion if resources depend on each other. However, that is only
241
			when the program shuts down.  See modern C++ design by
242
			Alexandrescu for more information.
243
 
244
			I'll try to make everything in this class private and accessed
245
			by a simple interface of friend functions.
246
 
247
	*/
248
 
249
	template<class RES>
250
	class ResourceManager
251
	{
252
		typedef ResourceRecord<RES> RR;
253
		typedef std::list<RR> RRList;
254
		typedef typename RRList::iterator RRListIter;
255
		RRList resources;
256
 
257
		ResourceManager(): resources(0) {}
258
		ResourceManager(const ResourceManager&);
259
		ResourceManager& operator=(const ResourceManager&);
260
 
261
	public:
262
 
263
 
264
		~ResourceManager()
265
		{
266
#if CLEAN_SHUTDOWN
267
			RRListIter i = resources.begin(); 
268
			while(i != resources.end())
269
				{
270
					if(i->get_usage()==0)
271
						{
272
							RRListIter tmp = i;
273
							++i;
274
							erase_resource(tmp);
275
						}
276
					else
277
						{
278
							std::cout << "Warning, ResourceManager:\n\n"
279
												<< (typeid(this).name())
280
												<< "\n\nis shutting down, and resource \n\n" 
281
												<< i->get_name() << "\n\nhas usage: " << i->get_usage()
282
												<< std::endl;
283
							std::cout <<
284
 								"In other words, this resource is not unused at this\n"
285
								"point. That is unfortunate because then it cannot\n"
286
								"be deleted (since it might be used by some other\n"
287
								"part of the program during shutdown). Please ensure\n"
288
								"that all resources are unused at program\n"
289
								"termination. If you use a global ResourcePtr, this is\n"
290
								"done by calling relinquish_resource just before\n"
291
								"exiting.\n"
292
												<< std::endl;
293
							++i;
294
						}
295
				}
296
#endif
297
		}
298
 
299
		int get_no_resources() const
300
		{
301
			return resources.size();
302
		}
303
 
304
		static ResourceManager& get_instance() 
305
		{
306
			static ResourceManager instance;
307
			return instance;
308
		}
309
 
310
		void erase_resource(RRListIter iter)
311
		{
312
			iter->erase_resource();
313
			resources.erase(iter);
314
		}
315
 
316
		/** Find a resource and return a ResourcePtr to it. Returns the 
317
 
318
		ResourcePtr<RES> get_resource_ptr(const std::string& str)
319
		{
320
			for(RRListIter i = resources.begin(); i != resources.end(); ++i)
321
				if((*i).get_name() == str) 
322
					return ResourcePtr<RES>(i);
129 jab 323
			return ResourcePtr<RES>(NULL_RRLIST_ITER);
39 bj 324
		}
325
 
326
		/** Add a resource with name passed as first argument and a pointer
327
				passed as 2nd argument. A ResourcePtr to the just inserted
328
				element is returned. */
329
		ResourcePtr<RES> register_resource(const std::string& str, 
330
																			 RES* res, bool static_resource)
331
		{
332
			for(RRListIter i = resources.begin(); i != resources.end(); ++i)
333
				if(i->get_name() == str)
129 jab 334
					return (ResourcePtr<RES>(NULL_RRLIST_ITER));
39 bj 335
 
336
			resources.push_front(RR(str, res, static_resource));
337
			ResourcePtr<RES> ptr = ResourcePtr<RES>(resources.begin());
338
			return ptr;
339
		}
340
 
341
/*		friend class ResourcePtr<RES>;
342
		friend int get_no_resources<RES>();
343
		friend ResourcePtr<RES> get_resource_ptr<RES>(const std::string& str); 
344
		friend ResourcePtr<RES> register_static_resource<RES>(const std::string&,
345
																													RES*); 
346
		friend ResourcePtr<RES> register_dynamic_resource<RES>(const std::string&,
347
																													 RES*);*/ 
348
	};
349
 
350
	template<class RES>
351
	inline void ResourcePtr<RES>::decrement_usage()
352
	{
129 jab 353
		assert( (rr == NULL_RRLIST_ITER) || (res != 0));
354
		if(rr != NULL_RRLIST_ITER)
39 bj 355
			{
356
				rr->decrement_usage();
357
				if(rr->get_usage() == 0 && (rr->get_flags()&REMOVE_WHEN_UNUSED))
358
					{
359
						ResourceManager<RES>& man = ResourceManager<RES>::get_instance();
360
						man.erase_resource(rr);
361
					}
362
			}
363
	}
364
 
365
	template<class RES>
366
	inline int get_no_resources()
367
	{
368
		ResourceManager<RES>& man = ResourceManager<RES>::get_instance();
369
		return man.get_no_resources();
370
	}
371
 
372
	template<class RES>
373
	inline ResourcePtr<RES> get_resource_ptr(const std::string& str)
374
	{
375
		ResourceManager<RES>& man = ResourceManager<RES>::get_instance();
376
		return man.get_resource_ptr(str);
377
	}
378
 
379
	template<class RES>
380
	inline ResourcePtr<RES> register_dynamic_resource(const std::string& str,RES* res)
381
	{
382
		ResourceManager<RES>& man = ResourceManager<RES>::get_instance();
383
		ResourcePtr<RES> ptr = man.register_resource(str, res, false);
384
		return ptr;
385
	}
386
 
387
	template<class RES>
388
	inline ResourcePtr<RES> register_static_resource(const std::string& str,RES* res)
389
	{
390
		ResourceManager<RES>& man = ResourceManager<RES>::get_instance();
391
		ResourcePtr<RES> ptr = man.register_resource(str, res, true);
392
		return ptr;
393
	}
394
 
395
}
396
 
397
 
398
#endif