Skip to content

Commit

Permalink
AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Aug 14, 2019
1 parent f75b112 commit fdcff7d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion AK/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class HashMap {

void dump() const { m_table.dump(); }

Optional<V> get(const K& key) const
Optional<typename Traits<V>::PeekType> get(const K& key) const
{
auto it = find(key);
if (it == end())
Expand Down
1 change: 1 addition & 0 deletions AK/NonnullOwnPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ make(Args&&... args)

template<typename T>
struct Traits<NonnullOwnPtr<T>> : public GenericTraits<NonnullOwnPtr<T>> {
using PeekType = const T*;
static unsigned hash(const NonnullOwnPtr<T>& p) { return (unsigned)p.ptr(); }
static void dump(const NonnullOwnPtr<T>& p) { kprintf("%p", p.ptr()); }
static bool equals(const NonnullOwnPtr<T>& a, const NonnullOwnPtr<T>& b) { return a.ptr() == b.ptr(); }
Expand Down
1 change: 1 addition & 0 deletions AK/OwnPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class OwnPtr {

template<typename T>
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
using PeekType = const T*;
static unsigned hash(const OwnPtr<T>& p) { return (unsigned)p.ptr(); }
static void dump(const OwnPtr<T>& p) { kprintf("%p", p.ptr()); }
static bool equals(const OwnPtr<T>& a, const OwnPtr<T>& b) { return a.ptr() == b.ptr(); }
Expand Down
6 changes: 6 additions & 0 deletions AK/RefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ class RefPtr {
return exchange(m_ptr, nullptr);
}

NonnullRefPtr<T> release_nonnull()
{
ASSERT(m_ptr);
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *leak_ref());
}

T* ptr() { return m_ptr; }
const T* ptr() const { return m_ptr; }

Expand Down
29 changes: 29 additions & 0 deletions AK/Tests/TestHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion AK/Traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace AK {

template<typename T>
struct GenericTraits {
using PeekType = T;
static constexpr bool is_trivial() { return false; }
static bool equals(const T& a, const T& b) { return a == b; }
};
Expand Down Expand Up @@ -44,7 +45,7 @@ struct Traits<char> : public GenericTraits<char> {
};

template<typename T>
struct Traits<T*> {
struct Traits<T*> : public GenericTraits<T*> {
static unsigned hash(const T* p)
{
return int_hash((unsigned)(__PTRDIFF_TYPE__)p);
Expand Down

0 comments on commit fdcff7d

Please sign in to comment.