-
Notifications
You must be signed in to change notification settings - Fork 31
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
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 fe450d3
Added ComPtr<T>.RIID field, minor perf tweaks
Sergio0694 e9aea0c
Performance improvements in ComPtr<T>.Address
Sergio0694 85f4dde
Minor XML docs fix
Sergio0694 d31ef1f
Added ComPtr<T>.RIID unit test
Sergio0694 681a840
Added new ComPtr<T>.As and CopyTo overloads
Sergio0694 7b1dea4
Minor code tweaks
Sergio0694 bcebd35
Added missing ComPtr<T>.Swap overload
Sergio0694 2aef133
Fixed pinning for ComPtr<T>.GetPinnableReference
Sergio0694 b0c9282
Improved visibility for ComPtr<T> pinning remarks
Sergio0694 1f66ef1
Code refactoring, implemented __uuidof<T>() API
Sergio0694 294d735
Removed [Pure] attribute
Sergio0694 650cef5
Restored original field naming in ComPtr<T>
Sergio0694 b90b27e
Removed target-typed new() operator
Sergio0694 19c79e3
Removed line wrapping in XML comments
Sergio0694 8702e3d
Renamed GUID helper type to UuidOfType
Sergio0694 c0bd665
FIxed typo in ComPtr<T>.ptr_ field name
Sergio0694 b358ae9
Switched XML docs style to single-line
Sergio0694 b2a4be6
Removed extra blank lines
Sergio0694 3f93d6e
Restored original variable names
Sergio0694 e10f572
Removed unnecessary fixed blocks
Sergio0694 65b7f00
Removed unnecessary AggressiveInlining to UuidOfType
Sergio0694 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public readonly unsafe ref struct UuidOfType | ||
{ | ||
private readonly Guid* riid; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂