1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 17:07:35 +00:00

AK: Add missing GenericTraits<NonnullRefPtr>

This enables us to use keys of type NonnullRefPtr in HashMaps and
HashTables.

This commit also includes fixes in various places that used
HashMap<T, NonnullRefPtr<U>>::get() and expected to get an
Optional<NonnullRefPtr<U>> and now get an Optional<U*>.
This commit is contained in:
Itamar 2021-05-08 12:27:12 +03:00 committed by Andreas Kling
parent 1da0d402b7
commit 8a01167c7d
6 changed files with 20 additions and 7 deletions

View file

@ -9,6 +9,7 @@
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Atomic.h> #include <AK/Atomic.h>
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/Traits.h>
#include <AK/Types.h> #include <AK/Types.h>
#ifdef KERNEL #ifdef KERNEL
# include <Kernel/Arch/x86/CPU.h> # include <Kernel/Arch/x86/CPU.h>
@ -335,5 +336,13 @@ inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b)
} }
template<typename T>
struct Traits<NonnullRefPtr<T>> : public GenericTraits<NonnullRefPtr<T>> {
using PeekType = T*;
using ConstPeekType = const T*;
static unsigned hash(const NonnullRefPtr<T>& p) { return ptr_hash(p.ptr()); }
static bool equals(const NonnullRefPtr<T>& a, const NonnullRefPtr<T>& b) { return a.ptr() == b.ptr(); }
};
using AK::adopt_ref; using AK::adopt_ref;
using AK::NonnullRefPtr; using AK::NonnullRefPtr;

View file

@ -7,6 +7,7 @@
#include "ParserAutoComplete.h" #include "ParserAutoComplete.h"
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/HashTable.h> #include <AK/HashTable.h>
#include <AK/OwnPtr.h>
#include <LibCpp/AST.h> #include <LibCpp/AST.h>
#include <LibCpp/Lexer.h> #include <LibCpp/Lexer.h>
#include <LibCpp/Parser.h> #include <LibCpp/Parser.h>

View file

@ -7,6 +7,7 @@
#include "FileDB.h" #include "FileDB.h"
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/NonnullRefPtr.h>
#include <LibCore/File.h> #include <LibCore/File.h>
namespace LanguageServers { namespace LanguageServers {
@ -18,7 +19,7 @@ RefPtr<const GUI::TextDocument> FileDB::get(const String& filename) const
if (!document_optional.has_value()) if (!document_optional.has_value())
return nullptr; return nullptr;
return document_optional.value(); return adopt_ref(*document_optional.value());
} }
RefPtr<GUI::TextDocument> FileDB::get(const String& filename) RefPtr<GUI::TextDocument> FileDB::get(const String& filename)

View file

@ -311,7 +311,7 @@ static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> load_main_library(co
loader.load_stage_4(); loader.load_stage_4();
} }
return main_library_loader; return NonnullRefPtr<DynamicLoader>(*main_library_loader);
} }
static Result<void, DlErrorMessage> __dlclose(void* handle) static Result<void, DlErrorMessage> __dlclose(void* handle)
@ -377,7 +377,8 @@ static Result<void*, DlErrorMessage> __dlopen(const char* filename, int flags)
auto existing_elf_object = s_global_objects.get(library_name); auto existing_elf_object = s_global_objects.get(library_name);
if (existing_elf_object.has_value()) { if (existing_elf_object.has_value()) {
// It's up to the caller to release the ref with dlclose(). // It's up to the caller to release the ref with dlclose().
return &existing_elf_object->leak_ref(); existing_elf_object.value()->ref();
return *existing_elf_object;
} }
VERIFY(!library_name.is_empty()); VERIFY(!library_name.is_empty());
@ -406,7 +407,8 @@ static Result<void*, DlErrorMessage> __dlopen(const char* filename, int flags)
return DlErrorMessage { "Could not load ELF object." }; return DlErrorMessage { "Could not load ELF object." };
// It's up to the caller to release the ref with dlclose(). // It's up to the caller to release the ref with dlclose().
return &object->leak_ref(); object.value()->ref();
return *object;
} }
static Result<void*, DlErrorMessage> __dlsym(void* handle, const char* symbol_name) static Result<void*, DlErrorMessage> __dlsym(void* handle, const char* symbol_name)

View file

@ -144,7 +144,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
dbgln("App {} has icon with size {}", app.name, icon->size()); dbgln("App {} has icon with size {}", app.name, icon->size());
} }
auto parent_menu = app_category_menus.get(app.category).value_or(*system_menu); auto parent_menu = app_category_menus.get(app.category).value_or(system_menu.ptr());
parent_menu->add_action(GUI::Action::create(app.name, icon, [app_identifier](auto&) { parent_menu->add_action(GUI::Action::create(app.name, icon, [app_identifier](auto&) {
dbgln("Activated app with ID {}", app_identifier); dbgln("Activated app with ID {}", app_identifier);
const auto& bin = g_apps[app_identifier].executable; const auto& bin = g_apps[app_identifier].executable;

View file

@ -46,14 +46,14 @@ public:
auto menu = m_menus.get(menu_id); auto menu = m_menus.get(menu_id);
if (!menu.has_value()) if (!menu.has_value())
return nullptr; return nullptr;
return const_cast<Menu*>(menu.value().ptr()); return menu.value();
} }
const Menu* find_menu_by_id(int menu_id) const const Menu* find_menu_by_id(int menu_id) const
{ {
auto menu = m_menus.get(menu_id); auto menu = m_menus.get(menu_id);
if (!menu.has_value()) if (!menu.has_value())
return nullptr; return nullptr;
return menu.value().ptr(); return menu.value();
} }
template<typename Callback> template<typename Callback>