You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
548 B
32 lines
548 B
1 year ago
|
using System.Collections.Generic;
|
||
|
|
||
|
namespace gaemstone.Utility;
|
||
|
|
||
|
public static class CallbackContextHelper
|
||
|
{
|
||
|
private static readonly Dictionary<nint, object> _contexts = new();
|
||
|
private static nint _counter = 0;
|
||
|
|
||
|
public static nint Create<T>(T context) where T : notnull
|
||
|
{
|
||
|
lock (_contexts) {
|
||
|
var id = _counter++;
|
||
|
_contexts.Add(id, context);
|
||
|
return id;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static T Get<T>(nint id)
|
||
|
{
|
||
|
lock (_contexts)
|
||
|
return (T)_contexts[id];
|
||
|
}
|
||
|
|
||
|
public static void Free(nint id)
|
||
|
{
|
||
|
lock (_contexts)
|
||
|
_contexts.Remove(id);
|
||
|
}
|
||
|
|
||
|
}
|