Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ComPtr<T> improvements, new __uuidof<T> macro #151

Merged
merged 22 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d3573d0
Added comments to ComPtr<T> type
Sergio0694 Nov 15, 2020
fe450d3
Added ComPtr<T>.RIID field, minor perf tweaks
Sergio0694 Nov 15, 2020
e9aea0c
Performance improvements in ComPtr<T>.Address
Sergio0694 Nov 15, 2020
85f4dde
Minor XML docs fix
Sergio0694 Nov 15, 2020
d31ef1f
Added ComPtr<T>.RIID unit test
Sergio0694 Nov 16, 2020
681a840
Added new ComPtr<T>.As and CopyTo overloads
Sergio0694 Nov 16, 2020
7b1dea4
Minor code tweaks
Sergio0694 Nov 16, 2020
bcebd35
Added missing ComPtr<T>.Swap overload
Sergio0694 Nov 16, 2020
2aef133
Fixed pinning for ComPtr<T>.GetPinnableReference
Sergio0694 Nov 16, 2020
b0c9282
Improved visibility for ComPtr<T> pinning remarks
Sergio0694 Nov 16, 2020
1f66ef1
Code refactoring, implemented __uuidof<T>() API
Sergio0694 Nov 17, 2020
294d735
Removed [Pure] attribute
Sergio0694 Nov 21, 2020
650cef5
Restored original field naming in ComPtr<T>
Sergio0694 Nov 21, 2020
b90b27e
Removed target-typed new() operator
Sergio0694 Nov 21, 2020
19c79e3
Removed line wrapping in XML comments
Sergio0694 Nov 21, 2020
8702e3d
Renamed GUID helper type to UuidOfType
Sergio0694 Nov 21, 2020
c0bd665
FIxed typo in ComPtr<T>.ptr_ field name
Sergio0694 Nov 22, 2020
b358ae9
Switched XML docs style to single-line
Sergio0694 Nov 22, 2020
b2a4be6
Removed extra blank lines
Sergio0694 Nov 22, 2020
3f93d6e
Restored original variable names
Sergio0694 Nov 22, 2020
e10f572
Removed unnecessary fixed blocks
Sergio0694 Nov 22, 2020
65b7f00
Removed unnecessary AggressiveInlining to UuidOfType
Sergio0694 Nov 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions sources/Interop/Windows/shared/uuids/Windows.Manual.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

// Ported from shared/uuids.h in the Windows SDK for Windows 10.0.19041.0
// Original source is Copyright © Microsoft. All rights reserved.

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace TerraFX.Interop
{
public static partial class Windows
{
/// <summary>Retrieves the GUID of of a specified type.</summary>
/// <typeparam name="T">The type to retrieve the GUID for.</typeparam>
/// <returns>A <see cref="UuidOfType"/> value wrapping a pointer to the GUID data for the input type. This value can be either converted to a <see cref="Guid"/> pointer, or implicitly assigned to a <see cref="Guid"/> value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe UuidOfType __uuidof<T>()
where T : unmanaged
{
return new UuidOfType(UUID<T>.RIID);
}

/// <summary>
/// A proxy type that wraps a pointer to GUID data. Values of this type can be implicitly
/// converted to and assigned to <see cref="Guid"/>* or <see cref="Guid"/> parameters.
/// </summary>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had only called out the doc comment syntax mismatch on the type, but it applies to all added doc comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Fixed in b358ae9 🙂

[EditorBrowsable(EditorBrowsableState.Never)]
public readonly unsafe ref struct UuidOfType
{
private readonly Guid* riid;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need AggressiveInlining on these. They are extremely small and the JIT should handle it already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 65b7f00.

internal UuidOfType(Guid* riid)
{
this.riid = riid;
}

/// <summary>
/// Reads a <see cref="Guid"/> value from the GUID buffer for a given <see cref="UuidOfType"/> instance.
/// </summary>
/// <param name="guid">The input <see cref="UuidOfType"/> instance to read data for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Guid(UuidOfType guid) => *guid.riid;

/// <summary>
/// Returns the <see cref="Guid"/>* pointer to the GUID buffer for a given <see cref="UuidOfType"/> instance.
/// </summary>
/// <param name="guid">The input <see cref="UuidOfType"/> instance to read data for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Guid*(UuidOfType guid) => guid.riid;
}

/// <summary>
/// A helper type to provide static GUID buffers for specific types.
/// </summary>
/// <typeparam name="T">The type to allocate a GUID buffer for.</typeparam>
private static unsafe class UUID<T>
where T : unmanaged
{
/// <summary>
/// The pointer to the <see cref="Guid"/> value for the current type.
/// </summary>
/// <remarks>The target memory area should never be written to.</remarks>
public static readonly Guid* RIID = CreateRIID();

/// <summary>
/// Allocates memory for a <see cref="Guid"/> value and initializes it.
/// </summary>
/// <returns>A pointer to memory holding the <see cref="Guid"/> value for the current type.</returns>
private static Guid* CreateRIID()
{
Guid* p = (Guid*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(T), sizeof(Guid));

*p = typeof(T).GUID;

return p;
}
}
}
}
Loading