using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Extensions;
using TerraFX.Interop.Windows;
using Win32 = TerraFX.Interop.Windows.Windows;
namespace ComputeSharp.D2D1.Uwp.Extensions;
///
/// Helper methods for working with the type.
///
internal static class IUnknownExtensions
{
///
/// Checks whether two values have the same underlying COM object.
///
/// The type of the first value to check.
/// The type of the second value to check.
/// The first object to compare.
/// The second object to compare.
/// Whether and point to the same COM object.
public static unsafe bool IsSameInstance(this ref T left, U* right)
where T : unmanaged // IUnknown
where U : unmanaged // IUnknown
{
if (Unsafe.AreSame(ref left, ref *(T*)right))
{
return true;
}
if (Unsafe.IsNullRef(ref left) || right is null)
{
return false;
}
using ComPtr leftUnknown = default;
using ComPtr rightUnknown = default;
((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof(), (void**)leftUnknown.GetAddressOf()).Assert();
((IUnknown*)right)->QueryInterface(Win32.__uuidof(), (void**)rightUnknown.GetAddressOf()).Assert();
return leftUnknown.Get() == rightUnknown.Get();
}
}