Code refactoring, moved TerraFX dep to .Graphics

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-25 01:32:40 +01:00
1 親 aa5c76253f
コミット 3f5460e0ca
5ファイル変更
+4 -1

検索条件に一致するファイルはありません

+4
ファイルの表示
@@ -6,6 +6,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta2-379749354" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputeSharp.Core\ComputeSharp.Core.csproj" />
</ItemGroup>
+65
ファイルの表示
@@ -0,0 +1,65 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using TerraFX.Interop;
namespace ComputeSharp.Core.Extensions
{
/// <summary>
/// Helper methods for working with the <see cref="ComPtr{T}"/> type.
/// </summary>
public static class ComPtrExtensions
{
/// <summary>
/// Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <see langword="void"/> double pointer.
/// </summary>
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to get the address for.</param>
/// <returns>The raw pointer to the input <see cref="ComPtr{T}"/> instance.</returns>
/// <remarks>This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void** GetVoidAddressOf<T>(this in ComPtr<T> ptr)
where T : unmanaged
{
return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in ptr));
}
/// <summary>
/// Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <see langword="void"/> double pointer.
/// </summary>
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to get the address for.</param>
/// <returns>The raw pointer to the input <see cref="ComPtr{T}"/> instance.</returns>
/// <remarks>This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ComPtr<T> Move<T>(this in ComPtr<T> ptr)
where T : unmanaged
{
ComPtr<T> copy = default;
Unsafe.AsRef(in ptr).Swap(ref copy);
return copy;
}
/// <summary>
/// Upcasts the current <see cref="ComPtr{T}"/> instance as a base type.
/// </summary>
/// <typeparam name="T">The type of the current <see cref="ComPtr{T}"/> instance.</typeparam>
/// <typeparam name="U">The type to cast the input instance to.</typeparam>
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to upcast.</param>
/// <returns>A <see cref="ComPtr{T}"/> instance of type <typeparamref name="U"/>.</returns>
/// <remarks>
/// This method is meant to be used in a chained expression and it does not increment the reference
/// count for the input pointer. Do not expose the return value of this extension to consumers.
/// </remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ref readonly ComPtr<U> Upcast<T, U>(this in ComPtr<T> ptr)
where T : unmanaged
where U : unmanaged
{
return ref Unsafe.As<ComPtr<T>, ComPtr<U>>(ref Unsafe.AsRef(in ptr));
}
}
}
+29
ファイルの表示
@@ -0,0 +1,29 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Diagnostics;
using HRESULT = System.Int32;
namespace ComputeSharp.Core.Extensions
{
/// <summary>
/// Helper methods to efficiently throw exceptions.
/// </summary>
[DebuggerStepThrough]
internal static class HResultExtensions
{
/// <summary>
/// Throws a <see cref="Win32Exception"/> if <paramref name="result"/> represents an error.
/// </summary>
/// <param name="result">The input <see cref="HRESULT"/> to check.</param>
/// <exception cref="Win32Exception">Thrown if <paramref name="result"/> represents an error.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Assert(this HRESULT result)
{
if (result < 0)
{
ThrowHelper.ThrowWin32Exception(result);
}
}
}
}
+69
ファイルの表示
@@ -0,0 +1,69 @@
using System;
using System.Runtime.CompilerServices;
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();
}
/// <summary>
/// Releases all the native resources for the current instance.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
CheckAndDispose();
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
private void CheckAndDispose()
{
if (!this.isDisposed)
{
this.isDisposed = true;
OnDispose();
}
}
/// <summary>
/// Releases unmanaged and (optionally) managed resources.
/// </summary>
protected abstract void OnDispose();
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/> if the current instance has been disposed.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
// We can't use ThrowHelper here as we only want to invoke ToString if we
// are about to throw an exception. The JIT will recognize this pattern
// as this method has a single basic block that always throws an exception.
void Throw() => throw new ObjectDisposedException(ToString());
Throw();
}
}
}
}