2024-05-25 11:16:37 +02:00
|
|
|
namespace ServicePoint;
|
2024-05-13 00:17:40 +02:00
|
|
|
|
2024-05-25 11:16:37 +02:00
|
|
|
public abstract class SpNativeInstance<T>
|
2024-05-13 00:17:40 +02:00
|
|
|
: IDisposable
|
|
|
|
where T : unmanaged
|
|
|
|
{
|
|
|
|
private unsafe T* _instance;
|
|
|
|
|
|
|
|
internal unsafe T* Instance
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_instance == null)
|
|
|
|
throw new NullReferenceException("instance is null");
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-25 11:16:37 +02:00
|
|
|
private protected unsafe SpNativeInstance(T* instance)
|
2024-05-13 00:17:40 +02:00
|
|
|
{
|
|
|
|
ArgumentNullException.ThrowIfNull(instance);
|
|
|
|
_instance = instance;
|
|
|
|
}
|
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
private protected abstract void Dealloc();
|
2024-05-13 00:17:40 +02:00
|
|
|
|
|
|
|
internal unsafe T* Into()
|
|
|
|
{
|
|
|
|
var instance = _instance;
|
|
|
|
_instance = null;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private unsafe void ReleaseUnmanagedResources()
|
|
|
|
{
|
|
|
|
if (_instance != null)
|
|
|
|
Dealloc();
|
|
|
|
_instance = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
ReleaseUnmanagedResources();
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2024-05-25 11:16:37 +02:00
|
|
|
~SpNativeInstance()
|
2024-05-13 00:17:40 +02:00
|
|
|
{
|
|
|
|
ReleaseUnmanagedResources();
|
|
|
|
}
|
|
|
|
}
|