From a6e69bda71440f40d6c9d289258a69c0942b90ba Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 16 Feb 2020 19:36:15 +0100 Subject: [PATCH] AK: Add basic Traits for RefPtr This allows RefPtr to be stored in a HashTable> :^) It's unfortunate about the const_casts. We'll need to fix HashMap::get to play nice with non-const Traits::PeekType at some point. --- AK/RefPtr.h | 8 ++++++++ Applications/IRCClient/IRCClient.cpp | 2 +- Libraries/LibC/dlfcn.cpp | 5 ++--- Libraries/LibProtocol/Client.cpp | 2 +- Servers/ProtocolServer/Download.cpp | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/AK/RefPtr.h b/AK/RefPtr.h index b942cc0b78..c3cd1aa556 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -29,6 +29,7 @@ #include #include #include +#include #include namespace AK { @@ -274,6 +275,13 @@ inline const LogStream& operator<<(const LogStream& stream, const RefPtr& val return stream << value.ptr(); } +template +struct Traits> : public GenericTraits> { + using PeekType = const T*; + static unsigned hash(const RefPtr& p) { return int_hash((u32)p.ptr()); } + static bool equals(const RefPtr& a, const RefPtr& b) { return a.ptr() == b.ptr(); } +}; + } using AK::RefPtr; diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp index f4fa61cd42..be027cfe45 100644 --- a/Applications/IRCClient/IRCClient.cpp +++ b/Applications/IRCClient/IRCClient.cpp @@ -436,7 +436,7 @@ void IRCClient::handle_privmsg_or_notice(const Message& msg, PrivmsgOrNotice typ IRCQuery* IRCClient::query_with_name(const String& name) { - return m_queries.get(name).value_or(nullptr); + return const_cast(m_queries.get(name).value_or(nullptr)); } IRCQuery& IRCClient::ensure_query(const String& name) diff --git a/Libraries/LibC/dlfcn.cpp b/Libraries/LibC/dlfcn.cpp index c7bea4b50b..9531511525 100644 --- a/Libraries/LibC/dlfcn.cpp +++ b/Libraries/LibC/dlfcn.cpp @@ -74,8 +74,7 @@ void* dlopen(const char* filename, int flags) auto existing_elf_object = g_elf_objects.get(file_path.basename()); if (existing_elf_object.has_value()) { - void* referenced_object = existing_elf_object.value().leak_ref(); - return referenced_object; + return const_cast(existing_elf_object.value()); } int fd = open(filename, O_RDONLY); @@ -110,7 +109,7 @@ void* dlopen(const char* filename, int flags) g_dlerror_msg = "Successfully loaded ELF object."; // we have one refcount already - return g_elf_objects.get(file_path.basename()).value().ptr(); + return const_cast(g_elf_objects.get(file_path.basename()).value()); } void* dlsym(void* handle, const char* symbol_name) diff --git a/Libraries/LibProtocol/Client.cpp b/Libraries/LibProtocol/Client.cpp index bf8a93aeaa..55c8754c07 100644 --- a/Libraries/LibProtocol/Client.cpp +++ b/Libraries/LibProtocol/Client.cpp @@ -74,7 +74,7 @@ void Client::handle(const Messages::ProtocolClient::DownloadFinished& message) void Client::handle(const Messages::ProtocolClient::DownloadProgress& message) { - if (auto download = m_downloads.get(message.download_id()).value_or(nullptr)) { + if (auto download = const_cast(m_downloads.get(message.download_id()).value_or(nullptr))) { download->did_progress({}, message.total_size(), message.downloaded_size()); } } diff --git a/Servers/ProtocolServer/Download.cpp b/Servers/ProtocolServer/Download.cpp index 000725ffb7..23764f0ff7 100644 --- a/Servers/ProtocolServer/Download.cpp +++ b/Servers/ProtocolServer/Download.cpp @@ -39,7 +39,7 @@ static HashMap>& all_downloads() Download* Download::find_by_id(i32 id) { - return all_downloads().get(id).value_or(nullptr); + return const_cast(all_downloads().get(id).value_or(nullptr)); } Download::Download(PSClientConnection& client)