diff --git a/AK/HashMap.h b/AK/HashMap.h index 554309fcfb..5833515f76 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -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 get(const K& key) const { auto it = find(key); if (it == end()) - return V(); + return {}; return (*it).value; } diff --git a/AK/Optional.h b/AK/Optional.h index 9dbef306fb..9af81ac13e 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -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 }; }; diff --git a/Applications/Terminal/main.cpp b/Applications/Terminal/main.cpp index 3be7ae441e..3d35caeee6 100644 --- a/Applications/Terminal/main.cpp +++ b/Applications/Terminal/main.cpp @@ -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(); })); diff --git a/DevTools/VisualBuilder/VBForm.cpp b/DevTools/VisualBuilder/VBForm.cpp index 6cec82cbbc..3cf2fa0dfd 100644 --- a/DevTools/VisualBuilder/VBForm.cpp +++ b/DevTools/VisualBuilder/VBForm.cpp @@ -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) diff --git a/Kernel/KParams.cpp b/Kernel/KParams.cpp index 26f4df771a..67a66aa079 100644 --- a/Kernel/KParams.cpp +++ b/Kernel/KParams.cpp @@ -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 diff --git a/Libraries/LibCore/CArgsParser.cpp b/Libraries/LibCore/CArgsParser.cpp index 12ce7c90b4..d4d7751a46 100644 --- a/Libraries/LibCore/CArgsParser.cpp +++ b/Libraries/LibCore/CArgsParser.cpp @@ -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& CArgsParserResult::get_single_values() const diff --git a/Libraries/LibCore/CHttpJob.cpp b/Libraries/LibCore/CHttpJob.cpp index a3b00b9175..ac56627aff 100644 --- a/Libraries/LibCore/CHttpJob.cpp +++ b/Libraries/LibCore/CHttpJob.cpp @@ -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; } diff --git a/Libraries/LibGUI/GEventLoop.cpp b/Libraries/LibGUI/GEventLoop.cpp index 9c77f1740c..97668053f1 100644 --- a/Libraries/LibGUI/GEventLoop.cpp +++ b/Libraries/LibGUI/GEventLoop.cpp @@ -266,7 +266,7 @@ void GWindowServerConnection::postprocess_bundles(Vector& } 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& 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; } diff --git a/Libraries/LibGUI/GFontDatabase.h b/Libraries/LibGUI/GFontDatabase.h index 46d4f26ace..d949862c82 100644 --- a/Libraries/LibGUI/GFontDatabase.h +++ b/Libraries/LibGUI/GFontDatabase.h @@ -20,10 +20,10 @@ public: void for_each_font(Function); void for_each_fixed_width_font(Function); - Metadata get_metadata_by_name(const StringView& name) const + Optional get_metadata_by_name(const StringView& name) const { return m_name_to_metadata.get(name); - }; + } private: GFontDatabase(); diff --git a/Servers/LookupServer/main.cpp b/Servers/LookupServer/main.cpp index 97df0b1f77..ddf058231d 100644 --- a/Servers/LookupServer/main.cpp +++ b/Servers/LookupServer/main.cpp @@ -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;