Added NativeObject base class

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-14 14:07:06 +01:00
1 親 d522ab7ca0
コミット d1d8cf8c6b
1ファイル変更
+52
+52
ファイルの表示
@@ -0,0 +1,52 @@
using System;
namespace ComputeSharp.Core.Interop
{
/// <summary>
/// Base class for a <see cref="IDisposable"/> class.
/// </summary>
public abstract class NativeObject : IDisposable
{
/// <summary>
/// Indicates whether or not the current instance has already been disposed.
/// </summary>
private bool isDisposed;
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations.
/// </summary>
~NativeObject()
{
CheckAndDispose(false);
}
/// <summary>
/// Releases all the native resources for the current instance.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
CheckAndDispose(true);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
private void CheckAndDispose(bool disposing)
{
if (!this.isDisposed)
{
this.isDisposed = true;
Dispose(disposing);
}
}
/// <summary>
/// Releases unmanaged and (optionally) managed resources.
/// </summary>
/// <param name="disposing">When set to <see langword="true"/>, indicates to dispose managed resources as well.</param>
protected abstract void Dispose(bool disposing);
}
}