Skip to content

Commit 1b4ea75

Browse files
committed
Stop originating error context in try_as and use a new try_as_reason instead for consume methods
1 parent d296af6 commit 1b4ea75

File tree

4 files changed

+38
-26
lines changed

4 files changed

+38
-26
lines changed

cppwinrt/code_writers.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,9 @@ namespace cppwinrt
11371137
{%
11381138
if constexpr (!std::is_same_v<D, %>)
11391139
{
1140-
auto const& castedResult = static_cast<% const&>(static_cast<D const&>(*this));
1140+
auto const [castedResult, code] = static_cast<D const*>(this)->template try_as_reason<%>();
1141+
check_cast_result(code);
11411142
auto const abiType = *(abi_t<%>**)&castedResult;
1142-
check_cast_result(abiType);
11431143
abiType->%(%);
11441144
}
11451145
else
@@ -1156,9 +1156,9 @@ namespace cppwinrt
11561156
{%
11571157
if constexpr (!std::is_same_v<D, %>)
11581158
{
1159-
auto const& castedResult = static_cast<% const&>(static_cast<D const&>(*this));
1159+
auto const [castedResult, code] = static_cast<D const*>(this)->template try_as_reason<%>();
1160+
check_cast_result(code);
11601161
auto const abiType = *(abi_t<%>**)&castedResult;
1161-
check_cast_result(abiType);
11621162
WINRT_VERIFY_(0, abiType->%(%));
11631163
}
11641164
else
@@ -1176,9 +1176,9 @@ namespace cppwinrt
11761176
{%
11771177
if constexpr (!std::is_same_v<D, %>)
11781178
{
1179-
auto const& castedResult = static_cast<% const&>(static_cast<D const&>(*this));
1179+
auto const [castedResult, code] = static_cast<D const*>(this)->template try_as_reason<%>();
1180+
check_cast_result(code);
11801181
auto const abiType = *(abi_t<%>**)&castedResult;
1181-
check_cast_result(abiType);
11821182
check_hresult(abiType->%(%));
11831183
}
11841184
else

strings/base_error.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -539,24 +539,11 @@ namespace winrt::impl
539539
return result;
540540
}
541541

542-
inline WINRT_IMPL_NOINLINE void check_cast_result(void* from, winrt::impl::slim_source_location const& sourceInformation = winrt::impl::slim_source_location::current())
542+
inline WINRT_IMPL_NOINLINE void check_cast_result(hresult const result, winrt::impl::slim_source_location const& sourceInformation = winrt::impl::slim_source_location::current())
543543
{
544-
if (!from)
544+
if (result != 0)
545545
{
546-
com_ptr<impl::IRestrictedErrorInfo> restrictedError;
547-
if (WINRT_IMPL_GetRestrictedErrorInfo(restrictedError.put_void()) == 0)
548-
{
549-
WINRT_IMPL_SetRestrictedErrorInfo(restrictedError.get());
550-
551-
int32_t code;
552-
impl::bstr_handle description;
553-
impl::bstr_handle restrictedDescription;
554-
impl::bstr_handle capabilitySid;
555-
if (restrictedError->GetErrorDetails(description.put(), &code, restrictedDescription.put(), capabilitySid.put()) == 0)
556-
{
557-
throw hresult_error(code, take_ownership_from_abi, sourceInformation);
558-
}
559-
}
546+
throw hresult_error(result, take_ownership_from_abi, sourceInformation);
560547
}
561548
}
562549
}

strings/base_implements.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,12 @@ namespace winrt::impl
788788
return m_inner.try_as<Qi>();
789789
}
790790

791+
template <typename Qi>
792+
auto try_as_reason() const noexcept
793+
{
794+
return m_inner.try_as_reason<Qi>();
795+
}
796+
791797
explicit operator bool() const noexcept
792798
{
793799
return m_inner.operator bool();

strings/base_windows.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,25 @@ namespace winrt::impl
128128
}
129129

130130
void* result{};
131-
hresult code = ptr->QueryInterface(guid_of<To>(), &result);
132-
if (code < 0)
131+
ptr->QueryInterface(guid_of<To>(), &result);
132+
return wrap_as_result<To>(result);
133+
}
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_reason(From* ptr) noexcept
137+
{
138+
#ifdef WINRT_DIAGNOSTICS
139+
get_diagnostics_info().add_query<To>();
140+
#endif
141+
142+
if (!ptr)
133143
{
134-
WINRT_IMPL_RoCaptureErrorContext(code);
144+
return { nullptr, 0 };
135145
}
136-
return wrap_as_result<To>(result);
146+
147+
void* result{};
148+
hresult code = ptr->QueryInterface(guid_of<To>(), &result);
149+
return { wrap_as_result<To>(result), code };
137150
}
138151
}
139152

@@ -209,6 +222,12 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
209222
return impl::try_as<To>(m_ptr);
210223
}
211224

225+
template <typename To>
226+
auto try_as_reason() const noexcept
227+
{
228+
return impl::try_as_reason<To>(m_ptr);
229+
}
230+
212231
template <typename To>
213232
void as(To& to) const
214233
{

0 commit comments

Comments
 (0)