improvements to HandleDictionary

This commit is contained in:
Michael Becker 2021-10-27 07:41:15 -04:00
parent 313cc26a36
commit fd1c4bece4
No known key found for this signature in database
GPG Key ID: 98C333A81F18C22C

View File

@ -21,27 +21,53 @@
using System; using System;
namespace MBS.Framework.Collections.Generic namespace MBS.Framework.Collections.Generic
{ {
public class HandleDictionary<TObject> public class HandleDictionary<TObject> : HandleDictionary<TObject, IntPtr>
{ {
private BidirectionalDictionary<IntPtr, TObject> _dict = new BidirectionalDictionary<IntPtr, TObject>();
public TObject GetObject(IntPtr handle) }
public class HandleDictionary<TObject, THandle>
{
private BidirectionalDictionary<THandle, TObject> _dict = new BidirectionalDictionary<THandle, TObject>();
public int Count { get { return _dict.Count; } }
public TObject GetObject(THandle handle)
{ {
return _dict.GetValue2(handle); return _dict.GetValue2(handle);
} }
public IntPtr GetHandle(TObject obj) public THandle GetHandle(TObject obj)
{ {
return _dict.GetValue1(obj); return _dict.GetValue1(obj);
} }
public void Add(THandle handle, TObject obj)
public void Add(IntPtr handle, TObject obj)
{ {
_dict.Add(handle, obj); _dict.Add(handle, obj);
} }
/// <summary>
/// Removes the <typeparamref name="THandle" /> of the given <paramref name="obj" />.
/// </summary>
/// <param name="obj">The object to remove.</param>
public void Remove(TObject obj) public void Remove(TObject obj)
{ {
_dict.RemoveByValue2(obj); _dict.RemoveByValue2(obj);
} }
/// <summary>
/// Removes the <typeparamref name="TObject" /> represented by the given <paramref name="handle" />.
/// </summary>
/// <param name="handle">The handle of the <typeparamref name="TObject" /> to remove.</param>
public void Remove(THandle handle)
{
_dict.RemoveByValue1(handle);
}
}
public static class HandleDictionaryExtensions
{
public static ushort Add<TObject>(this HandleDictionary<TObject, ushort> dict, TObject obj)
{
ushort index = (ushort)(dict.Count + 1);
dict.Add(index, obj);
return index;
}
} }
} }