AK: Make HashMap::get(Key) return an Optional<Value>.

This allows HashMap::get() to be used for value types that cannot be default
constructed (e.g NonnullOwnPtr.)
This commit is contained in:
Andreas Kling 2019-07-24 10:25:43 +02:00
parent e2798d6208
commit 1d0b464618
Notes: sideshowbarker 2024-07-19 13:04:18 +09:00
10 changed files with 27 additions and 17 deletions

View file

@ -1,6 +1,7 @@
#pragma once
#include <AK/HashTable.h>
#include <AK/Optional.h>
#include <AK/StdLibExtras.h>
#include <AK/Vector.h>
#include <AK/kstdio.h>
@ -51,21 +52,27 @@ public:
IteratorType begin() { return m_table.begin(); }
IteratorType end() { return m_table.end(); }
IteratorType find(const K& key) { return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); }); }
IteratorType find(const K& key)
{
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
}
ConstIteratorType begin() const { return m_table.begin(); }
ConstIteratorType end() const { return m_table.end(); }
ConstIteratorType find(const K& key) const { return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); }); }
ConstIteratorType find(const K& key) const
{
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
}
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
void dump() const { m_table.dump(); }
V get(const K& key) const
Optional<V> get(const K& key) const
{
auto it = find(key);
if (it == end())
return V();
return {};
return (*it).value;
}

View file

@ -103,7 +103,9 @@ public:
return fallback;
}
operator bool() const { return has_value(); }
private:
char m_storage[sizeof(T)] __attribute__((aligned(sizeof(T))));
char m_storage[sizeof(T)];
bool m_has_value { false };
};

View file

@ -184,7 +184,8 @@ int main(int argc, char** argv)
font_menu->add_action(GAction::create(font_name, [&terminal, &config](const GAction& action) {
terminal.set_font(GFontDatabase::the().get_by_name(action.text()));
auto metadata = GFontDatabase::the().get_metadata_by_name(action.text());
config->write_entry("Text", "Font", metadata.path);
ASSERT(metadata.has_value());
config->write_entry("Text", "Font", metadata.value().path);
config->sync();
terminal.force_repaint();
}));

View file

@ -93,7 +93,7 @@ VBWidget* VBForm::widget_at(const Point& position)
auto* gwidget = child_at(position);
if (!gwidget)
return nullptr;
return m_gwidget_map.get(gwidget);
return m_gwidget_map.get(gwidget).value_or(nullptr);
}
void VBForm::grabber_mousedown_event(GMouseEvent& event, Direction grabber)

View file

@ -29,7 +29,7 @@ KParams::KParams(const String& cmdline)
String KParams::get(const String& key) const
{
return m_params.get(key);
return m_params.get(key).value_or({});
}
bool KParams::has(const String& key) const

View file

@ -10,7 +10,7 @@ bool CArgsParserResult::is_present(const String& arg_name) const
String CArgsParserResult::get(const String& arg_name) const
{
return m_args.get(arg_name);
return m_args.get(arg_name).value_or({});
}
const Vector<String>& CArgsParserResult::get_single_values() const

View file

@ -92,7 +92,7 @@ void CHttpJob::on_socket_connected()
buffer.append(payload.pointer(), payload.size());
bool ok;
if (buffer.size() >= m_headers.get("Content-Length").to_int(ok) && ok) {
if (buffer.size() >= m_headers.get("Content-Length").value_or("0").to_int(ok) && ok) {
m_state = State::Finished;
break;
}

View file

@ -266,7 +266,7 @@ void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>&
}
switch (event.type) {
case WSAPI_ServerMessage::Type::Paint:
if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id)) {
if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id).value_or({})) {
++coalesced_paints;
break;
}
@ -295,7 +295,7 @@ void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>&
handle_window_entered_or_left_event(event, *window);
break;
case WSAPI_ServerMessage::Type::WindowResized:
if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id)) {
if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id).value_or({})) {
++coalesced_resizes;
break;
}

View file

@ -20,10 +20,10 @@ public:
void for_each_font(Function<void(const StringView&)>);
void for_each_fixed_width_font(Function<void(const StringView&)>);
Metadata get_metadata_by_name(const StringView& name) const
Optional<Metadata> get_metadata_by_name(const StringView& name) const
{
return m_name_to_metadata.get(name);
};
}
private:
GFontDatabase();

View file

@ -157,9 +157,9 @@ int main(int argc, char** argv)
for (auto& key : dns_custom_hostnames.keys()) {
dbgprintf("Known hostname: '%s'\n", key.characters());
}
if (dns_custom_hostnames.contains(hostname)) {
responses.append(dns_custom_hostnames.get(hostname));
dbgprintf("LookupServer: Found preconfigured host (from /etc/hosts): %s\n", responses[0].characters());
if (auto known_host = dns_custom_hostnames.get(hostname)) {
responses.append(known_host.value());
dbg() << "LookupServer: Found preconfigured host (from /etc/hosts): " << known_host.value();
} else if (!hostname.is_empty()) {
bool did_timeout;
int retries = 3;