Browse Source

AK: Add basic Traits for RefPtr

This allows RefPtr to be stored in a HashTable<RefPtr<T>> :^)

It's unfortunate about the const_casts. We'll need to fix HashMap::get
to play nice with non-const Traits<T>::PeekType at some point.
Andreas Kling 5 years ago
parent
commit
a6e69bda71

+ 8 - 0
AK/RefPtr.h

@@ -29,6 +29,7 @@
 #include <AK/LogStream.h>
 #include <AK/NonnullRefPtr.h>
 #include <AK/StdLibExtras.h>
+#include <AK/Traits.h>
 #include <AK/Types.h>
 
 namespace AK {
@@ -274,6 +275,13 @@ inline const LogStream& operator<<(const LogStream& stream, const RefPtr<T>& val
     return stream << value.ptr();
 }
 
+template<typename T>
+struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
+    using PeekType = const T*;
+    static unsigned hash(const RefPtr<T>& p) { return int_hash((u32)p.ptr()); }
+    static bool equals(const RefPtr<T>& a, const RefPtr<T>& b) { return a.ptr() == b.ptr(); }
+};
+
 }
 
 using AK::RefPtr;

+ 1 - 1
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<IRCQuery*>(m_queries.get(name).value_or(nullptr));
 }
 
 IRCQuery& IRCClient::ensure_query(const String& name)

+ 2 - 3
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<ELFDynamicLoader*>(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<ELFDynamicLoader*>(g_elf_objects.get(file_path.basename()).value());
 }
 
 void* dlsym(void* handle, const char* symbol_name)

+ 1 - 1
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<Download*>(m_downloads.get(message.download_id()).value_or(nullptr))) {
         download->did_progress({}, message.total_size(), message.downloaded_size());
     }
 }

+ 1 - 1
Servers/ProtocolServer/Download.cpp

@@ -39,7 +39,7 @@ static HashMap<i32, RefPtr<Download>>& all_downloads()
 
 Download* Download::find_by_id(i32 id)
 {
-    return all_downloads().get(id).value_or(nullptr);
+    return const_cast<Download*>(all_downloads().get(id).value_or(nullptr));
 }
 
 Download::Download(PSClientConnection& client)