view LightClone/Source/FixedString.h @ 71:bc8afcf7e1ec

Refactor world into game screen
author koryspansel <koryspansel@bendbroadband.com>
date Tue, 11 Oct 2011 13:20:43 -0700
parents 3a63df04f3c0
children
line wrap: on
line source

/*
 * FixedString
 */

#ifndef __FIXEDSTRING_H__
#define __FIXEDSTRING_H__

#include "Types.h"
#include <string.h>

/*
 * FixedString
 */
template<uint32 Size = 64>
class FixedString
{
public:

	/*
	 * MaximumLength
	 */
	static const uint32 MaximumLength = Size;

	/*
	 * Hash
	 */
	struct Hash
	{
		/*
		 * operator()
		 */
		uint32 operator()(const FixedString& kString)
		{
			uint32 nHash = 5381;

			const char* pString = (const char*)kString;
			while(*pString)
			{
				nHash = ((nHash << 5) + nHash) + *pString++;
			}

			return nHash;
		}
	};

private:

	/*
	 * kString
	 */
	char kString[Size];

	/*
	 * nLength
	 */
	uint32 nLength;

public:

	/*
	 * FixedString
	 */
	FixedString() : nLength(0)
	{
		memset(kString, 0, sizeof(kString));
	}

	/*
	 * FixedString
	 */
	FixedString(const char* pValue) : nLength(0)
	{
		if(pValue)
		{
			while(*pValue)
			{
				kString[nLength++] = *pValue++;
			}
		}

		kString[nLength] = 0;
	}

	/*
	 * FixedString
	 */
	FixedString(const FixedString& kOther) : nLength(kOther.nLength)
	{
		for(uint32 i = 0 ; i < kOther.nLength; ++i)
		{
			kString[i] = kOther.kString[i];
		}

		kString[nLength] = 0;
	}

	/*
	 * operator =
	 */
	const FixedString& operator =(const FixedString& kOther)
	{
		nLength = kOther.nLength;

		for(uint32 i = 0 ; i < kOther.nLength; ++i)
		{
			kString[i] = kOther.kString[i];
		}

		kString[nLength] = 0;

		return *this;
	}

	/*
	 * Length
	 */
	uint32 Length() const
	{
		return nLength;
	}

	/*
	 * operator char*
	 */
	operator char*()
	{
		return kString;
	}

	/*
	 * operator const char*
	 */
	operator const char*() const
	{
		return kString;
	}
};

/*
 * operator ==
 */
template<uint32 Size>
bool operator ==(const FixedString<Size>& kStringA, const FixedString<Size>& kStringB)
{
	return kStringA.Length() == kStringB.Length() && strcmp(kStringA, kStringB) == 0;
}

/*
 * operator !=
 */
template<uint32 Size>
bool operator !=(const FixedString<Size>& kStringA, const FixedString<Size>& kStringB)
{
	return kStringA.Length() != kStringB.Length() || strcmp(kStringA, kStringB) != 0;
}

#endif //__FIXEDSTRING_H__