// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from winrt/wrl/client.h in the Windows SDK for Windows 10.0.20348.0
// Original source is Copyright © Microsoft. All rights reserved.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using static ComputeSharp.Win32.Windows;
using static ComputeSharp.Win32.S;
namespace ComputeSharp.Win32;
/// A type that allows working with pointers to COM objects more securely.
/// The type to wrap in the current instance.
/// While this type is not marked as so that it can also be used in fields, make sure to keep the reference counts properly tracked if you do store instances on the heap.
internal unsafe struct ComPtr : IDisposable
where T : unmanaged
{
/// The raw pointer to a COM object, if existing.
private T* ptr_;
/// Creates a new instance from a raw pointer and increments the ref count.
/// The raw pointer to wrap.
public ComPtr(T* other)
{
ptr_ = other;
InternalAddRef();
}
/// Releases the current COM object, if any, and replaces the internal pointer with an input raw pointer.
/// The input raw pointer to wrap.
/// This method will release the current raw pointer, if any, but it will not increment the references for .
public void Attach(T* other)
{
if (ptr_ != null)
{
var @ref = ((IUnknown*)ptr_)->Release();
Debug.Assert((@ref != 0) || (ptr_ != other));
}
ptr_ = other;
}
/// Returns the raw pointer wrapped by the current instance, and resets the current value.
/// The raw pointer wrapped by the current value.
/// This method will not change the reference count for the COM object in use.
public T* Detach()
{
T* ptr = ptr_;
ptr_ = null;
return ptr;
}
/// Increments the reference count for the current COM object, if any, and copies its address to a target raw pointer.
/// The target raw pointer to copy the address of the current COM object to.
/// This method always returns .
public readonly HRESULT CopyTo(T** ptr)
{
InternalAddRef();
*ptr = ptr_;
return S_OK;
}
/// Increments the reference count for the current COM object, if any, and copies its address to a target .
/// The target raw pointer to copy the address of the current COM object to.
/// This method always returns .
public readonly HRESULT CopyTo(ComPtr* p)
{
InternalAddRef();
*p->ReleaseAndGetAddressOf() = ptr_;
return S_OK;
}
/// Increments the reference count for the current COM object, if any, and copies its address to a target .
/// The target reference to copy the address of the current COM object to.
/// This method always returns .
public readonly HRESULT CopyTo(ref ComPtr other)
{
InternalAddRef();
other.Attach(ptr_);
return S_OK;
}
/// Converts the current COM object reference to a given interface type and assigns that to a target raw pointer.
/// The target raw pointer to copy the address of the current COM object to.
/// The result of for the target type .
public readonly HRESULT CopyTo(U** ptr)
where U : unmanaged, IComObject
{
return ((IUnknown*)ptr_)->QueryInterface(__uuidof(), (void**)ptr);
}
/// Converts the current COM object reference to a given interface type and assigns that to a target .
/// The target raw pointer to copy the address of the current COM object to.
/// The result of for the target type .
public readonly HRESULT CopyTo(ComPtr* p)
where U : unmanaged, IComObject
{
return ((IUnknown*)ptr_)->QueryInterface(__uuidof(), (void**)p->ReleaseAndGetAddressOf());
}
/// Converts the current COM object reference to a given interface type and assigns that to a target .
/// The target reference to copy the address of the current COM object to.
/// The result of for the target type .
public readonly HRESULT CopyTo(ref ComPtr other)
where U : unmanaged, IComObject
{
U* ptr;
HRESULT result = ((IUnknown*)ptr_)->QueryInterface(__uuidof(), (void**)&ptr);
other.Attach(ptr);
return result;
}
/// Converts the current object reference to a type indicated by the given IID and assigns that to a target address.
/// The IID indicating the interface type to convert the COM object reference to.
/// The target raw pointer to copy the address of the current COM object to.
/// The result of for the target IID.
public readonly HRESULT CopyTo(Guid* riid, void** ptr)
{
return ((IUnknown*)ptr_)->QueryInterface(riid, ptr);
}
/// Converts the current object reference to a type indicated by the given IID and assigns that to a target value.
/// The IID indicating the interface type to convert the COM object reference to.
/// The target raw pointer to copy the address of the current COM object to.
/// The result of for the target IID.
public readonly HRESULT CopyTo(Guid* riid, ComPtr* p)
{
return ((IUnknown*)ptr_)->QueryInterface(riid, (void**)p->ReleaseAndGetAddressOf());
}
/// Converts the current object reference to a type indicated by the given IID and assigns that to a target value.
/// The IID indicating the interface type to convert the COM object reference to.
/// The target reference to copy the address of the current COM object to.
/// The result of for the target IID.
public readonly HRESULT CopyTo(Guid* riid, ref ComPtr other)
{
IUnknown* ptr;
HRESULT result = ((IUnknown*)ptr_)->QueryInterface(riid, (void**)&ptr);
other.Attach(ptr);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
T* pointer = ptr_;
if (pointer != null)
{
ptr_ = null;
_ = ((IUnknown*)pointer)->Release();
}
}
/// Gets the currently wrapped raw pointer to a COM object.
/// The raw pointer wrapped by the current instance.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly T* Get()
{
return ptr_;
}
/// Gets the address of the current instance as a raw double pointer. This method is only valid when the current instance is on the stack or pinned.
///
/// The raw pointer to the current instance.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly T** GetAddressOf()
{
return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this));
}
/// Gets the address of the current instance as a raw double pointer.
/// The raw pointer to the current instance.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[EditorBrowsable(EditorBrowsableState.Never)]
[UnscopedRef]
public readonly ref T* GetPinnableReference()
{
return ref Unsafe.AsRef(in this).ptr_;
}
/// Releases the current COM object in use and gets the address of the instance as a raw double pointer. This method is only valid when the current instance is on the stack or pinned.
/// The raw pointer to the current instance.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T** ReleaseAndGetAddressOf()
{
Dispose();
return GetAddressOf();
}
/// Swaps the current COM object reference with that of a given instance.
/// The target instance to swap with the current one.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Swap(ComPtr* r)
{
T* tmp = ptr_;
ptr_ = r->ptr_;
r->ptr_ = tmp;
}
/// Swaps the current COM object reference with that of a given instance.
/// The target instance to swap with the current one.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Swap(ref ComPtr other)
{
T* tmp = ptr_;
ptr_ = other.ptr_;
other.ptr_ = tmp;
}
// Increments the reference count for the current COM object, if any
private readonly void InternalAddRef()
{
T* temp = ptr_;
if (temp != null)
{
_ = ((IUnknown*)temp)->AddRef();
}
}
}