diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-07-01 13:08:14 -0400 |
---|---|---|
committer | Gunnar Beutner <gunnar@beutner.name> | 2021-07-03 15:41:35 +0200 |
commit | 27fe2b45e553c887d10684d80ca5869714f0e19d (patch) | |
tree | 1bc2a9a623a50abab4426446d3ce062743cc0913 /Userland/Applications/Assistant | |
parent | d69691a26b8833a56aeccd23e6e4b75b0b8a0798 (diff) | |
download | serenity-27fe2b45e553c887d10684d80ca5869714f0e19d.zip |
Assistant: Convert all Vector<NonnullRefPtr> to NonnullRefPtrVector
Diffstat (limited to 'Userland/Applications/Assistant')
-rw-r--r-- | Userland/Applications/Assistant/Providers.cpp | 22 | ||||
-rw-r--r-- | Userland/Applications/Assistant/Providers.h | 2 | ||||
-rw-r--r-- | Userland/Applications/Assistant/main.cpp | 33 |
3 files changed, 31 insertions, 26 deletions
diff --git a/Userland/Applications/Assistant/Providers.cpp b/Userland/Applications/Assistant/Providers.cpp index 0873bf91c6..f6a4ef3e99 100644 --- a/Userland/Applications/Assistant/Providers.cpp +++ b/Userland/Applications/Assistant/Providers.cpp @@ -62,12 +62,12 @@ void URLResult::activate() const Desktop::Launcher::open(URL::create_with_url_or_path(title())); } -void AppProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) +void AppProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) { if (query.starts_with("=") || query.starts_with('$')) return; - Vector<NonnullRefPtr<Result>> results; + NonnullRefPtrVector<Result> results; Desktop::AppFile::for_each([&](NonnullRefPtr<Desktop::AppFile> app_file) { auto match_result = fuzzy_match(query, app_file->name()); @@ -81,7 +81,7 @@ void AppProvider::query(String const& query, Function<void(Vector<NonnullRefPtr< on_complete(results); } -void CalculatorProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) +void CalculatorProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) { if (!query.starts_with("=")) return; @@ -107,20 +107,20 @@ void CalculatorProvider::query(String const& query, Function<void(Vector<Nonnull calculation = result.to_string_without_side_effects(); } - Vector<NonnullRefPtr<Result>> results; + NonnullRefPtrVector<Result> results; results.append(adopt_ref(*new CalculatorResult(calculation))); on_complete(results); } -void FileProvider::query(const String& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) +void FileProvider::query(const String& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) { build_filesystem_cache(); if (m_fuzzy_match_work) m_fuzzy_match_work->cancel(); - m_fuzzy_match_work = Threading::BackgroundAction<Vector<NonnullRefPtr<Result>>>::create([this, query](auto& task) { - Vector<NonnullRefPtr<Result>> results; + m_fuzzy_match_work = Threading::BackgroundAction<NonnullRefPtrVector<Result>>::create([this, query](auto& task) { + NonnullRefPtrVector<Result> results; for (auto& path : m_full_path_cache) { if (task.is_cancelled()) @@ -172,19 +172,19 @@ void FileProvider::build_filesystem_cache() return 0; }, [this](auto) { m_building_cache = false; }); } -void TerminalProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) +void TerminalProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) { if (!query.starts_with('$')) return; auto command = query.substring(1); - Vector<NonnullRefPtr<Result>> results; + NonnullRefPtrVector<Result> results; results.append(adopt_ref(*new TerminalResult(move(command)))); on_complete(results); } -void URLProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) +void URLProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) { URL url = URL(query); @@ -198,7 +198,7 @@ void URLProvider::query(String const& query, Function<void(Vector<NonnullRefPtr< if (!url.is_valid()) return; - Vector<NonnullRefPtr<Result>> results; + NonnullRefPtrVector<Result> results; results.append(adopt_ref(*new URLResult(url))); on_complete(results); } diff --git a/Userland/Applications/Assistant/Providers.h b/Userland/Applications/Assistant/Providers.h index e8be8deb1b..71c2b03e89 100644 --- a/Userland/Applications/Assistant/Providers.h +++ b/Userland/Applications/Assistant/Providers.h @@ -141,7 +141,7 @@ public: class URLProvider : public Provider { public: - void query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override; + void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override; }; } diff --git a/Userland/Applications/Assistant/main.cpp b/Userland/Applications/Assistant/main.cpp index 8140c23caf..7ee79fbbed 100644 --- a/Userland/Applications/Assistant/main.cpp +++ b/Userland/Applications/Assistant/main.cpp @@ -22,7 +22,7 @@ namespace Assistant { struct AppState { Optional<size_t> selected_index; - Vector<NonnullRefPtr<Result>> results; + NonnullRefPtrVector<Result> results; size_t visible_result_count { 0 }; Threading::Lock lock; @@ -123,7 +123,7 @@ public: m_file_provider.build_filesystem_cache(); } - Function<void(Vector<NonnullRefPtr<Result>>)> on_new_results; + Function<void(NonnullRefPtrVector<Result>)> on_new_results; void search(String const& query) { @@ -149,7 +149,7 @@ public: } private: - void recv_results(String const& query, Vector<NonnullRefPtr<Result>> const& results) + void recv_results(String const& query, NonnullRefPtrVector<Result> const& results) { { Threading::Locker db_locker(m_lock); @@ -160,8 +160,8 @@ private: it = m_result_cache.find(query); for (auto& result : results) { - auto found = it->value.find_if([result](auto& other) { - return result->equals(other); + auto found = it->value.find_if([&result](auto& other) { + return result.equals(other); }); if (found.is_end()) @@ -174,7 +174,12 @@ private: if (new_results == m_result_cache.end()) return; - dual_pivot_quick_sort(new_results->value, 0, static_cast<int>(new_results->value.size() - 1), [](auto& a, auto& b) { + // NonnullRefPtrVector will provide dual_pivot_quick_sort references rather than pointers, + // and dual_pivot_quick_sort requires being able to construct the underlying type on the + // stack. Assistant::Result is pure virtual, thus cannot be constructed on the stack. + auto& sortable_results = static_cast<Vector<NonnullRefPtr<Result>>&>(new_results->value); + + dual_pivot_quick_sort(sortable_results, 0, static_cast<int>(sortable_results.size() - 1), [](auto& a, auto& b) { return a->score() > b->score(); }); @@ -190,7 +195,7 @@ private: URLProvider m_url_provider; Threading::Lock m_lock; - HashMap<String, Vector<NonnullRefPtr<Result>>> m_result_cache; + HashMap<String, NonnullRefPtrVector<Result>> m_result_cache; }; } @@ -241,7 +246,7 @@ int main(int argc, char** argv) text_box.on_return_pressed = [&]() { if (!app_state.selected_index.has_value()) return; - app_state.results[app_state.selected_index.value()]->activate(); + app_state.results[app_state.selected_index.value()].activate(); exit(0); }; text_box.on_up_pressed = [&]() { @@ -283,13 +288,13 @@ int main(int argc, char** argv) results_container.remove_all_children(); for (size_t i = 0; i < app_state.visible_result_count; ++i) { - auto result = app_state.results[i]; + auto& result = app_state.results[i]; auto& match = results_container.add<Assistant::ResultRow>(); - match.set_image(result->bitmap()); - match.set_title(result->title()); - match.set_subtitle(result->subtitle()); - match.on_selected = [result_copy = result]() { - result_copy->activate(); + match.set_image(result.bitmap()); + match.set_title(result.title()); + match.set_subtitle(result.subtitle()); + match.on_selected = [&result]() { + result.activate(); exit(0); }; } |