Mirror/Unity-Technologies-networking/Runtime/Telepathy/SafeDictionary.cs

53 lines
1.1 KiB
C#
Raw Normal View History

2018-08-14 09:13:02 +00:00
// replaces ConcurrentDictionary which is not available in .NET 3.5 yet.
using System.Collections.Generic;
using System.Linq;
namespace Telepathy
{
public class SafeDictionary<TKey,TValue>
{
Dictionary<TKey,TValue> dict = new Dictionary<TKey,TValue>();
public void Add(TKey key, TValue value)
{
lock(dict)
{
dict[key] = value;
}
}
public void Remove(TKey key)
{
lock(dict)
{
dict.Remove(key);
}
}
// can't check .ContainsKey before Get because it might change inbetween,
// so we need a TryGetValue
public bool TryGetValue(TKey key, out TValue result)
{
lock(dict)
{
return dict.TryGetValue(key, out result);
}
}
public List<TValue> GetValues()
{
lock(dict)
{
return dict.Values.ToList();
}
}
public void Clear()
{
lock(dict)
{
dict.Clear();
}
}
}
}