Everywhere: Replace Vector<T*> with nonnull entries with Vector<T&>

This commit is contained in:
Ali Mohammad Pur 2021-06-08 19:36:27 +04:30 committed by Andreas Kling
parent 3d94b5051d
commit 7ac196974d
22 changed files with 92 additions and 94 deletions

View file

@ -21,16 +21,16 @@ String CookieJar::get_cookie(const URL& url, Web::Cookie::Source source)
if (!domain.has_value())
return {};
Vector<Web::Cookie::Cookie*> cookie_list = get_matching_cookies(url, domain.value(), source);
auto cookie_list = get_matching_cookies(url, domain.value(), source);
StringBuilder builder;
for (const auto* cookie : cookie_list) {
for (const auto& cookie : cookie_list) {
// If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ")
if (!builder.is_empty())
builder.append("; ");
// Output the cookie's name, the %x3D ("=") character, and the cookie's value.
builder.appendff("{}={}", cookie->name, cookie->value);
builder.appendff("{}={}", cookie.name, cookie.value);
}
return builder.build();
@ -238,14 +238,14 @@ void CookieJar::store_cookie(const Web::Cookie::ParsedCookie& parsed_cookie, con
m_cookies.set(key, move(cookie));
}
Vector<Web::Cookie::Cookie*> CookieJar::get_matching_cookies(const URL& url, const String& canonicalized_domain, Web::Cookie::Source source)
Vector<Web::Cookie::Cookie&> CookieJar::get_matching_cookies(const URL& url, const String& canonicalized_domain, Web::Cookie::Source source)
{
// https://tools.ietf.org/html/rfc6265#section-5.4
auto now = Core::DateTime::now();
// 1. Let cookie-list be the set of cookies from the cookie store that meets all of the following requirements:
Vector<Web::Cookie::Cookie*> cookie_list;
Vector<Web::Cookie::Cookie&> cookie_list;
for (auto& cookie : m_cookies) {
// Either: The cookie's host-only-flag is true and the canonicalized request-host is identical to the cookie's domain.
@ -270,11 +270,11 @@ Vector<Web::Cookie::Cookie*> CookieJar::get_matching_cookies(const URL& url, con
// 2. The user agent SHOULD sort the cookie-list in the following order:
// - Cookies with longer paths are listed before cookies with shorter paths.
// - Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times.
cookie_list.insert_before_matching(&cookie.value, [&cookie](auto* entry) {
if (cookie.value.path.length() > entry->path.length()) {
cookie_list.insert_before_matching(cookie.value, [&cookie](auto& entry) {
if (cookie.value.path.length() > entry.path.length()) {
return true;
} else if (cookie.value.path.length() == entry->path.length()) {
if (cookie.value.creation_time.timestamp() < entry->creation_time.timestamp())
} else if (cookie.value.path.length() == entry.path.length()) {
if (cookie.value.creation_time.timestamp() < entry.creation_time.timestamp())
return true;
}
return false;

View file

@ -37,7 +37,7 @@ private:
static String default_path(const URL& url);
void store_cookie(const Web::Cookie::ParsedCookie& parsed_cookie, const URL& url, String canonicalized_domain, Web::Cookie::Source source);
Vector<Web::Cookie::Cookie*> get_matching_cookies(const URL& url, const String& canonicalized_domain, Web::Cookie::Source source);
Vector<Web::Cookie::Cookie&> get_matching_cookies(const URL& url, const String& canonicalized_domain, Web::Cookie::Source source);
void purge_expired_cookies();
HashMap<CookieStorageKey, Web::Cookie::Cookie> m_cookies;

View file

@ -136,9 +136,9 @@ void KeyboardMapperWidget::load_from_file(String filename)
m_character_map = result.value();
set_current_map("map");
for (Widget* widget : m_map_group->child_widgets()) {
auto radio_button = (GUI::RadioButton*)widget;
radio_button->set_checked(radio_button->name() == "map");
for (auto& widget : m_map_group->child_widgets()) {
auto& radio_button = static_cast<GUI::RadioButton&>(widget);
radio_button.set_checked(radio_button.name() == "map");
}
update_window_title();
@ -153,9 +153,9 @@ void KeyboardMapperWidget::load_from_system()
m_character_map = result.value().character_map_data();
set_current_map("map");
for (Widget* widget : m_map_group->child_widgets()) {
auto radio_button = (GUI::RadioButton*)widget;
radio_button->set_checked(radio_button->name() == "map");
for (auto& widget : m_map_group->child_widgets()) {
auto& radio_button = static_cast<GUI::RadioButton&>(widget);
radio_button.set_checked(radio_button.name() == "map");
}
update_window_title();

View file

@ -112,14 +112,14 @@ void CellTypeDialog::setup_tabs(GUI::TabWidget& tabs, const Vector<Position>& po
for (auto& type_name : CellType::names())
g_types.append(type_name);
Vector<Cell*> cells;
Vector<Cell&> cells;
for (auto& position : positions) {
if (auto cell = sheet.at(position))
cells.append(cell);
cells.append(*cell);
}
if (cells.size() == 1) {
auto& cell = *cells.first();
auto& cell = cells.first();
m_format = cell.type_metadata().format;
m_length = cell.type_metadata().length;
m_type = &cell.type();

View file

@ -115,18 +115,18 @@ void Sheet::update()
return;
}
m_visited_cells_in_update.clear();
Vector<Cell*> cells_copy;
Vector<Cell&> cells_copy;
// Grab a copy as updates might insert cells into the table.
for (auto& it : m_cells) {
if (it.value->dirty()) {
cells_copy.append(it.value);
cells_copy.append(*it.value);
m_workbook.set_dirty(true);
}
}
for (auto& cell : cells_copy)
update(*cell);
update(cell);
m_visited_cells_in_update.clear();
}

View file

@ -171,11 +171,11 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
m_current_cell_label->set_enabled(true);
m_current_cell_label->set_text(builder.string_view());
Vector<Cell*> cells;
Vector<Cell&> cells;
for (auto& position : selection)
cells.append(&sheet.ensure(position));
cells.append(sheet.ensure(position));
auto first_cell = cells.first();
auto& first_cell = cells.first();
m_cell_value_editor->on_change = nullptr;
m_cell_value_editor->set_text("");
m_should_change_selected_cells = false;
@ -191,14 +191,14 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
// FIXME: Lines?
auto offset = m_cell_value_editor->cursor().column();
try_generate_tip_for_input_expression(text, offset);
for (auto* cell : cells)
cell->set_data(text);
for (auto& cell : cells)
cell.set_data(text);
sheet.update();
update();
}
};
m_cell_value_editor->set_enabled(true);
static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(first_cell);
static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&first_cell);
};
m_selected_view->on_selection_dropped = [&]() {
m_cell_value_editor->set_enabled(false);

View file

@ -663,7 +663,7 @@ NonnullRefPtr<GUI::Widget> build_graphs_tab()
cpu_graph_group_box.set_layout<GUI::HorizontalBoxLayout>();
cpu_graph_group_box.layout()->set_margins({ 6, 16, 6, 6 });
cpu_graph_group_box.set_fixed_height(120);
Vector<GraphWidget*> cpu_graphs;
Vector<GraphWidget&> cpu_graphs;
for (size_t i = 0; i < ProcessModel::the().cpus().size(); i++) {
auto& cpu_graph = cpu_graph_group_box.add<GraphWidget>();
cpu_graph.set_max(100);
@ -679,12 +679,12 @@ NonnullRefPtr<GUI::Widget> build_graphs_tab()
return String::formatted("Kernel: {}%", value);
},
});
cpu_graphs.append(&cpu_graph);
cpu_graphs.append(cpu_graph);
}
ProcessModel::the().on_cpu_info_change = [cpu_graphs](const NonnullOwnPtrVector<ProcessModel::CpuInfo>& cpus) {
float sum_cpu = 0;
for (size_t i = 0; i < cpus.size(); ++i) {
cpu_graphs[i]->add_value({ (int)cpus[i].total_cpu_percent, (int)cpus[i].total_cpu_percent_kernel });
cpu_graphs[i].add_value({ (int)cpus[i].total_cpu_percent, (int)cpus[i].total_cpu_percent_kernel });
sum_cpu += cpus[i].total_cpu_percent;
}
float cpu_usage = sum_cpu / (float)cpus.size();

View file

@ -489,17 +489,17 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action
return GUI::Action::create("Switch to &Next Editor", { Mod_Ctrl, Key_E }, [this](auto&) {
if (m_all_editor_wrappers.size() <= 1)
return;
Vector<EditorWrapper*> wrappers;
Vector<EditorWrapper&> wrappers;
m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
wrappers.append(&child);
wrappers.append(child);
return IterationDecision::Continue;
});
for (size_t i = 0; i < wrappers.size(); ++i) {
if (m_current_editor_wrapper.ptr() == wrappers[i]) {
if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
if (i == wrappers.size() - 1)
wrappers[0]->editor().set_focus(true);
wrappers[0].editor().set_focus(true);
else
wrappers[i + 1]->editor().set_focus(true);
wrappers[i + 1].editor().set_focus(true);
}
}
});
@ -510,17 +510,17 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_ac
return GUI::Action::create("Switch to &Previous Editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
if (m_all_editor_wrappers.size() <= 1)
return;
Vector<EditorWrapper*> wrappers;
Vector<EditorWrapper&> wrappers;
m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
wrappers.append(&child);
wrappers.append(child);
return IterationDecision::Continue;
});
for (int i = wrappers.size() - 1; i >= 0; --i) {
if (m_current_editor_wrapper.ptr() == wrappers[i]) {
if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
if (i == 0)
wrappers.last()->editor().set_focus(true);
wrappers.last().editor().set_focus(true);
else
wrappers[i - 1]->editor().set_focus(true);
wrappers[i - 1].editor().set_focus(true);
}
}
});

View file

@ -58,7 +58,7 @@ struct EventLoop::Private {
};
static EventLoop* s_main_event_loop;
static Vector<EventLoop*>* s_event_loop_stack;
static Vector<EventLoop&>* s_event_loop_stack;
static NeverDestroyed<IDAllocator> s_id_allocator;
static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
static HashTable<Notifier*>* s_notifiers;
@ -257,7 +257,7 @@ EventLoop::EventLoop([[maybe_unused]] MakeInspectable make_inspectable)
: m_private(make<Private>())
{
if (!s_event_loop_stack) {
s_event_loop_stack = new Vector<EventLoop*>;
s_event_loop_stack = new Vector<EventLoop&>;
s_timers = new HashMap<int, NonnullOwnPtr<EventLoopTimer>>;
s_notifiers = new HashTable<Notifier*>;
}
@ -274,7 +274,7 @@ EventLoop::EventLoop([[maybe_unused]] MakeInspectable make_inspectable)
#endif
VERIFY(rc == 0);
s_event_loop_stack->append(this);
s_event_loop_stack->append(*this);
#ifdef __serenity__
if (getuid() != 0
@ -314,9 +314,7 @@ EventLoop& EventLoop::main()
EventLoop& EventLoop::current()
{
EventLoop* event_loop = s_event_loop_stack->last();
VERIFY(event_loop != nullptr);
return *event_loop;
return s_event_loop_stack->last();
}
void EventLoop::quit(int code)
@ -340,7 +338,7 @@ public:
{
if (&m_event_loop != s_main_event_loop) {
m_event_loop.take_pending_events_from(EventLoop::current());
s_event_loop_stack->append(&event_loop);
s_event_loop_stack->append(event_loop);
}
}
~EventLoopPusher()

View file

@ -362,8 +362,8 @@ void ColorPicker::create_color_button(Widget& container, unsigned rgb)
auto& widget = container.add<ColorButton>(*this, color);
widget.on_click = [this](Color color) {
for (auto& value : m_color_widgets) {
value->set_selected(false);
value->update();
value.set_selected(false);
value.update();
}
m_color = color;
@ -375,7 +375,7 @@ void ColorPicker::create_color_button(Widget& container, unsigned rgb)
widget.set_selected(true);
}
m_color_widgets.append(&widget);
m_color_widgets.append(widget);
}
ColorButton::ColorButton(ColorPicker& picker, Color color)

View file

@ -37,7 +37,7 @@ private:
Color m_color;
bool m_color_has_alpha_channel { true };
Vector<ColorButton*> m_color_widgets;
Vector<ColorButton&> m_color_widgets;
RefPtr<CustomColorWidget> m_custom_color;
RefPtr<ColorPreview> m_preview_widget;
RefPtr<TextBox> m_html_text;

View file

@ -155,7 +155,7 @@ void Splitter::recompute_grabbables()
m_hovered_index = {};
auto child_widgets = this->child_widgets();
child_widgets.remove_all_matching([&](auto& widget) { return !widget->is_visible(); });
child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });
if (child_widgets.size() < 2)
return;
@ -164,8 +164,8 @@ void Splitter::recompute_grabbables()
size_t end_index = 1;
while (end_index < child_widgets.size()) {
auto const& first_widget = *child_widgets[start_index];
auto const& second_widget = *child_widgets[end_index];
auto const& first_widget = child_widgets[start_index];
auto const& second_widget = child_widgets[end_index];
m_grabbables.append(Grabbable {
.index = m_grabbables.size(),
.grabbable_rect = rect_between_widgets(first_widget, second_widget, true),

View file

@ -851,14 +851,14 @@ void Widget::focus_previous_widget(FocusSource source, bool siblings_only)
{
auto focusable_widgets = window()->focusable_widgets(source);
if (siblings_only)
focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
focusable_widgets.remove_all_matching([this](auto& entry) { return entry.parent() != parent(); });
for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
if (focusable_widgets[i] != this)
if (&focusable_widgets[i] != this)
continue;
if (i > 0)
focusable_widgets[i - 1]->set_focus(true, source);
focusable_widgets[i - 1].set_focus(true, source);
else
focusable_widgets.last()->set_focus(true, source);
focusable_widgets.last().set_focus(true, source);
}
}
@ -866,24 +866,24 @@ void Widget::focus_next_widget(FocusSource source, bool siblings_only)
{
auto focusable_widgets = window()->focusable_widgets(source);
if (siblings_only)
focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
focusable_widgets.remove_all_matching([this](auto& entry) { return entry.parent() != parent(); });
for (size_t i = 0; i < focusable_widgets.size(); ++i) {
if (focusable_widgets[i] != this)
if (&focusable_widgets[i] != this)
continue;
if (i < focusable_widgets.size() - 1)
focusable_widgets[i + 1]->set_focus(true, source);
focusable_widgets[i + 1].set_focus(true, source);
else
focusable_widgets.first()->set_focus(true, source);
focusable_widgets.first().set_focus(true, source);
}
}
Vector<Widget*> Widget::child_widgets() const
Vector<Widget&> Widget::child_widgets() const
{
Vector<Widget*> widgets;
Vector<Widget&> widgets;
widgets.ensure_capacity(children().size());
for (auto& child : const_cast<Widget*>(this)->children()) {
if (is<Widget>(child))
widgets.append(static_cast<Widget*>(&child));
widgets.append(static_cast<Widget&>(child));
}
return widgets;
}

View file

@ -250,7 +250,7 @@ public:
});
}
Vector<Widget*> child_widgets() const;
Vector<Widget&> child_widgets() const;
void do_layout();

View file

@ -843,13 +843,13 @@ void Window::start_interactive_resize()
WindowServerConnection::the().async_start_window_resize(m_window_id);
}
Vector<Widget*> Window::focusable_widgets(FocusSource source) const
Vector<Widget&> Window::focusable_widgets(FocusSource source) const
{
if (!m_main_widget)
return {};
HashTable<Widget*> seen_widgets;
Vector<Widget*> collected_widgets;
Vector<Widget&> collected_widgets;
Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
bool widget_accepts_focus = false;
@ -868,7 +868,7 @@ Vector<Widget*> Window::focusable_widgets(FocusSource source) const
if (widget_accepts_focus) {
auto& effective_focus_widget = widget.focus_proxy() ? *widget.focus_proxy() : widget;
if (seen_widgets.set(&effective_focus_widget) == AK::HashSetResult::InsertedNewEntry)
collected_widgets.append(&effective_focus_widget);
collected_widgets.append(effective_focus_widget);
}
widget.for_each_child_widget([&](auto& child) {
if (!child.is_visible())
@ -1056,7 +1056,7 @@ void Window::focus_a_widget_if_possible(FocusSource source)
{
auto focusable_widgets = this->focusable_widgets(source);
if (!focusable_widgets.is_empty())
set_focused_widget(focusable_widgets[0], source);
set_focused_widget(&focusable_widgets[0], source);
}
void Window::did_disable_focused_widget(Badge<Widget>)

View file

@ -172,7 +172,7 @@ public:
void apply_icon();
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
Vector<Widget*> focusable_widgets(FocusSource) const;
Vector<Widget&> focusable_widgets(FocusSource) const;
void schedule_relayout();

View file

@ -228,7 +228,7 @@ ByteBuffer TLSv12::build_certificate()
{
PacketBuilder builder { MessageType::Handshake, m_context.options.version };
Vector<const Certificate*> certificates;
Vector<Certificate const&> certificates;
Vector<Certificate>* local_certificates = nullptr;
if (m_context.is_server) {
@ -250,7 +250,7 @@ ByteBuffer TLSv12::build_certificate()
// FIXME: Check for and respond with only the requested certificate types.
if (true) {
certificates.append(&certificate);
certificates.append(certificate);
}
}
}
@ -266,9 +266,9 @@ ByteBuffer TLSv12::build_certificate()
builder.append_u24(total_certificate_size);
for (auto& certificate : certificates) {
if (!certificate->der.is_empty()) {
builder.append_u24(certificate->der.size());
builder.append(certificate->der.bytes());
if (!certificate.der.is_empty()) {
builder.append_u24(certificate.der.size());
builder.append(certificate.der.bytes());
}
}
}

View file

@ -227,34 +227,34 @@ void MenuManager::close_everyone()
void MenuManager::close_everyone_not_in_lineage(Menu& menu)
{
Vector<Menu*> menus_to_close;
Vector<Menu&> menus_to_close;
for (auto& open_menu : m_open_menu_stack) {
if (!open_menu)
continue;
if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
continue;
menus_to_close.append(open_menu);
menus_to_close.append(*open_menu);
}
close_menus(menus_to_close);
}
void MenuManager::close_menus(const Vector<Menu*>& menus)
void MenuManager::close_menus(const Vector<Menu&>& menus)
{
for (auto& menu : menus) {
if (menu == m_current_menu)
if (&menu == m_current_menu)
clear_current_menu();
menu->set_visible(false);
menu->clear_hovered_item();
menu.set_visible(false);
menu.clear_hovered_item();
m_open_menu_stack.remove_first_matching([&](auto& entry) {
return entry == menu;
return entry == &menu;
});
}
refresh();
}
static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
static void collect_menu_subtree(Menu& menu, Vector<Menu&>& menus)
{
menus.append(&menu);
menus.append(menu);
for (size_t i = 0; i < menu.item_count(); ++i) {
auto& item = menu.item(i);
if (!item.is_submenu())
@ -265,7 +265,7 @@ static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
void MenuManager::close_menu_and_descendants(Menu& menu)
{
Vector<Menu*> menus_to_close;
Vector<Menu&> menus_to_close;
collect_menu_subtree(menu, menus_to_close);
close_menus(menus_to_close);
}

View file

@ -50,7 +50,7 @@ public:
Menu* hovered_menu() { return m_hovered_menu; }
private:
void close_menus(const Vector<Menu*>&);
void close_menus(const Vector<Menu&>&);
virtual void event(Core::Event&) override;
void handle_mouse_event(MouseEvent&);

View file

@ -21,7 +21,7 @@ Menubar::~Menubar()
void Menubar::add_menu(Menu& menu)
{
m_menus.append(&menu);
m_menus.append(menu);
}
}

View file

@ -29,7 +29,7 @@ public:
void for_each_menu(Callback callback)
{
for (auto& menu : m_menus) {
if (callback(*menu) == IterationDecision::Break)
if (callback(menu) == IterationDecision::Break)
return;
}
}
@ -39,7 +39,7 @@ private:
ClientConnection& m_client;
int m_menubar_id { 0 };
Vector<Menu*> m_menus;
Vector<Menu&> m_menus;
};
}

View file

@ -200,19 +200,19 @@ public:
{
auto* blocking_modal_window = window.blocking_modal_window();
if (blocking_modal_window || window.is_modal()) {
Vector<Window*> modal_stack;
Vector<Window&> modal_stack;
auto* modal_stack_top = blocking_modal_window ? blocking_modal_window : &window;
for (auto* parent = modal_stack_top->parent_window(); parent; parent = parent->parent_window()) {
auto* blocked_by = parent->blocking_modal_window();
if (!blocked_by || (blocked_by != modal_stack_top && !modal_stack_top->is_descendant_of(*blocked_by)))
break;
modal_stack.append(parent);
modal_stack.append(*parent);
if (!parent->is_modal())
break;
}
if (!modal_stack.is_empty()) {
for (size_t i = modal_stack.size(); i > 0; i--) {
IterationDecision decision = f(*modal_stack[i - 1], false);
IterationDecision decision = f(modal_stack[i - 1], false);
if (decision != IterationDecision::Continue)
return decision;
}