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

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
10 changed files with 27 additions and 17 deletions

View file

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

View file

@ -103,7 +103,9 @@ public:
return fallback; return fallback;
} }
operator bool() const { return has_value(); }
private: private:
char m_storage[sizeof(T)] __attribute__((aligned(sizeof(T)))); char m_storage[sizeof(T)];
bool m_has_value { false }; 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) { font_menu->add_action(GAction::create(font_name, [&terminal, &config](const GAction& action) {
terminal.set_font(GFontDatabase::the().get_by_name(action.text())); terminal.set_font(GFontDatabase::the().get_by_name(action.text()));
auto metadata = GFontDatabase::the().get_metadata_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(); config->sync();
terminal.force_repaint(); terminal.force_repaint();
})); }));

View file

@ -93,7 +93,7 @@ VBWidget* VBForm::widget_at(const Point& position)
auto* gwidget = child_at(position); auto* gwidget = child_at(position);
if (!gwidget) if (!gwidget)
return nullptr; 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) 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 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 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 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 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()); buffer.append(payload.pointer(), payload.size());
bool ok; 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; m_state = State::Finished;
break; break;
} }

View file

@ -266,7 +266,7 @@ void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>&
} }
switch (event.type) { switch (event.type) {
case WSAPI_ServerMessage::Type::Paint: 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; ++coalesced_paints;
break; break;
} }
@ -295,7 +295,7 @@ void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>&
handle_window_entered_or_left_event(event, *window); handle_window_entered_or_left_event(event, *window);
break; break;
case WSAPI_ServerMessage::Type::WindowResized: 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; ++coalesced_resizes;
break; break;
} }

View file

@ -20,10 +20,10 @@ public:
void for_each_font(Function<void(const StringView&)>); void for_each_font(Function<void(const StringView&)>);
void for_each_fixed_width_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); return m_name_to_metadata.get(name);
}; }
private: private:
GFontDatabase(); GFontDatabase();

View file

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