From 4f0be70254efab15530feaf5fb4894ff56fe8617 Mon Sep 17 00:00:00 2001 From: Raymond Chen Date: Wed, 8 Jun 2022 15:05:47 -0700 Subject: [PATCH] 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. --- cppwinrt/code_writers.h | 38 ++++++++++++++++++++++++++--------- cppwinrt/component_writers.h | 4 +++- test/test_component/Class.cpp | 7 +++++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/cppwinrt/code_writers.h b/cppwinrt/code_writers.h index 1781ebe61..748996b1a 100644 --- a/cppwinrt/code_writers.h +++ b/cppwinrt/code_writers.h @@ -2986,13 +2986,15 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable 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]] " : "", @@ -3010,17 +3012,33 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable 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(signature)); + if (is_opt_type) + { + auto format = R"( [[nodiscard]] static %_revoker %(auto_revoke_t, %); +)"; + w.write(format, + method_name, + method_name, + bind(signature)); + } + else + { + auto format = R"( [[nodiscard]] static auto %(auto_revoke_t, %); +)"; + w.write(format, + method_name, + bind(signature)); + } } } } diff --git a/cppwinrt/component_writers.h b/cppwinrt/component_writers.h index 8521a777e..acd2b871c 100644 --- a/cppwinrt/component_writers.h +++ b/cppwinrt/component_writers.h @@ -518,7 +518,7 @@ 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().as<%>(); return %::%_revoker{ f, f.%(%) }; @@ -526,6 +526,8 @@ catch (...) { return winrt::to_hresult(); } )"; w.write(format, + type_name, + method_name, type_name, method_name, bind(signature), diff --git a/test/test_component/Class.cpp b/test/test_component/Class.cpp index f328a7ddf..4f36df5c1 100644 --- a/test/test_component/Class.cpp +++ b/test/test_component/Class.cpp @@ -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&&) {}); + } } \ No newline at end of file