#pragma once

#include <map>
#include <set>
#include "Serialization.h"

// simple template class used to support OBSE custom data types (strings, arrays, etc)

template <class Var>
class VarMap
{
protected:
	UInt32	GetUnusedID()
	{
		UInt32 id = 1;
		while (vars.find(id) != vars.end())
		{
			id++;
		}
		return id;
	}

	std::map<UInt32, Var*>	vars;
	std::set<UInt32>		tempVars;		// set of IDs of temporary vars, makes for easy cleanup

public:
	VarMap()
	{
		//
	}

	~VarMap()
	{
		//
	}

	Var*	Get(double varID)
	{
		if (varID == 0 || vars.find(varID) == vars.end())
			return NULL;
		else
			return vars[varID];
	}

	bool	VarExists(UInt32 varID)
	{
		if (varID == 0)
			return false;
		else if (Get(varID))
			return true;
		else
		{
			_MESSAGE("Internal Error: initialized variable %d not found in varmap", varID);
			return false;
		}
	}

	void	Delete(UInt32 varID)
	{
		Var* var = Get(varID);
		if (var)
		{
			delete var;
			vars.erase(varID);
		}
	}

	void Reset(OBSESerializationInterface* intfc)
	{
		for (std::map<UInt32, Var*>::iterator iter = vars.begin();
				iter != vars.end();
				++iter)
		{
			delete iter->second;
		}

		vars.clear();
	}

	void	MarkTemporary(UInt32 varID, bool bTemporary)
	{
		if (bTemporary)
			tempVars.insert(varID);
		else
			tempVars.erase(varID);
	}

	bool IsTemporary(UInt32 varID)
	{
		return (tempVars.find(varID) != tempVars.end()) ? true : false;
	}
};