/*
	ZStringBuffer.hpp
	Author: James Russell <jcrussell@762studios.com>
	Created: 10/19/2016

	Purpose: 

	The string buffer is a specialized lookup table for strings, designed to hold
	static strings for entity properties.  In doing so, properties need merely keep
	a reference key for a static string that is shared between entities and different
	simulations, reducing memory usage and network traffic.

	Dynamic strings are stored within the property buffer and are limited in size.

	It is recommended that all static strings be declared during the simulation
	initialization step, as no concurrency mechanisms are in place when looking up
	strings from string keys.  As such, there is no way of removing a string once
	added.

	This class is implemented as a singleton.

	License:

	Copyright 2016, 762 Studios.

*/

#pragma once

#ifndef _ZSTRINGBUFFER_HPP
#define _ZSTRINGBUFFER_HPP

#include "ZSimulationDefs.hpp"

class ZStringBuffer 
{
public:
	// typedef used to look up a static string
	typedef uint32_t StringKey;

	// singleton instance getter
	static ZStringBuffer* Instance() 
		{ if (_Instance == NULL) _Instance = new ZStringBuffer(); return _Instance; }

	// adds a string to the buffer
	StringKey AddString(const char* str);

	// given a string, finds the key (returns StringKey(-1) if not found)
	StringKey GetKey(const char* str);

	// given a key, finds the string (returns NULL if not found)
	const char* GetString(StringKey key);

private:
	static ZStringBuffer* _Instance;	// singleton instance

	char*  Buffer;			// the buffer all strings will be stored in
	size_t BufferSize;		// current size of the buffer
	size_t BufferOffset;	// current offset into the buffer

	ZArray<size_t> Offsets;	// string key as index into this array, which provides offset into buffer

	ZHashMap<SST_HashValue64, ZArray<StringKey>> Lookup;	// fast lookup for strings (narrows the search)

	// c'tor
	ZStringBuffer();

	// d'tor
	~ZStringBuffer();
};

#endif