Structured binding support for IKeyValuePair (#590)

This commit is contained in:
Raymond Chen 2020-04-16 22:25:23 -07:00 коммит произвёл GitHub
Родитель 7b11196551
Коммит ae94947a23
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 68 добавлений и 0 удалений

Просмотреть файл

@ -66,3 +66,36 @@ WINRT_EXPORT namespace winrt
return make<impl::observable_map<K, V, std::unordered_map<K, V, Hash, KeyEqual, Allocator>>>(std::move(values));
}
}
namespace std
{
template<typename K, typename V>
struct tuple_size<winrt::Windows::Foundation::Collections::IKeyValuePair<K, V>>
: integral_constant<size_t, 2>
{
};
template<size_t Idx, typename K, typename V>
struct tuple_element<Idx, winrt::Windows::Foundation::Collections::IKeyValuePair<K, V>>
{
static_assert(Idx < 2, "key-value pair index out of bounds");
using type = conditional_t<Idx == 0, K, V>;
};
}
namespace winrt::Windows::Foundation::Collections
{
template<size_t Idx, typename K, typename V>
std::tuple_element_t<Idx, IKeyValuePair<K, V>> get(IKeyValuePair<K, V> const& kvp)
{
static_assert(Idx < 2, "key-value pair index out of bounds");
if constexpr (Idx == 0)
{
return kvp.Key();
}
else
{
return kvp.Value();
}
}
}

Просмотреть файл

@ -69,6 +69,21 @@ TEST_CASE("range_for,IIterable,IKeyValuePair")
for (IKeyValuePair<int32_t, hstring> i : c)
{
result[i.Key()] = i.Value();
// Sneak in a structured binding test.
auto const [key, value] = i;
REQUIRE(key == i.Key());
REQUIRE(value == i.Value());
}
REQUIRE(result == values);
// Do it again with structured bindings.
result.clear();
for (auto&& [key, value] : c)
{
result[key] = value;
}
REQUIRE(result == values);
@ -92,6 +107,16 @@ TEST_CASE("range_for,IMap")
}
REQUIRE(result == values);
// Do it again with structured bindings.
result.clear();
for (auto&& [key, value] : c)
{
result[key] = value;
}
REQUIRE(result == values);
}
TEST_CASE("range_for,IMapView")
@ -112,5 +137,15 @@ TEST_CASE("range_for,IMapView")
}
REQUIRE(result == values);
// Do it again with structured bindings.
result.clear();
for (auto&& [key, value] : c)
{
result[key] = value;
}
REQUIRE(result == values);
}