Ryujinx/Ryujinx.HLE/HOS/IdDictionary.cs

75 lines
1.5 KiB
C#
Raw Normal View History

2018-02-04 18:08:20 -05:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Ryujinx.HLE.HOS
2018-02-04 18:08:20 -05:00
{
class IdDictionary
2018-02-04 18:08:20 -05:00
{
private ConcurrentDictionary<int, object> _objs;
2018-02-04 18:08:20 -05:00
public ICollection<object> Values => _objs.Values;
public IdDictionary()
{
_objs = new ConcurrentDictionary<int, object>();
2018-02-04 18:08:20 -05:00
}
public bool Add(int id, object data)
{
return _objs.TryAdd(id, data);
}
public int Add(object data)
{
for (int id = 1; id < int.MaxValue; id++)
2018-02-04 18:08:20 -05:00
{
if (_objs.TryAdd(id, data))
{
return id;
}
2018-02-04 18:08:20 -05:00
}
throw new InvalidOperationException();
2018-02-04 18:08:20 -05:00
}
public object GetData(int id)
{
if (_objs.TryGetValue(id, out object data))
{
return data;
}
return null;
}
public T GetData<T>(int id)
2018-02-04 18:08:20 -05:00
{
if (_objs.TryGetValue(id, out object data) && data is T)
2018-02-04 18:08:20 -05:00
{
return (T)data;
2018-02-04 18:08:20 -05:00
}
return default(T);
}
public object Delete(int id)
2018-02-04 18:08:20 -05:00
{
if (_objs.TryRemove(id, out object obj))
2018-02-04 18:08:20 -05:00
{
return obj;
2018-02-04 18:08:20 -05:00
}
return null;
2018-02-04 18:08:20 -05:00
}
public ICollection<object> Clear()
2018-02-04 18:08:20 -05:00
{
ICollection<object> values = _objs.Values;
2018-02-04 18:08:20 -05:00
_objs.Clear();
return values;
2018-02-04 18:08:20 -05:00
}
}
}