Skip to content

Commit 47aef80

Browse files
authored
Add try_get_activation_factory overloads with class name (#769)
1 parent 5ffa6d9 commit 47aef80

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

strings/base_activation.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,9 @@ namespace winrt::impl
400400
return factory.call(static_cast<CastType>(callback));
401401
}
402402

403-
template <typename Class, typename Interface = Windows::Foundation::IActivationFactory>
404-
com_ref<Interface> try_get_activation_factory(hresult_error* exception = nullptr) noexcept
403+
template <typename Interface = Windows::Foundation::IActivationFactory>
404+
com_ref<Interface> try_get_activation_factory(param::hstring const& name, hresult_error* exception = nullptr) noexcept
405405
{
406-
param::hstring const name{ name_of<Class>() };
407406
void* result{};
408407
hresult const hr = get_runtime_activation_factory<Interface>(name, &result);
409408

@@ -463,7 +462,7 @@ WINRT_EXPORT namespace winrt
463462
{
464463
// Normally, the callback avoids having to return a ref-counted object and the resulting AddRef/Release bump.
465464
// In this case we do want a unique reference, so we use the lambda to return one and thus produce an
466-
// AddRef'd object that is returned to the caller.
465+
// AddRef'd object that is returned to the caller.
467466
return impl::call_factory<Class, Interface>([](auto&& factory)
468467
{
469468
return factory;
@@ -473,13 +472,25 @@ WINRT_EXPORT namespace winrt
473472
template <typename Class, typename Interface = Windows::Foundation::IActivationFactory>
474473
auto try_get_activation_factory() noexcept
475474
{
476-
return impl::try_get_activation_factory<Class, Interface>();
475+
return impl::try_get_activation_factory<Interface>(name_of<Class>());
477476
}
478477

479478
template <typename Class, typename Interface = Windows::Foundation::IActivationFactory>
480479
auto try_get_activation_factory(hresult_error& exception) noexcept
481480
{
482-
return impl::try_get_activation_factory<Class, Interface>(&exception);
481+
return impl::try_get_activation_factory<Interface>(name_of<Class>(), &exception);
482+
}
483+
484+
template <typename Interface = Windows::Foundation::IActivationFactory>
485+
auto try_get_activation_factory(param::hstring const& name) noexcept
486+
{
487+
return impl::try_get_activation_factory<Interface>(name);
488+
}
489+
490+
template <typename Interface = Windows::Foundation::IActivationFactory>
491+
auto try_get_activation_factory(param::hstring const& name, hresult_error& exception) noexcept
492+
{
493+
return impl::try_get_activation_factory<Interface>(name, &exception);
483494
}
484495

485496
inline void clear_factory_cache() noexcept

test/old_tests/UnitTests/get_activation_factory.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ TEST_CASE("get_activation_factory")
2626
REQUIRE_THROWS_AS(get_activation_factory(L"Composable.DoesNotExist"), hresult_class_not_registered);
2727
}
2828

29-
TEST_CASE("try_get_activation_factory")
29+
// Used to test whether the IRestrictedErrorInfo was left on the thread
30+
auto get_error_info()
3031
{
31-
// Used to test whether the IRestrictedErrorInfo was left on the thread
32-
auto get_error_info = []
33-
{
34-
com_ptr<IErrorInfo> info;
35-
GetErrorInfo(0, info.put());
36-
return info.as<impl::IRestrictedErrorInfo>();
37-
};
32+
com_ptr<IErrorInfo> info;
33+
GetErrorInfo(0, info.put());
34+
return info.as<impl::IRestrictedErrorInfo>();
35+
}
3836

37+
38+
TEST_CASE("try_get_activation_factory")
39+
{
3940
// Try successfully
4041
{
4142
auto factory = try_get_activation_factory<Component::Errors>();
@@ -67,3 +68,37 @@ TEST_CASE("try_get_activation_factory")
6768
REQUIRE(e.code() == REGDB_E_CLASSNOTREG);
6869
}
6970
}
71+
72+
TEST_CASE("try_get_activation_factory_with_names")
73+
{
74+
// Try successfully
75+
{
76+
auto factory = try_get_activation_factory(winrt::name_of<Component::Errors>());
77+
REQUIRE(factory != nullptr);
78+
REQUIRE(get_error_info() == nullptr);
79+
}
80+
81+
// Try unsuccessfully
82+
{
83+
auto factory = try_get_activation_factory(winrt::name_of<Component::IErrors>());
84+
REQUIRE(factory == nullptr);
85+
REQUIRE(get_error_info() == nullptr);
86+
}
87+
88+
// Try successfully with error info
89+
{
90+
hresult_error e;
91+
auto factory = try_get_activation_factory(winrt::name_of<Component::Errors>(), e);
92+
REQUIRE(factory != nullptr);
93+
REQUIRE(get_error_info() == nullptr);
94+
}
95+
96+
// Try unsuccessfully with error info
97+
{
98+
hresult_error e;
99+
auto factory = try_get_activation_factory(winrt::name_of<Component::IErrors>(), e);
100+
REQUIRE(factory == nullptr);
101+
REQUIRE(get_error_info() == nullptr);
102+
REQUIRE(e.code() == REGDB_E_CLASSNOTREG);
103+
}
104+
}

0 commit comments

Comments
 (0)