using System.Collections.Generic;
namespace Mhf.Server.Common.Instance
{
///
/// Provides Unique Ids for instancing.
///
public class InstanceGenerator
{
private readonly object _lock;
private uint _currentId;
private readonly Dictionary _instances;
public InstanceGenerator()
{
_lock = new object();
_currentId = 0;
_instances = new Dictionary();
}
public void AssignInstance(IInstance instance)
{
uint id;
lock (_lock)
{
id = _currentId;
_currentId++;
}
_instances.Add(id, instance);
instance.InstanceId = id;
}
public T CreateInstance() where T : IInstance, new()
{
uint id;
lock (_lock)
{
id = _currentId;
_currentId++;
}
T instance = new T();
_instances.Add(id, instance);
instance.InstanceId = id;
return instance;
}
public IInstance GetInstance(uint id)
{
if (!_instances.ContainsKey(id))
{
return null;
}
return _instances[id];
}
}
}