using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; namespace ComputeSharp.Core.Extensions { /// /// Helper methods for working with the type. /// internal static class ComPtrExtensions { /// /// Reinterprets the current instance as one of type . /// /// The type of the current instance. /// The input instance to upcast. /// A instance of type . /// /// 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. /// [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ref readonly ComPtr AsIUnknown(this in ComPtr ptr) where T : unmanaged { return ref Unsafe.As, ComPtr>(ref Unsafe.AsRef(in ptr)); } /// /// Gets the address of the current instance as a raw double pointer. /// /// The type to wrap in the current instance. /// The input instance to get the address for. /// The raw pointer to the input instance. /// This method is only valid when the current instance is on the stack or pinned. [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void** GetVoidAddressOf(this in ComPtr ptr) where T : unmanaged { return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in ptr)); } /// /// Gets the address of the current instance as a raw double pointer. /// /// The type to wrap in the current instance. /// The input instance to get the address for. /// The raw pointer to the input instance. /// This method is only valid when the current instance is on the stack or pinned. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ComPtr Move(this in ComPtr ptr) where T : unmanaged { ComPtr copy = default; Unsafe.AsRef(in ptr).Swap(ref copy); return copy; } } }