Skip to content

Commit bb0a408

Browse files
authored
Simplify the definition of the consume function (#1544)
1 parent 751a57b commit bb0a408

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

cppwinrt/code_writers.h

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,15 @@ namespace cppwinrt
619619
}
620620
}
621621

622-
static void write_abi_args(writer& w, method_signature const& method_signature)
622+
static void write_abi_args(writer& w, method_signature const& method_signature, bool start_comma)
623623
{
624624
separator s{ w };
625625

626+
if (start_comma)
627+
{
628+
s();
629+
}
630+
626631
for (auto&& [param, param_signature] : method_signature.params())
627632
{
628633
s();
@@ -1135,39 +1140,15 @@ namespace cppwinrt
11351140
// immediately while preserving the error code and local variables.
11361141
format = R"( template <typename D%> auto consume_%<D%>::%(%) const noexcept
11371142
{%
1138-
if constexpr (!std::is_same_v<D, %>)
1139-
{
1140-
winrt::hresult _winrt_cast_result_code;
1141-
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
1142-
check_hresult(_winrt_cast_result_code);
1143-
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
1144-
_winrt_abi_type->%(%);
1145-
}
1146-
else
1147-
{
1148-
auto const _winrt_abi_type = *(abi_t<%>**)this;
1149-
_winrt_abi_type->%(%);
1150-
}%
1143+
consume_noexcept_remove_overload<%, D>(static_cast<D const*>(this), &abi_t<%>::%%);%
11511144
}
11521145
)";
11531146
}
11541147
else
11551148
{
11561149
format = R"( template <typename D%> auto consume_%<D%>::%(%) const noexcept
11571150
{%
1158-
if constexpr (!std::is_same_v<D, %>)
1159-
{
1160-
winrt::hresult _winrt_cast_result_code;
1161-
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
1162-
check_hresult(_winrt_cast_result_code);
1163-
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
1164-
WINRT_VERIFY_(0, _winrt_abi_type->%(%));
1165-
}
1166-
else
1167-
{
1168-
auto const _winrt_abi_type = *(abi_t<%>**)this;
1169-
WINRT_VERIFY_(0, _winrt_abi_type->%(%));
1170-
}%
1151+
consume_noexcept<%, D>(static_cast<D const*>(this), &abi_t<%>::%%);%
11711152
}
11721153
)";
11731154
}
@@ -1176,19 +1157,7 @@ namespace cppwinrt
11761157
{
11771158
format = R"( template <typename D%> auto consume_%<D%>::%(%) const
11781159
{%
1179-
if constexpr (!std::is_same_v<D, %>)
1180-
{
1181-
winrt::hresult _winrt_cast_result_code;
1182-
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
1183-
check_hresult(_winrt_cast_result_code);
1184-
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
1185-
check_hresult(_winrt_abi_type->%(%));
1186-
}
1187-
else
1188-
{
1189-
auto const _winrt_abi_type = *(abi_t<%>**)this;
1190-
check_hresult(_winrt_abi_type->%(%));
1191-
}%
1160+
consume_general<%, D>(static_cast<D const*>(this), &abi_t<%>::%%);%
11921161
}
11931162
)";
11941163
}
@@ -1202,12 +1171,8 @@ namespace cppwinrt
12021171
bind<write_consume_return_type>(signature, false),
12031172
type,
12041173
type,
1205-
type,
1206-
get_abi_name(method),
1207-
bind<write_abi_args>(signature),
1208-
type,
12091174
get_abi_name(method),
1210-
bind<write_abi_args>(signature),
1175+
bind<write_abi_args>(signature, true),
12111176
bind<write_consume_return_statement>(signature));
12121177

12131178
if (is_add_overload(method))
@@ -2750,7 +2715,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
27502715
bind<write_consume_return_type>(signature, true),
27512716
type_name,
27522717
bind_list(", ", generics),
2753-
bind<write_abi_args>(signature),
2718+
bind<write_abi_args>(signature, false),
27542719
bind<write_consume_return_statement>(signature));
27552720
}
27562721
else
@@ -2822,7 +2787,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
28222787
bind<write_consume_params>(signature),
28232788
bind<write_consume_return_type>(signature, true),
28242789
type_name,
2825-
bind<write_abi_args>(signature),
2790+
bind<write_abi_args>(signature, false),
28262791
bind<write_consume_return_statement>(signature));
28272792
}
28282793
}

strings/base_windows.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,60 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
451451
IInspectable(void* ptr, take_ownership_from_abi_t) noexcept : IUnknown(ptr, take_ownership_from_abi) {}
452452
};
453453
}
454+
455+
WINRT_EXPORT namespace winrt::impl
456+
{
457+
template <typename Base, typename Derive, typename MemberPointer, typename ...Args>
458+
void consume_noexcept_remove_overload(Derive const* d, MemberPointer mptr, Args&&... args) noexcept
459+
{
460+
if constexpr (!std::is_same_v<Derive, Base>)
461+
{
462+
winrt::hresult _winrt_cast_result_code;
463+
auto const _winrt_casted_result = try_as_with_reason<Base, Derive const*>(d, _winrt_cast_result_code);
464+
check_hresult(_winrt_cast_result_code);
465+
auto const _winrt_abi_type = *(abi_t<Base>**)&_winrt_casted_result;
466+
(_winrt_abi_type->*mptr)(std::forward<Args>(args)...);
467+
}
468+
else
469+
{
470+
auto const _winrt_abi_type = *(abi_t<Base>**)d;
471+
(_winrt_abi_type->*mptr)(std::forward<Args>(args)...);
472+
}
473+
}
474+
475+
template <typename Base, typename Derive, typename MemberPointer, typename ...Args>
476+
void consume_noexcept(Derive const* d, MemberPointer mptr, Args&&... args) noexcept
477+
{
478+
if constexpr (!std::is_same_v<Derive, Base>)
479+
{
480+
winrt::hresult _winrt_cast_result_code;
481+
auto const _winrt_casted_result = try_as_with_reason<Base, Derive const*>(d, _winrt_cast_result_code);
482+
check_hresult(_winrt_cast_result_code);
483+
auto const _winrt_abi_type = *(abi_t<Base>**)&_winrt_casted_result;
484+
WINRT_VERIFY_(0, (_winrt_abi_type->*mptr)(std::forward<Args>(args)...));
485+
}
486+
else
487+
{
488+
auto const _winrt_abi_type = *(abi_t<Base>**)d;
489+
WINRT_VERIFY_(0, (_winrt_abi_type->*mptr)(std::forward<Args>(args)...));
490+
}
491+
}
492+
493+
template <typename Base, typename Derive, typename MemberPointer, typename ...Args>
494+
void consume_general(Derive const* d, MemberPointer mptr, Args&&... args)
495+
{
496+
if constexpr (!std::is_same_v<Derive, Base>)
497+
{
498+
winrt::hresult _winrt_cast_result_code;
499+
auto const _winrt_casted_result = try_as_with_reason<Base, Derive const*>(d, _winrt_cast_result_code);
500+
check_hresult(_winrt_cast_result_code);
501+
auto const _winrt_abi_type = *(abi_t<Base>**)&_winrt_casted_result;
502+
check_hresult((_winrt_abi_type->*mptr)(std::forward<Args>(args)...));
503+
}
504+
else
505+
{
506+
auto const _winrt_abi_type = *(abi_t<Base>**)d;
507+
check_hresult((_winrt_abi_type->*mptr)(std::forward<Args>(args)...));
508+
}
509+
}
510+
}

0 commit comments

Comments
 (0)