// 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.Runtime.CompilerServices;
using static TerraFX.Interop.Windows.S;
using static TerraFX.Interop.Windows.Windows;
namespace TerraFX.Interop.Windows
{
/// 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, IUnknown.Interface
{
/// 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();
}
/// Creates a new instance from a second one and increments the ref count.
/// The other instance to copy.
public ComPtr(ComPtr other)
{
ptr_ = other.ptr_;
InternalAddRef();
}
/// Converts a raw pointer to a new instance and increments the ref count.
/// The raw pointer to wrap.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ComPtr(T* other)
=> new ComPtr(other);
/// Unwraps a instance and returns the internal raw pointer.
/// The instance to unwrap.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator T*(ComPtr other)
=> other.Get();
/// Converts the current object reference to type and assigns that to a target value.
/// The interface type to use to try casting the current COM object.
/// A raw pointer to the target value to write to.
/// The result of for the target type .
/// This method will automatically release the target COM object pointed to by , if any.
public readonly HRESULT As(ComPtr* p)
where U : unmanaged, IUnknown.Interface
{
return ptr_->QueryInterface(__uuidof(), (void**)p->ReleaseAndGetAddressOf());
}
/// Converts the current object reference to type and assigns that to a target value.
/// The interface type to use to try casting the current COM object.
/// A reference to the target value to write to.
/// The result of for the target type .
/// This method will automatically release the target COM object pointed to by , if any.
public readonly HRESULT As(ref ComPtr other)
where U : unmanaged, IUnknown.Interface
{
U* ptr;
HRESULT result = 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 value.
/// The IID indicating the interface type to convert the COM object reference to.
/// A raw pointer to the target value to write to.
/// The result of for the target IID.
/// This method will automatically release the target COM object pointed to by , if any.
public readonly HRESULT AsIID(Guid* riid, ComPtr* other)
{
return ptr_->QueryInterface(riid, (void**)other->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.
/// A reference to the target value to write to.
/// The result of for the target IID.
/// This method will automatically release the target COM object pointed to by , if any.
public readonly HRESULT AsIID(Guid* riid, ref ComPtr other)
{
IUnknown* ptr;
HRESULT result = ptr_->QueryInterface(riid, (void**)&ptr);
other.Attach(ptr);
return result;
}
/// 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 = 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, IUnknown.Interface
{
return 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, IUnknown.Interface
{
return 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, IUnknown.Interface
{
U* ptr;
HRESULT result = 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 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 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 = ptr_->QueryInterface(riid, (void**)&ptr);
other.Attach(ptr);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
T* pointer = ptr_;
if (pointer != null)
{
ptr_ = null;
_ = 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)]
public readonly ref T* GetPinnableReference()
{
fixed (T** ptr = &ptr_)
{
return ref *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()
{
_ = InternalRelease();
return GetAddressOf();
}
/// Resets the current instance by decrementing the reference count for the target COM object and setting the internal raw pointer to .
/// The updated reference count for the COM object that was in use, if any.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Reset()
{
return InternalRelease();
}
/// 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)
{
_ = temp->AddRef();
}
}
// Decrements the reference count for the current COM object, if any
private uint InternalRelease()
{
uint @ref = 0;
T* temp = ptr_;
if (temp != null)
{
ptr_ = null;
@ref = temp->Release();
}
return @ref;
}
}
}