From 19d9d5bfe16c99aaa87d02ee2d38d9d8003c497d Mon Sep 17 00:00:00 2001 From: creator1creeper1 Date: Sun, 9 Jan 2022 13:00:51 +0100 Subject: [PATCH] Everywhere: Mark Vector of mutable references as mutable The point of a reference type is to behave just like the referred-to type. So, a Foo& should behave just like a Foo. In these cases, we had a const Vector. If it was a const Vector of Foo, iterating over the Vector would only permit taking const references to the individual Foos. However, we had a const Vector of Foo&. The behavior should not change. We should still only be permitted to take const references to the individual Foos. Otherwise, we would be allowed to mutate the individual Foos, which would mutate the elements of the const Vector. This wouldn't modify the stored pointers, but it would modify the objects that the references refer to. Since references should be transparent, this should not be legal. So it should be impossible to get mutable references into a const Vector. Since we need mutable references in these cases to call the mutating member functions, we need to mark the Vector as mutable as well. --- Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp | 2 +- Userland/Applications/SystemMonitor/main.cpp | 2 +- Userland/Services/WindowServer/MenuManager.cpp | 2 +- Userland/Services/WindowServer/MenuManager.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index 6a7dcd5316..767626625a 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -308,7 +308,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector new_sheets) m_should_change_selected_cells = false; m_cell_value_editor->on_focusin = [this] { m_should_change_selected_cells = true; }; m_cell_value_editor->on_focusout = [this] { m_should_change_selected_cells = false; }; - m_cell_value_editor->on_change = [cells = move(cells), this] { + m_cell_value_editor->on_change = [cells = move(cells), this]() mutable { if (m_should_change_selected_cells) { auto* sheet_ptr = m_selected_view->sheet_if_available(); if (!sheet_ptr) diff --git a/Userland/Applications/SystemMonitor/main.cpp b/Userland/Applications/SystemMonitor/main.cpp index 719a8defdf..061388d605 100644 --- a/Userland/Applications/SystemMonitor/main.cpp +++ b/Userland/Applications/SystemMonitor/main.cpp @@ -698,7 +698,7 @@ NonnullRefPtr build_performance_tab() cpu_graphs.append(cpu_graph); } } - ProcessModel::the().on_cpu_info_change = [cpu_graphs](const NonnullOwnPtrVector& cpus) { + ProcessModel::the().on_cpu_info_change = [cpu_graphs](const NonnullOwnPtrVector& cpus) mutable { float sum_cpu = 0; for (size_t i = 0; i < cpus.size(); ++i) { cpu_graphs[i].add_value({ static_cast(cpus[i].total_cpu_percent), static_cast(cpus[i].total_cpu_percent_kernel) }); diff --git a/Userland/Services/WindowServer/MenuManager.cpp b/Userland/Services/WindowServer/MenuManager.cpp index 0ad3ed1a59..1d46feee29 100644 --- a/Userland/Services/WindowServer/MenuManager.cpp +++ b/Userland/Services/WindowServer/MenuManager.cpp @@ -235,7 +235,7 @@ void MenuManager::close_everyone_not_in_lineage(Menu& menu) close_menus(menus_to_close); } -void MenuManager::close_menus(const Vector& menus) +void MenuManager::close_menus(Vector& menus) { for (auto& menu : menus) { if (&menu == m_current_menu) diff --git a/Userland/Services/WindowServer/MenuManager.h b/Userland/Services/WindowServer/MenuManager.h index 74a73375df..ca3e2f38ae 100644 --- a/Userland/Services/WindowServer/MenuManager.h +++ b/Userland/Services/WindowServer/MenuManager.h @@ -49,7 +49,7 @@ public: private: MenuManager(); - void close_menus(const Vector&); + void close_menus(Vector&); virtual void event(Core::Event&) override; void handle_mouse_event(MouseEvent&);