servicepoint/crates/servicepoint_binding_cs/ServicePoint/SpNativeInstance.cs

52 lines
1,013 B
C#
Raw Normal View History

namespace ServicePoint;
2024-05-13 00:17:40 +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;
}
}
private protected unsafe SpNativeInstance(T* instance)
2024-05-13 00:17:40 +02:00
{
ArgumentNullException.ThrowIfNull(instance);
_instance = instance;
}
2024-09-07 14:11:15 +02:00
private protected abstract void Free();
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)
2024-09-07 14:11:15 +02:00
Free();
2024-05-13 00:17:40 +02:00
_instance = null;
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~SpNativeInstance()
2024-05-13 00:17:40 +02:00
{
ReleaseUnmanagedResources();
}
}