Skip to content

Commit 4f0be70

Browse files
authored
Static events should not use the auto trick (#1158)
The same way we don't use the auto trick for static properties and static methods.
1 parent a54d678 commit 4f0be70

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

cppwinrt/code_writers.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,13 +2986,15 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
29862986
return;
29872987
}
29882988

2989+
auto is_opt_type = settings.component_opt && settings.component_filter.includes(type);
2990+
29892991
for (auto&& method : factory.second.type.MethodList())
29902992
{
29912993
method_signature signature{ method };
29922994
auto method_name = get_name(method);
29932995
auto async_types_guard = w.push_async_types(signature.is_async());
29942996

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

30113013
if (is_add_overload(method))
30123014
{
3013-
auto format = R"( using %_revoker = impl::factory_event_revoker<%, &impl::abi_t<%>::remove_%>;
3014-
[[nodiscard]] static auto %(auto_revoke_t, %);
3015+
{
3016+
auto format = R"( using %_revoker = impl::factory_event_revoker<%, &impl::abi_t<%>::remove_%>;
30153017
)";
3018+
w.write(format,
3019+
method_name,
3020+
factory.second.type,
3021+
factory.second.type,
3022+
method_name);
3023+
}
30163024

3017-
w.write(format,
3018-
method_name,
3019-
factory.second.type,
3020-
factory.second.type,
3021-
method_name,
3022-
method_name,
3023-
bind<write_consume_params>(signature));
3025+
if (is_opt_type)
3026+
{
3027+
auto format = R"( [[nodiscard]] static %_revoker %(auto_revoke_t, %);
3028+
)";
3029+
w.write(format,
3030+
method_name,
3031+
method_name,
3032+
bind<write_consume_params>(signature));
3033+
}
3034+
else
3035+
{
3036+
auto format = R"( [[nodiscard]] static auto %(auto_revoke_t, %);
3037+
)";
3038+
w.write(format,
3039+
method_name,
3040+
bind<write_consume_params>(signature));
3041+
}
30243042
}
30253043
}
30263044
}

cppwinrt/component_writers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,16 @@ catch (...) { return winrt::to_hresult(); }
518518

519519
if (is_add_overload(method))
520520
{
521-
auto format = R"( auto %::%(auto_revoke_t, %)
521+
auto format = R"( %::%_revoker %::%(auto_revoke_t, %)
522522
{
523523
auto f = make<winrt::@::factory_implementation::%>().as<%>();
524524
return %::%_revoker{ f, f.%(%) };
525525
}
526526
)";
527527

528528
w.write(format,
529+
type_name,
530+
method_name,
529531
type_name,
530532
method_name,
531533
bind<write_consume_params>(signature),

test/test_component/Class.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,11 @@ namespace winrt::test_component::implementation
515515
}
516516
return pass;
517517
}
518+
}
519+
520+
namespace
521+
{
522+
void ValidateStaticEventAutoRevoke() {
523+
auto x = winrt::test_component::Simple::StaticEvent(winrt::auto_revoke, [](auto&&, auto&&) {});
524+
}
518525
}

0 commit comments

Comments
 (0)