Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions strings/base_collections_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,26 @@ namespace winrt::impl

bool IndexOf(Windows::Foundation::IInspectable const& value, uint32_t& index) const
{
try
if constexpr (is_com_interface_v<T>)
{
return IndexOf(unbox_value<T>(value), index);
if (!value)
{
return base_type::IndexOf(nullptr, index);
}
else if (auto as = value.try_as<T>())
{
return base_type::IndexOf(as, index);
}
}
catch (hresult_no_interface const&)
else
{
index = 0;
return false;
if (auto as = value.try_as<T>())
{
return base_type::IndexOf(as.value(), index);
}
}

return false;
}

using base_type::GetMany;
Expand Down
37 changes: 37 additions & 0 deletions test/test/observable_index_of.cpp
Original file line number Diff line number Diff line change
@@ -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<int>().as<IVector<IInspectable>>();
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<IStringable>().as<IVector<IInspectable>>();
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<IStringable>().as<IVector<IInspectable>>();
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));
}
}
1 change: 1 addition & 0 deletions test/test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
<ClCompile Include="notify_awaiter.cpp" />
<ClCompile Include="no_make_detection.cpp" />
<ClCompile Include="numerics.cpp" />
<ClCompile Include="observable_index_of.cpp" />
<ClCompile Include="out_params.cpp" />
<ClCompile Include="out_params_abi.cpp" />
<ClCompile Include="out_params_bad.cpp" />
Expand Down