-
Notifications
You must be signed in to change notification settings - Fork 274
feat: add make_array_of_(uuid|guid) #539
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
base: master
Are you sure you want to change the base?
Conversation
include/wil/cppwinrt_authoring.h
Outdated
| template <typename... Ts> | ||
| WI_NODISCARD_REASON("The classes are unregistered when the returned value is destructed") | ||
| std::vector<unique_com_class_object_cookie> register_com_server( | ||
| wil::clsid_array<Ts...> const& clsids, DWORD context = CLSCTX_LOCAL_SERVER, DWORD flags = REGCLS_MULTIPLEUSE) |
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 would recommend keeping the std::array parameter and making clsid_array a new overload.
The overload can simply pass it on to the std::array version
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 don't mind that, but let's see what @dunhor vote for.
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'd tend to agree with @sylveon here, but I'll take it one step further - I don't really see a value for the clsid_array type at all. In fact, I'd say that it harms the API overall. Consider the way that most people will use this funciton:
std::array<GUID> clsids = { CLSID_Foo, CLSID_Bar };
auto unregister = wil::register_com_server<Foo, Bar>(clsids);Tying the type names of the GUIDs to the array parameter is actively harmful and violates your "don't repeat twice" principle (I'd need to provide the type names in the type of clsid_array in this case).
I'd say remove this type altogether. A type alias might be fine, but still probably unnecessary.
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.
The benefit is when you are using __uuidof or winrt::guid_of, tying the types to the array allows template type deduction to work, preventing you from typing the name twice.
Like this:
wil::register_com_server(wil::make_array_of_uuid<Foo, Bar>());But we definitely need to keep the untyped array overloads for examples like the one you have.
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.
Gotcha, I see.
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.
Additionally, you state that you are getting ambiguous overload errors. I'm wondering if that's because the tests are calling using double braces ({{ CLSID_A, CLSID_B, ... }}). If you switch the tests over to single braces, I wonder if that resolves the errors.
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.
The double braces are required for std::array. We would've needed std::initializer_list for single braces.
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.
std::array supports aggregate initialization, so single braces should be fine https://godbolt.org/z/o5cPPjYKx
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.
Uh, I could swear that wasn't the case in specific instances. Guess I was wrong.
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.
See: https://en.cppreference.com/w/cpp/language/aggregate_initialization.html#Appertainment
Specifically:
Otherwise,
elemis an aggregate and that subaggregate is replaced in the list of aggregate elements by the sequence of its own aggregate elements, and the appertainment analysis resumes with the first such element and the same initializer clause. In other words, these rules apply recursively to the aggregate’s subaggregates.
Which probably makes my previous statement about ambiguous overloads incorrect, at least as long as clsid_array is an aggregate. I.e. it would need a constructor, however that constructor would not need to be explicit (though, frankly, it's probably good to be explicit anyway)
|
I added wiki for |
| } | ||
|
|
||
| template <typename... Types> | ||
| constexpr auto make_array_of_uuid() |
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 think I like the names make_clsid_array_uuid and make_clsid_array_guid (or perhaps make_clsid_array_uuidof and make_clsid_array_guid_of to make it more explicit which operation is being used to form the CLSIDs). This makes it clearer what the function's purpose is. What do others think here?
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 suppose that the __uuidof version could be useful outside of the scope of CLSIDs, e.g. if you were to implement something like RuntimeClass and wanted an array of IIDs when implementing QueryInterface etc.
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.
What about make_clsid_array and make_clsid_array_winrt? I don't particularly like uuid vs guid because it is not clear what are the differences.
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.
What about make_clsid_array and make_clsid_array_winrt? I don't particularly like uuid vs guid because it is not clear what are the differences.
Seems dunhor's suggestion can answer the question? I'd expect user typing make_clsid_array_ and then seeing uuidof vs guid_of would be able to tell which is which
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.
guid/uuid are the most generic. clsid and iid are specializations.
it is odd that clsid_array is the building block. I'd expect guid/uuid to be at the bottom.
these are all aliases for the same thing, so using different names can be dangerous due to the false sense of type safety.
| struct clsid_array | ||
| { | ||
| std::array<GUID, sizeof...(Types)> value; | ||
| explicit clsid_array(std::array<GUID, sizeof...(Types)> value) : value(std::move(value)) |
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.
Note that the pattern here is "copy then move". In practice this value is always move-constructed from temporary, or in more recent standard just materialized without any temporary.
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.
std::move is pointless here. It's a std::array and GUIDs. Accept by const reference and just assign and make it constexpr while you are at it.
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This is a follow up PR of #537. Requires #537 before merging.
I'm mostly fine with sylveon's improvement in #537 given the current unfortunate situation, but every time I see that we have to write class name twice during registration a little bit of me die inside:
If you register N class you have to spell out the type 2N times. Not good. I consider please repeat yourself twice a sign of bad API design.
So this PR adds
make_array_of_uuidandmake_array_of_guidto mitigate the problem:It works by wrapping the array in a template and let template parameter deduction works out the rest.
Also disallow using
make_array_of_guidif the type is a COM class, i.e.__uuof(T)is defined. Shamelessly borrowed from @dunhor'shas_iid_vin previous PR.This PR does not attempt to address the issue that the implementation type might not have a default interface that is exclusive to it.