diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ComPtrExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ComPtrExtensions.cs new file mode 100644 index 00000000..8e51d066 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ComPtrExtensions.cs @@ -0,0 +1,41 @@ +using ComputeSharp.D2D1.Extensions; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1.Uwp.Extensions; + +/// +/// Helper methods for working with the type. +/// +internal static class ComPtrExtensions +{ + /// + /// 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 in ComPtr left, in ComPtr right) + where T : unmanaged + where U : unmanaged + { + if (left.Get() == right.Get()) + { + return true; + } + + if (left.Get() is null || right.Get() is null) + { + return false; + } + + using ComPtr leftUnknown = default; + using ComPtr rightUnknown = default; + + left.CopyTo(leftUnknown.GetAddressOf()).Assert(); + right.CopyTo(rightUnknown.GetAddressOf()).Assert(); + + return leftUnknown.Get() == rightUnknown.Get(); + } +} \ No newline at end of file