Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 50 additions & 2 deletions include/wil/cppwinrt_authoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ unique_com_class_object_cookie
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(
std::array<GUID, sizeof...(Ts)> const& guids, DWORD context = CLSCTX_LOCAL_SERVER, DWORD flags = REGCLS_MULTIPLEUSE)
std::array<GUID, sizeof...(Ts)> const& clsids, DWORD context = CLSCTX_LOCAL_SERVER, DWORD flags = REGCLS_MULTIPLEUSE)
{
std::vector<wil::unique_com_class_object_cookie> registrations;
registrations.reserve(sizeof...(Ts));

std::size_t i = 0;
(registrations.push_back(wil::register_com_server<Ts>(guids[i++], context, flags | REGCLS_SUSPENDED)), ...);
(registrations.push_back(wil::register_com_server<Ts>(clsids[i++], context, flags | REGCLS_SUSPENDED)), ...);

// allow the user to keep class objects suspended if they've explicitly passed REGCLS_SUSPENDED.
if (!WI_IsFlagSet(flags, REGCLS_SUSPENDED))
Expand All @@ -192,6 +192,54 @@ std::vector<unique_com_class_object_cookie> register_com_server(
return registrations;
}

template <typename... Types>
struct clsid_array
{
std::array<GUID, sizeof...(Types)> value;
explicit clsid_array(std::array<GUID, sizeof...(Types)> value) : value(std::move(value))
Copy link
Contributor Author

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.

Copy link
Member

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.

{
}
};

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)
{
return register_com_server<Ts...>(clsids.value, context, flags);
}

namespace details
{
template <typename T>
struct has_iid
{
template <typename U = T, std::enable_if_t<std::is_same_v<GUID, std::decay_t<decltype(__uuidof(U))>>, int> = 0>
static std::true_type invoke(int);

template <typename U = T>
static std::false_type invoke(float);

static constexpr bool value = decltype(invoke(0))::value;
};

template <typename T>
constexpr bool has_iid_v = has_iid<T>::value;
}

template <typename... Types>
constexpr auto make_array_of_uuid()
Copy link
Member

@dunhor dunhor Aug 6, 2025

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?

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Member

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.

{
return clsid_array<Types...>{std::array<GUID, sizeof...(Types)>{__uuidof(Types)...}};
}

template <typename... Types>
constexpr auto make_array_of_guid()
{
static_assert(!(... && details::has_iid_v<Types>), "Use make_array_of_uuid for COM class instead");
return clsid_array<Types...>{std::array<GUID, sizeof...(Types)>{winrt::guid_of<Types>()...}};
}

#endif // (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_COM_SERVER) && defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_ICLASSFACTORY) && defined(_COMBASEAPI_H_))

#if (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_FOUNDATION) && defined(WINRT_Windows_Foundation_H)) || \
Expand Down
88 changes: 44 additions & 44 deletions tests/ComApartmentVariableTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,49 +363,49 @@ TEST_CASE("ComApartmentVariable::ShutdownRegistration", "[LocalOnly][com][unique
}
}

TEST_CASE("ComApartmentVariable::CallAllMethods", "[com][apartment_variable]")
{
RunApartmentVariableTest(TestApartmentVariableAllMethods<mock_platform>);
}

TEST_CASE("ComApartmentVariable::GetOrCreateForms", "[com][apartment_variable]")
{
RunApartmentVariableTest(TestApartmentVariableGetOrCreateForms<mock_platform>);
}

TEST_CASE("ComApartmentVariable::VariableLifetimes", "[com][apartment_variable]")
{
RunApartmentVariableTest(TestApartmentVariableLifetimes<mock_platform>);
}

TEST_CASE("ComApartmentVariable::WinningApartmentAlreadyRundownRace", "[com][apartment_variable]")
{
RunApartmentVariableTest(TestWinningApartmentAlreadyRundownRace<mock_platform>);
}

TEST_CASE("ComApartmentVariable::LosingApartmentAlreadyRundownRace", "[com][apartment_variable]")
{
RunApartmentVariableTest(TestLosingApartmentAlreadyRundownRace<mock_platform>);
}

TEST_CASE("ComApartmentVariable::MultipleApartments", "[com][apartment_variable]")
{
RunApartmentVariableTest(TestMultipleApartments<mock_platform>);
}

TEST_CASE("ComApartmentVariable::UseRealPlatformRunAllTests", "[com][apartment_variable]")
{
if (!wil::are_apartment_variables_supported())
{
return;
}

RunApartmentVariableTest(TestApartmentVariableAllMethods);
RunApartmentVariableTest(TestApartmentVariableGetOrCreateForms);
RunApartmentVariableTest(TestApartmentVariableLifetimes);
RunApartmentVariableTest(TestWinningApartmentAlreadyRundownRace);
RunApartmentVariableTest(TestLosingApartmentAlreadyRundownRace);
RunApartmentVariableTest(TestMultipleApartments);
}
//TEST_CASE("ComApartmentVariable::CallAllMethods", "[com][apartment_variable]")
//{
// RunApartmentVariableTest(TestApartmentVariableAllMethods<mock_platform>);
//}
//
//TEST_CASE("ComApartmentVariable::GetOrCreateForms", "[com][apartment_variable]")
//{
// RunApartmentVariableTest(TestApartmentVariableGetOrCreateForms<mock_platform>);
//}
//
//TEST_CASE("ComApartmentVariable::VariableLifetimes", "[com][apartment_variable]")
//{
// RunApartmentVariableTest(TestApartmentVariableLifetimes<mock_platform>);
//}
//
//TEST_CASE("ComApartmentVariable::WinningApartmentAlreadyRundownRace", "[com][apartment_variable]")
//{
// RunApartmentVariableTest(TestWinningApartmentAlreadyRundownRace<mock_platform>);
//}
//
//TEST_CASE("ComApartmentVariable::LosingApartmentAlreadyRundownRace", "[com][apartment_variable]")
//{
// RunApartmentVariableTest(TestLosingApartmentAlreadyRundownRace<mock_platform>);
//}
//
//TEST_CASE("ComApartmentVariable::MultipleApartments", "[com][apartment_variable]")
//{
// RunApartmentVariableTest(TestMultipleApartments<mock_platform>);
//}
//
//TEST_CASE("ComApartmentVariable::UseRealPlatformRunAllTests", "[com][apartment_variable]")
//{
// if (!wil::are_apartment_variables_supported())
// {
// return;
// }
//
// RunApartmentVariableTest(TestApartmentVariableAllMethods);
// RunApartmentVariableTest(TestApartmentVariableGetOrCreateForms);
// RunApartmentVariableTest(TestApartmentVariableLifetimes);
// RunApartmentVariableTest(TestWinningApartmentAlreadyRundownRace);
// RunApartmentVariableTest(TestLosingApartmentAlreadyRundownRace);
// RunApartmentVariableTest(TestMultipleApartments);
//}

#endif
18 changes: 18 additions & 0 deletions tests/CppWinRTAuthoringTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,24 @@ TEST_CASE("CppWinRTAuthoringTests::MultiRegisterComServer", "[com_server]")
}
}

TEST_CASE("CppWinRTAuthoringTests::MultiRegisterComServerWithMakeArrayOfUuid", "[com_server]")
{
auto revoker = wil::register_com_server(wil::make_array_of_uuid<MyServer, BuggyServer>());

auto instance = create_server_instance(__uuidof(MyServer));
REQUIRE(winrt::get_module_lock() == 1);

try
{
auto instance2 = create_server_instance(__uuidof(BuggyServer));
REQUIRE(false);
}
catch (winrt::hresult_error const& e)
{
REQUIRE(e.code() == E_ACCESSDENIED);
}
}

TEST_CASE("CppWinRTAuthoringTests::MultiRegisterComServerUnregistersOnFail", "[com_server]")
{
winrt::init_apartment();
Expand Down
Loading