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

wil should add GUID-related APIs #362

Open
JeffMill opened this issue Sep 19, 2023 · 4 comments
Open

wil should add GUID-related APIs #362

JeffMill opened this issue Sep 19, 2023 · 4 comments
Labels
feature-request New feature or request

Comments

@JeffMill
Copy link

JeffMill commented Sep 19, 2023

WIL has these constants:

    //! For {guid} string form. Includes space for the null terminator.
    constexpr size_t guid_string_buffer_length = 39;
 
    //! For {guid} string form. Not including the null terminator.
    constexpr size_t guid_string_length = 38;

but no complementary APIs to do conversion to/from std::string. Plus things for CoCreateGuid, format to/from strings, etc. I know many teams end up using cppwinrt's helpers to do this instead.

@dunhor dunhor added the feature-request New feature or request label Sep 19, 2023
@dunhor
Copy link
Member

dunhor commented Sep 19, 2023

IMO - if you're trying to heap allocate a GUID string, 9 times out of 10 you're doing it wrong. The length is statically known, so it's almost always better to just have it allocated where you need it as an array - either on the stack or as a member of a struct. Some possible exceptions:

  • The string is a member of a struct, but is conditionally set and the extra memory being allocated might be excessive
  • You are calling an API that takes ownership of the string (rare) or are implementing an API that gives ownership of a string (less rare). In this case, you'd probably want a LocalAlloc-ed, etc. type, though
  • You are, for some reason, storing a dynamically sized list of these strings

I could see something like:

template <typename StringT>
HRESULT to_string_nothrow(const GUID&, StringT&);

template <typename StringT>
StringT to_string(const GUID&);

for these scenarios as quick helpers for the rare times where this is needed.

@jonwis
Copy link
Member

jonwis commented Oct 10, 2023

And also a create_guid that emits your favorite GUID type, defaulting to GUID, like:

template<typename TGuid = GUID> create_guid()
{
    GUID g;
    THROW_IF_FAILED(::CoCreateGuid(&g));
    return { g };
}

(No nonthrowing variant required since CoCreateGuid already exists.)

@JeffMill
Copy link
Author

JeffMill commented Nov 10, 2023

to implementer: consider that GUIDs have two forms -- with and without surrounding braces, so it would be nice if this implementation also supported this. PowerShell does a good job IMO:

> [Guid]'23f1dd41-a85f-4b5c-8ce0-010bd2f886f7'

Guid
----
23f1dd41-a85f-4b5c-8ce0-010bd2f886f7

> [Guid]'{23f1dd41-a85f-4b5c-8ce0-010bd2f886f7}'

Guid
----
23f1dd41-a85f-4b5c-8ce0-010bd2f886f7

> ([Guid]'{23f1dd41-a85f-4b5c-8ce0-010bd2f886f7}').ToString()
23f1dd41-a85f-4b5c-8ce0-010bd2f886f7

And would be nice to get to underlying struct as well, so one could access the underlying fields of the GUID struct if needed.

@jonwis
Copy link
Member

jonwis commented Nov 16, 2023

See also the work I did on the static_sid type, which returns a custom type of "the right size." So you could define a new struct guid_string that contains a 39-charcter wchar_t inside, and has an "operator std::wstring_view" (or the new zwstring_view) and has .c_str so you can just get a stack thing that can be used like a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants