1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-21 11:55:07 +00:00

AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()

Add the concept of a PeekType to Traits<T>. This is the type we'll
return (wrapped in an Optional) from HashMap::get().

The PeekType for OwnPtr<T> and NonnullOwnPtr<T> is const T*,
which means that HashMap::get() will return an Optional<const T*> for
maps-of-those.
This commit is contained in:
Andreas Kling 2019-08-14 11:47:38 +02:00
parent f75b1127b2
commit fdcff7d15e
6 changed files with 40 additions and 2 deletions

View file

@ -76,4 +76,33 @@ TEST_CASE(assert_on_iteration_during_clear)
map.clear();
}
TEST_CASE(hashmap_of_nonnullownptr_get)
{
struct Object {
Object(const String& s) : string(s) {}
String string;
};
HashMap<int, NonnullOwnPtr<Object>> objects;
objects.set(1, make<Object>("One"));
objects.set(2, make<Object>("Two"));
objects.set(3, make<Object>("Three"));
{
auto x = objects.get(2);
EXPECT_EQ(x.has_value(), true);
EXPECT_EQ(x.value()->string, "Two");
}
{
// Do it again to make sure that peeking into the map above didn't
// remove the value from the map.
auto x = objects.get(2);
EXPECT_EQ(x.has_value(), true);
EXPECT_EQ(x.value()->string, "Two");
}
EXPECT_EQ(objects.size(), 3);
}
TEST_MAIN(HashMap)