⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 casteroidpool.h

📁 rps game symbian os application development
💻 H
字号:
#ifndef CASTEROIDPOOL_H_
#define CASTEROIDPOOL_H_

#include <e32std.h>
#include <e32base.h>
#include "WorldObjects.h"
#include "roids.pan"

	
//Forward
template<class T,TInt S> class TPoolIterator;

//class
template<class T, TInt S> 
class CObjectPool : public CBase
	{
public:

	CObjectPool(){iSize= S;};
	
	T* NewObject();
	T* NewObjectOrNull();	
	void FreeObject(T* aAsteroid);
	void Reset();
	inline TInt Count() const 
		{return iCount;};
	T* Object(TInt aIndex) const;
	TPoolIterator<T,S> Iterator()
		{
		return TPoolIterator<T,S>(this);
		}
	
private:
	friend	class TPoolIterator<T,S>;
	T			iObjects[S];
	TUint8		iFreeMap[S];
	TInt		iSize;
	TInt		iCount;
	};

template<class T, TInt S>
class TPoolIterator 
	{
public:
	TPoolIterator(CObjectPool<T,S>* aPool):
		iPool(aPool)
		{
		}
		
	T* First()
		{
		iIterator = 0;
		return Next();	
		}
		
	T* Next()
		{
		while(iPool->iFreeMap[iIterator]==EFalse)
			{		
			iIterator++;
			if(iIterator== iPool->iSize)
				return NULL;
			}
		return &iPool->iObjects[iIterator++];		
		}
private:
	TInt iIterator;
	CObjectPool<T,S>* iPool;
	};

template<class T, TInt S> T* CObjectPool<T, S>::Object(TInt aIndex) const
	{
	TInt count=0;
	for(TInt i=0; i<iSize; i++)
		{
		if(iFreeMap[i])
			count++;
		if(count == aIndex)
		return iObjects[count];
		}
	}

template<class T, TInt S> T* CObjectPool<T, S>::NewObject()
	{
	T* ret = NewObjectOrNull();
	__ASSERT_ALWAYS(ret!=NULL, Panic(ERoidsPoolExhausted));	
	return ret;
	}		
template<class T, TInt S> T* CObjectPool<T, S>::NewObjectOrNull()
	{
	for(TInt i=0; i<iSize; i++)
		{
		if(!iFreeMap[i])
			{
			iFreeMap[i] = ETrue;
			iCount++;
			return new(&iObjects[i]) T();
			}
		}
	return NULL; // never reached
	}

template<class T, TInt S> void CObjectPool<T, S>::FreeObject(T* aObject)
	{
	ASSERT(iCount!=0);
	for(TInt i=0; i<iSize; i++)
		{
		if(aObject == &iObjects[i])
			{
			iFreeMap[i] = EFalse;
			iCount--;
			return;
			}
		}
	ASSERT(EFalse);
	}

template<class T, TInt S> void CObjectPool<T, S>::Reset()
	{
	iCount=0;
	for(TInt i=0; i<iSize; i++)
		{
		iFreeMap[i] = EFalse;			
		}
	}   

#endif /*CASTEROIDPOOL_H_*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -