view LightClone/Source/FixedStack.h @ 69:d1be174e5585

Adding fixed stack for game screens
author koryspansel <koryspansel@bendbroadband.com>
date Mon, 10 Oct 2011 09:49:54 -0700
parents
children ffaeccdc105e
line wrap: on
line source

/*
 * FixedStack
 */

#ifndef __FIXEDSTACK_H__
#define __FIXEDSTACK_H__

#include "Types.h"
#include "Debug.h"

/*
 * FixedStack
 */
template<typename Type, uint32 Capacity>
class FixedStack
{
	/*
	 * Bottom
	 */
	static const int32 Bottom = (int32)Capacity - 1;

	/*
	 * kStack
	 */
	Type kStack[Capacity];

	/*
	 * nTop
	 */
	int32 nTop;

public:

	/*
	 * FixedStack
	 */
	FixedStack() : nTop(-1)
	{
	}

	/*
	 * Push
	 */
	ErrorCode Push(Type kValue)
	{
		ErrorCode eCode = Error_Success;

		if(nTop < Bottom)
		{
			kStack[++nTop] = kValue;
		}

		return eCode;
	}

	/*
	 * Pop
	 */
	Type Pop()
	{
		ErrorCode eCode = Error_Fail;
		return eCode;
	}

	/*
	 * Peek
	 */
	const Type& Peek()
	{
	}

	/*
	 * Clear
	 */
	void Clear()
	{
		nTop = -1;
	}

	/*
	 * Size
	 */
	uint32 Size() const
	{
		return nSize;
	}

	/*
	 * Find
	 */
	int32 Find(Type& kValue) const
	{
		for(uint32 i = 0; i < nSize; ++i)
		{
			if(pList[i] == kValue)
			{
				return (int32)i;
			}
		}

		return -1;
	}

	/*
	 * operator []
	 */
	Type& operator[](uint32 nIndex)
	{
		ASSERT(nIndex < nSize);
		return pList[nIndex];
	}

	/*
	 * operator []
	 */
	const Type& operator[](uint32 nIndex) const
	{
		ASSERT(nIndex < nSize);
		return pList[nIndex];
	}

private:

	/*
	 * Resize
	 */
	ErrorCode Resize(uint32 nAmount)
	{
		if(nAmount > nCapacity)
		{
			Type* pArray = new Type[2 * nAmount];

			if(pList)
			{
				for(uint32 i = 0; i < nSize; ++i)
				{
					pArray[i] = pList[i];
				}

				delete[] pList;
			}

			pList		= pArray;
			nCapacity	= 2 * nAmount;
		}

		return Error_Success;
	}
};

#endif //__FIXEDSTACK_H__