Add IComObject interface, update __uuidof<T>

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-23 16:59:26 +01:00
1 親 fb7fd8b0a4
コミット e3e99f5dd4
5ファイル変更
+25 -48

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

+1
ファイルの表示
@@ -57,6 +57,7 @@
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.Win32.D3D12\DirectX\um\d3dcommon\D3D_TESSELLATOR_DOMAIN.cs" Link="DirectX\um\d3dcommon\D3D_TESSELLATOR_DOMAIN.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3d11shader\ID3D11ShaderReflection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\dcommon\D2D_VECTOR_2F.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.Win32.D3D12\Windows\other\helper-types\IComObject.cs" Link="Windows\other\helper-types\IComObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\HSTRING.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.Win32.D3D12\Windows\shared\uuids\Windows.Manual.cs" Link="Windows\shared\uuids\Windows.Manual.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.Win32.D3D12\ComPtr`1.cs" Link="ComPtr`1.cs" />
+3 -3
ファイルの表示
@@ -87,7 +87,7 @@ internal unsafe struct ComPtr<T> : IDisposable
/// <param name="ptr">The target raw pointer to copy the address of the current COM object to.</param>
/// <returns>The result of <see cref="IUnknown.QueryInterface"/> for the target type <typeparamref name="U"/>.</returns>
public readonly HRESULT CopyTo<U>(U** ptr)
where U : unmanaged
where U : unmanaged, IComObject
{
return ((IUnknown*)ptr_)->QueryInterface(__uuidof<U>(), (void**)ptr);
}
@@ -96,7 +96,7 @@ internal unsafe struct ComPtr<T> : IDisposable
/// <param name="p">The target raw pointer to copy the address of the current COM object to.</param>
/// <returns>The result of <see cref="IUnknown.QueryInterface"/> for the target type <typeparamref name="U"/>.</returns>
public readonly HRESULT CopyTo<U>(ComPtr<U>* p)
where U : unmanaged
where U : unmanaged, IComObject
{
return ((IUnknown*)ptr_)->QueryInterface(__uuidof<U>(), (void**)p->ReleaseAndGetAddressOf());
}
@@ -105,7 +105,7 @@ internal unsafe struct ComPtr<T> : IDisposable
/// <param name="other">The target reference to copy the address of the current COM object to.</param>
/// <returns>The result of <see cref="IUnknown.QueryInterface"/> for the target type <typeparamref name="U"/>.</returns>
public readonly HRESULT CopyTo<U>(ref ComPtr<U> other)
where U : unmanaged
where U : unmanaged, IComObject
{
U* ptr;
HRESULT result = ((IUnknown*)ptr_)->QueryInterface(__uuidof<U>(), (void**)&ptr);
+1
ファイルの表示
@@ -215,6 +215,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\BOOL.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\HANDLE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\HRESULT.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\IComObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\NativeInheritanceAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\NativeTypeNameAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\VtblIndexAttribute.cs" />
+14
ファイルの表示
@@ -0,0 +1,14 @@
using System;
namespace ComputeSharp.Win32;
/// <summary>
/// An interface for a COM object, with a specific IID.
/// </summary>
internal unsafe interface IComObject
{
/// <summary>
/// Gets a pointer to the IID for the object type.
/// </summary>
static abstract Guid* IID { get; }
}
+6 -45
ファイルの表示
@@ -7,50 +7,28 @@ using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
#pragma warning disable IDE1006
namespace ComputeSharp.Win32;
internal static partial class Windows
{
/// <summary>Retrieves the GUID of of a specified type.</summary>
/// <param name="value">A value of type <typeparamref name="T"/>.</param>
/// <typeparam name="T">The type to retrieve the GUID for.</typeparam>
/// <returns>A <see cref="UuidOfType"/> value wrapping a pointer to the GUID data for the input type. This value can be either converted to a <see cref="Guid"/> pointer, or implicitly assigned to a <see cref="Guid"/> value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe UuidOfType __uuidof<T>(T value) // for type inference similar to C++'s __uuidof
where T : unmanaged
{
return new UuidOfType(UUID<T>.RIID);
}
/// <summary>Retrieves the GUID of of a specified type.</summary>
/// <param name="value">A pointer to a value of type <typeparamref name="T"/>.</param>
/// <typeparam name="T">The type to retrieve the GUID for.</typeparam>
/// <returns>A <see cref="UuidOfType"/> value wrapping a pointer to the GUID data for the input type. This value can be either converted to a <see cref="Guid"/> pointer, or implicitly assigned to a <see cref="Guid"/> value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe UuidOfType __uuidof<T>(T* value) // for type inference similar to C++'s __uuidof
where T : unmanaged
{
return new UuidOfType(UUID<T>.RIID);
}
/// <summary>Retrieves the GUID of of a specified type.</summary>
/// <typeparam name="T">The type to retrieve the GUID for.</typeparam>
/// <returns>A <see cref="UuidOfType"/> value wrapping a pointer to the GUID data for the input type. This value can be either converted to a <see cref="Guid"/> pointer, or implicitly assigned to a <see cref="Guid"/> value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe UuidOfType __uuidof<T>()
where T : unmanaged
where T : unmanaged, IComObject
{
return new UuidOfType(UUID<T>.RIID);
return new UuidOfType(T.IID);
}
/// <summary>A proxy type that wraps a pointer to GUID data. Values of this type can be implicitly converted to and assigned to <see cref="Guid"/>* or <see cref="Guid"/> parameters.</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly unsafe ref struct UuidOfType
{
private readonly Guid* riid;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal UuidOfType(Guid* riid)
{
this.riid = riid;
@@ -58,29 +36,12 @@ internal static partial class Windows
/// <summary>Reads a <see cref="Guid"/> value from the GUID buffer for a given <see cref="UuidOfType"/> instance.</summary>
/// <param name="guid">The input <see cref="UuidOfType"/> instance to read data for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Guid(UuidOfType guid) => *guid.riid;
/// <summary>Returns the <see cref="Guid"/>* pointer to the GUID buffer for a given <see cref="UuidOfType"/> instance.</summary>
/// <param name="guid">The input <see cref="UuidOfType"/> instance to read data for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Guid*(UuidOfType guid) => guid.riid;
}
/// <summary>A helper type to provide static GUID buffers for specific types.</summary>
/// <typeparam name="T">The type to allocate a GUID buffer for.</typeparam>
private static unsafe class UUID<T>
where T : unmanaged
{
/// <summary>The pointer to the <see cref="Guid"/> value for the current type.</summary>
/// <remarks>The target memory area should never be written to.</remarks>
public static readonly Guid* RIID = CreateRIID();
/// <summary>Allocates memory for a <see cref="Guid"/> value and initializes it.</summary>
/// <returns>A pointer to memory holding the <see cref="Guid"/> value for the current type.</returns>
private static Guid* CreateRIID()
{
var p = (Guid*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(T), sizeof(Guid));
*p = typeof(T).GUID;
return p;
}
}
}