view LightClone/Source/Util.h @ 41:a986355cca5f

Move Tower structure into Environment
author koryspansel
date Thu, 22 Sep 2011 10:25:35 -0700
parents 7e3a0ae9c016
children
line wrap: on
line source

/*
 * Util
 */

#ifndef __UTIL_H__
#define __UTIL_H__

#include "Core.h"

/*
 * Buffer
 */
class Buffer
{
	/*
	 * pData
	 */
	uint8* pData;

	/*
	 * nSize
	 */
	uint32 nSize;

	/*
	 * nOffset
	 */
	uint32 nOffset;

public:

	/*
	 * Buffer
	 *	Construct a Buffer instance by taking ownership of the data
	 */
	Buffer(uint8* pBuffer, uint32 nLength, bool bCopy = true) : pData(pBuffer), nSize(nLength), nOffset(0)
	{
		if(bCopy)
		{
			pData = new uint8[nLength];

			for(uint32 i = 0; i < nLength; ++i)
				pData[i] = pBuffer[i];
		}
	}

	/*
	 * ~Buffer
	 */
	~Buffer()
	{
		delete[] pData;
	}

	/*
	 * Read
	 */
	template<typename Type>
	ErrorCode Read(Type* pValue)
	{
		if(pData)
		{
			const uint32 nLength = sizeof(Type);

			if(nOffset + nLength <= nSize)
			{
				if(pValue)
					*pValue = *(Type*)(pData + nOffset);
			
				nOffset += nLength;

				return Error_Success;
			}
		}

		return Error_Fail;
	}

	/*
	 * IsValid
	 */
	bool IsValid() const
	{
		return pData && nSize > 0;
	}
};

/*
 * LoadFile
 */
Buffer LoadFile(const char* pName);

#endif //__UTIL_H__