Skip to content

Commit

Permalink
Static events should not use the auto trick (#1158)
Browse files Browse the repository at this point in the history
The same way we don't use the auto trick for static properties
and static methods.
  • Loading branch information
oldnewthing authored Jun 8, 2022
1 parent a54d678 commit 4f0be70
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
38 changes: 28 additions & 10 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2986,13 +2986,15 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
return;
}

auto is_opt_type = settings.component_opt && settings.component_filter.includes(type);

for (auto&& method : factory.second.type.MethodList())
{
method_signature signature{ method };
auto method_name = get_name(method);
auto async_types_guard = w.push_async_types(signature.is_async());

if (settings.component_opt && settings.component_filter.includes(type))
if (is_opt_type)
{
w.write(" %static % %(%);\n",
is_get_overload(method) ? "[[nodiscard]] " : "",
Expand All @@ -3010,17 +3012,33 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>

if (is_add_overload(method))
{
auto format = R"( using %_revoker = impl::factory_event_revoker<%, &impl::abi_t<%>::remove_%>;
[[nodiscard]] static auto %(auto_revoke_t, %);
{
auto format = R"( using %_revoker = impl::factory_event_revoker<%, &impl::abi_t<%>::remove_%>;
)";
w.write(format,
method_name,
factory.second.type,
factory.second.type,
method_name);
}

w.write(format,
method_name,
factory.second.type,
factory.second.type,
method_name,
method_name,
bind<write_consume_params>(signature));
if (is_opt_type)
{
auto format = R"( [[nodiscard]] static %_revoker %(auto_revoke_t, %);
)";
w.write(format,
method_name,
method_name,
bind<write_consume_params>(signature));
}
else
{
auto format = R"( [[nodiscard]] static auto %(auto_revoke_t, %);
)";
w.write(format,
method_name,
bind<write_consume_params>(signature));
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion cppwinrt/component_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,16 @@ catch (...) { return winrt::to_hresult(); }

if (is_add_overload(method))
{
auto format = R"( auto %::%(auto_revoke_t, %)
auto format = R"( %::%_revoker %::%(auto_revoke_t, %)
{
auto f = make<winrt::@::factory_implementation::%>().as<%>();
return %::%_revoker{ f, f.%(%) };
}
)";

w.write(format,
type_name,
method_name,
type_name,
method_name,
bind<write_consume_params>(signature),
Expand Down
7 changes: 7 additions & 0 deletions test/test_component/Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,11 @@ namespace winrt::test_component::implementation
}
return pass;
}
}

namespace
{
void ValidateStaticEventAutoRevoke() {
auto x = winrt::test_component::Simple::StaticEvent(winrt::auto_revoke, [](auto&&, auto&&) {});
}
}

0 comments on commit 4f0be70

Please sign in to comment.