From 7140a778bb35eb1c57025b7fdcb7920fb3622519 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 13 Sep 2021 07:57:16 -0700 Subject: [PATCH 1/2] observable_index_of --- strings/base_collections_vector.h | 22 ++++++++++++++---- test/test/observable_index_of.cpp | 37 +++++++++++++++++++++++++++++++ test/test/test.vcxproj | 1 + 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/test/observable_index_of.cpp diff --git a/strings/base_collections_vector.h b/strings/base_collections_vector.h index b82ba3853..27288be22 100644 --- a/strings/base_collections_vector.h +++ b/strings/base_collections_vector.h @@ -115,13 +115,27 @@ namespace winrt::impl bool IndexOf(Windows::Foundation::IInspectable const& value, uint32_t& index) const { - try + if constexpr (is_com_interface_v) { - return IndexOf(unbox_value(value), index); + if (!value) + { + return base_type::IndexOf(nullptr, index); + } + else if (auto as = value.try_as()) + { + return base_type::IndexOf(as, index); + } + else + { + return false; + } + } + else if (auto as = value.try_as()) + { + return base_type::IndexOf(as.value(), index); } - catch (hresult_no_interface const&) + else { - index = 0; return false; } } diff --git a/test/test/observable_index_of.cpp b/test/test/observable_index_of.cpp new file mode 100644 index 000000000..14d434d34 --- /dev/null +++ b/test/test/observable_index_of.cpp @@ -0,0 +1,37 @@ +#include "pch.h" + +using namespace winrt; +using namespace Windows::Foundation; +using namespace Windows::Foundation::Collections; + +TEST_CASE("observable_index_of") +{ + { + auto v = single_threaded_observable_vector().as>(); + v.Append(box_value(123)); + uint32_t index = 0; + REQUIRE(v.IndexOf(box_value(123), index)); + REQUIRE(!v.IndexOf(nullptr, index)); + REQUIRE(!v.IndexOf(box_value(456), index)); + REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index)); + } + { + auto value = Uri(L"http://kennykerr.ca"); + + auto v = single_threaded_observable_vector().as>(); + v.Append(value); + uint32_t index = 0; + REQUIRE(v.IndexOf(value, index)); + REQUIRE(!v.IndexOf(nullptr, index)); + REQUIRE(!v.IndexOf(box_value(456), index)); + REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index)); + } + { + auto v = single_threaded_observable_vector().as>(); + v.Append(nullptr); + uint32_t index = 0; + REQUIRE(v.IndexOf(nullptr, index)); + REQUIRE(!v.IndexOf(box_value(456), index)); + REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index)); + } +} diff --git a/test/test/test.vcxproj b/test/test/test.vcxproj index ed6e934bd..b6c9c229a 100644 --- a/test/test/test.vcxproj +++ b/test/test/test.vcxproj @@ -417,6 +417,7 @@ + From 03444f9635160d549f244af681a85d06d2cbc8bf Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 13 Sep 2021 09:18:38 -0700 Subject: [PATCH 2/2] make it clear --- strings/base_collections_vector.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/strings/base_collections_vector.h b/strings/base_collections_vector.h index 27288be22..ff0ad6bec 100644 --- a/strings/base_collections_vector.h +++ b/strings/base_collections_vector.h @@ -125,19 +125,16 @@ namespace winrt::impl { return base_type::IndexOf(as, index); } - else - { - return false; - } - } - else if (auto as = value.try_as()) - { - return base_type::IndexOf(as.value(), index); } else { - return false; + if (auto as = value.try_as()) + { + return base_type::IndexOf(as.value(), index); + } } + + return false; } using base_type::GetMany;