Subversion Repositories gelsvn

Rev

Rev 89 | Rev 448 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 89 Rev 129
Line 127... Line 127...
127
	{
127
	{
128
		typedef ResourceRecord<RES> RR;
128
		typedef ResourceRecord<RES> RR;
129
		typedef std::list<RR> RRList;
129
		typedef std::list<RR> RRList;
130
		typedef typename RRList::iterator RRListIter;
130
		typedef typename RRList::iterator RRListIter;
131
 
131
 
-
 
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
 
132
		friend class ResourceManager<RES>;
140
		friend class ResourceManager<RES>;
133
 
141
 
134
	private:
142
	private:		
135
		RRListIter rr;
143
		RRListIter rr;
136
		RES* res;
144
		RES* res;
137
	
145
	
138
		/** Explicit constructor with value. The constructor can only be called
146
		/** Explicit constructor with value. The constructor can only be called
139
				by a friend, i.e. the resource manager. */
147
				by a friend, i.e. the resource manager. */
140
		explicit ResourcePtr(const RRListIter& _rr): rr(_rr), res(0)
148
		explicit ResourcePtr(const RRListIter& _rr): rr(_rr), res(0)
141
		{
149
		{
142
			if(rr != 0)
150
			if(rr != NULL_RRLIST_ITER)
143
				{
151
				{
144
					rr->increment_usage();
152
					rr->increment_usage();
145
					res = rr->get_ptr();
153
					res = rr->get_ptr();
146
				}
154
				}
147
		}
155
		}
148
 
156
 
149
		void decrement_usage();
157
		void decrement_usage();
150
	
158
	
151
	public:
159
	public:
152
	
160
	
153
		ResourcePtr(): rr(0), res(0) {}
161
		ResourcePtr(): rr(NULL_RRLIST_ITER), res(0) {}
154
	
162
	
155
		ResourcePtr(const ResourcePtr& r2): rr(r2.rr), res(0)
163
		ResourcePtr(const ResourcePtr& r2): rr(r2.rr), res(0)
156
		{
164
		{
157
			if(rr != 0)
165
			if(rr != NULL_RRLIST_ITER)
158
				{
166
				{
159
					rr->increment_usage();
167
					rr->increment_usage();
160
					res = rr->get_ptr();
168
					res = rr->get_ptr();
161
				}
169
				}
162
		}
170
		}
Line 164... Line 172...
164
		const ResourcePtr& operator=(const ResourcePtr& r2) 
172
		const ResourcePtr& operator=(const ResourcePtr& r2) 
165
		{
173
		{
166
			// Guard against self-assignment
174
			// Guard against self-assignment
167
			if (r2.rr != this->rr)
175
			if (r2.rr != this->rr)
168
				{
176
				{
169
					if(rr != 0)	decrement_usage();
177
					if(rr != NULL_RRLIST_ITER)	decrement_usage();
170
 
178
 
171
					rr  = r2.rr;
179
					rr  = r2.rr;
172
					res = 0;
180
					res = 0;
173
 
181
 
174
					if(rr != 0)
182
					if(rr != NULL_RRLIST_ITER)
175
						{
183
						{
176
							res = rr->get_ptr();
184
							res = rr->get_ptr();
177
							rr->increment_usage();
185
							rr->increment_usage();
178
						}
186
						}
179
				}
187
				}
Line 182... Line 190...
182
 
190
 
183
		~ResourcePtr() {decrement_usage();}
191
		~ResourcePtr() {decrement_usage();}
184
 
192
 
185
		RES& operator*() const 
193
		RES& operator*() const 
186
		{
194
		{
187
			assert(rr != 0);
195
			assert(rr != NULL_RRLIST_ITER);
188
			assert(res != 0);
196
			assert(res != 0);
189
			return *res;
197
			return *res;
190
		}
198
		}
191
 
199
 
192
		RES* const operator->() const 
200
		RES* const operator->() const 
193
		{
201
		{
194
			assert(rr != 0);
202
			assert(rr != NULL_RRLIST_ITER);
195
			assert(res !=0);
203
			assert(res !=0);
196
			return res;
204
			return res;
197
		}
205
		}
198
 
206
 
199
		RES* const get_raw_ptr() const 
207
		RES* const get_raw_ptr() const 
200
		{
208
		{
201
			assert(rr != 0);
209
			assert(rr != NULL_RRLIST_ITER);
202
			assert(res != 0);
210
			assert(res != 0);
203
			return res;
211
			return res;
204
		}
212
		}
205
 
213
 
206
		int usage() const
214
		int usage() const
207
		{
215
		{
208
			if(rr != 0)
216
			if(rr != NULL_RRLIST_ITER)
209
				return rr->get_usage();
217
				return rr->get_usage();
210
			return -1;
218
			return -1;
211
		}
219
		}
212
 
220
 
213
		bool is_valid() const {return rr != 0;}
221
		bool is_valid() const {return rr != NULL_RRLIST_ITER;}
214
 
222
 
215
		void relinquish_resource()
223
		void relinquish_resource()
216
		{
224
		{
217
			ResourcePtr p;
225
			ResourcePtr p;
218
			*this = p;
226
			*this = p;
Line 310... Line 318...
310
		ResourcePtr<RES> get_resource_ptr(const std::string& str)
318
		ResourcePtr<RES> get_resource_ptr(const std::string& str)
311
		{
319
		{
312
			for(RRListIter i = resources.begin(); i != resources.end(); ++i)
320
			for(RRListIter i = resources.begin(); i != resources.end(); ++i)
313
				if((*i).get_name() == str) 
321
				if((*i).get_name() == str) 
314
					return ResourcePtr<RES>(i);
322
					return ResourcePtr<RES>(i);
315
			return ResourcePtr<RES>(0);
323
			return ResourcePtr<RES>(NULL_RRLIST_ITER);
316
		}
324
		}
317
 
325
 
318
		/** Add a resource with name passed as first argument and a pointer
326
		/** Add a resource with name passed as first argument and a pointer
319
				passed as 2nd argument. A ResourcePtr to the just inserted
327
				passed as 2nd argument. A ResourcePtr to the just inserted
320
				element is returned. */
328
				element is returned. */
321
		ResourcePtr<RES> register_resource(const std::string& str, 
329
		ResourcePtr<RES> register_resource(const std::string& str, 
322
																			 RES* res, bool static_resource)
330
																			 RES* res, bool static_resource)
323
		{
331
		{
324
			for(RRListIter i = resources.begin(); i != resources.end(); ++i)
332
			for(RRListIter i = resources.begin(); i != resources.end(); ++i)
325
				if(i->get_name() == str)
333
				if(i->get_name() == str)
326
					return (ResourcePtr<RES>(0));
334
					return (ResourcePtr<RES>(NULL_RRLIST_ITER));
327
 
335
 
328
			resources.push_front(RR(str, res, static_resource));
336
			resources.push_front(RR(str, res, static_resource));
329
			ResourcePtr<RES> ptr = ResourcePtr<RES>(resources.begin());
337
			ResourcePtr<RES> ptr = ResourcePtr<RES>(resources.begin());
330
			return ptr;
338
			return ptr;
331
		}
339
		}
Line 340... Line 348...
340
	};
348
	};
341
 
349
 
342
	template<class RES>
350
	template<class RES>
343
	inline void ResourcePtr<RES>::decrement_usage()
351
	inline void ResourcePtr<RES>::decrement_usage()
344
	{
352
	{
345
		assert( (rr == 0) || (res != 0));
353
		assert( (rr == NULL_RRLIST_ITER) || (res != 0));
346
		if(rr != 0)
354
		if(rr != NULL_RRLIST_ITER)
347
			{
355
			{
348
				rr->decrement_usage();
356
				rr->decrement_usage();
349
				if(rr->get_usage() == 0 && (rr->get_flags()&REMOVE_WHEN_UNUSED))
357
				if(rr->get_usage() == 0 && (rr->get_flags()&REMOVE_WHEN_UNUSED))
350
					{
358
					{
351
						ResourceManager<RES>& man = ResourceManager<RES>::get_instance();
359
						ResourceManager<RES>& man = ResourceManager<RES>::get_instance();