Skip to content

Commit 6d4d14a

Browse files
committed
Revert "Remove try_as_with_reason and just call as"
This reverts commit 96f637e.
1 parent 96f637e commit 6d4d14a

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

cppwinrt/code_writers.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,14 +1130,15 @@ namespace cppwinrt
11301130
{
11311131
// we intentionally ignore errors when unregistering event handlers to be consistent with event_revoker
11321132
//
1133-
// The `noexcept` versions will crash if .as<>() throws but that is no different than previous
1133+
// The `noexcept` versions will crash if check_hresult throws but that is no different than previous
11341134
// behavior where it would not check the cast result and nullptr crash. At least the exception will terminate
11351135
// immediately while preserving the error code and local variables.
11361136
format = R"( template <typename D%> auto consume_%<D%>::%(%) const noexcept
11371137
{%
11381138
if constexpr (!std::is_same_v<D, %>)
11391139
{
1140-
auto const& castedResult = static_cast<D const*>(this)->template as<%>();
1140+
auto const& [castedResult, code] = static_cast<D const*>(this)->template try_as_with_reason<%>();
1141+
check_hresult(code);
11411142
auto const abiType = *(abi_t<%>**)&castedResult;
11421143
abiType->%(%);
11431144
}
@@ -1155,7 +1156,8 @@ namespace cppwinrt
11551156
{%
11561157
if constexpr (!std::is_same_v<D, %>)
11571158
{
1158-
auto const& castedResult = static_cast<D const*>(this)->template as<%>();
1159+
auto const& [castedResult, code] = static_cast<D const*>(this)->template try_as_with_reason<%>();
1160+
check_hresult(code);
11591161
auto const abiType = *(abi_t<%>**)&castedResult;
11601162
WINRT_VERIFY_(0, abiType->%(%));
11611163
}
@@ -1174,7 +1176,8 @@ namespace cppwinrt
11741176
{%
11751177
if constexpr (!std::is_same_v<D, %>)
11761178
{
1177-
auto const& castedResult = static_cast<D const*>(this)->template as<%>();
1179+
auto const& [castedResult, code] = static_cast<D const*>(this)->template try_as_with_reason<%>();
1180+
check_hresult(code);
11781181
auto const abiType = *(abi_t<%>**)&castedResult;
11791182
check_hresult(abiType->%(%));
11801183
}

strings/base_implements.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,15 +783,15 @@ namespace winrt::impl
783783
struct WINRT_IMPL_EMPTY_BASES root_implements_composing_outer<true>
784784
{
785785
template <typename Qi>
786-
auto as() const noexcept
786+
auto try_as() const noexcept
787787
{
788-
return m_inner.as<Qi>();
788+
return m_inner.try_as<Qi>();
789789
}
790790

791791
template <typename Qi>
792-
auto try_as() const noexcept
792+
auto try_as_with_reason() const noexcept
793793
{
794-
return m_inner.try_as<Qi>();
794+
return m_inner.try_as_with_reason<Qi>();
795795
}
796796

797797
explicit operator bool() const noexcept

strings/base_windows.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ namespace winrt::impl
131131
ptr->QueryInterface(guid_of<To>(), &result);
132132
return wrap_as_result<To>(result);
133133
}
134+
135+
template <typename To, typename From, std::enable_if_t<is_com_interface_v<To>, int> = 0>
136+
std::pair<com_ref<To>, hresult> try_as_with_reason(From* ptr) noexcept
137+
{
138+
#ifdef WINRT_DIAGNOSTICS
139+
get_diagnostics_info().add_query<To>();
140+
#endif
141+
142+
if (!ptr)
143+
{
144+
return { nullptr, 0 };
145+
}
146+
147+
void* result{};
148+
hresult code = ptr->QueryInterface(guid_of<To>(), &result);
149+
return { wrap_as_result<To>(result), code };
150+
}
134151
}
135152

136153
WINRT_EXPORT namespace winrt::Windows::Foundation
@@ -205,6 +222,12 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
205222
return impl::try_as<To>(m_ptr);
206223
}
207224

225+
template <typename To>
226+
auto try_as_with_reason() const noexcept
227+
{
228+
return impl::try_as_with_reason<To>(m_ptr);
229+
}
230+
208231
template <typename To>
209232
void as(To& to) const
210233
{

0 commit comments

Comments
 (0)