using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using TerraFX.Interop.Windows; namespace ComputeSharp.Tests.Extensions; /// /// Helper methods for working with the type. /// internal static class ComPtrExtensions { /// /// Gets the reference count of a given value. /// /// The type to wrap in the current instance. /// The input instance to check. /// The reference count value for . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe uint GetReferenceCount(this in ComPtr ptr) where T : unmanaged, IUnknown.Interface { if (ptr.Get() is null) { return 0; } _ = Marshal.AddRef((nint)ptr.Get()); // Here we use Marshal APIs as T isn't constrained to IUnknown. // It doesn't matter anyway since this is just a test helper. return (uint)Marshal.Release((nint)ptr.Get()); } }