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

LibCore: Make CObject reference-counted

Okay, I've spent a whole day on this now, and it finally kinda works!
With this patch, CObject and all of its derived classes are reference
counted instead of tree-owned.

The previous, Qt-like model was nice and familiar, but ultimately also
outdated and difficult to reason about.

CObject-derived types should now be stored in RefPtr/NonnullRefPtr and
each class can be constructed using the forwarding construct() helper:

    auto widget = GWidget::construct(parent_widget);

Note that construct() simply forwards all arguments to an existing
constructor. It is inserted into each class by the C_OBJECT macro,
see CObject.h to understand how that works.

CObject::delete_later() disappears in this patch, as there is no longer
a single logical owner of a CObject.
This commit is contained in:
Andreas Kling 2019-09-22 00:17:53 +02:00
parent 0c72e0c09f
commit bc319d9e88
45 changed files with 174 additions and 233 deletions

View file

@ -18,7 +18,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C
widget->set_fill_with_background_color(true);
widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
auto left_container = GWidget::construct(widget);
auto left_container = GWidget::construct(widget.ptr());
left_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
left_container->set_preferred_size(48, 0);
left_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
@ -28,7 +28,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C
icon_label->set_preferred_size(40, 40);
left_container->layout()->add_spacer();
auto right_container = GWidget::construct(widget);
auto right_container = GWidget::construct(widget.ptr());
right_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
right_container->layout()->set_margins({ 0, 4, 4, 4 });
@ -46,7 +46,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C
right_container->layout()->add_spacer();
auto button_container = GWidget::construct(right_container);
auto button_container = GWidget::construct(right_container.ptr());
button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
button_container->set_preferred_size(0, 20);
button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));

View file

@ -110,7 +110,6 @@ void GApplication::hide_tooltip()
{
if (m_tooltip_window) {
m_tooltip_window->hide();
delete m_tooltip_window;
m_tooltip_window = nullptr;
}
}

View file

@ -57,11 +57,11 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie
horizontal_container->set_fill_with_background_color(true);
horizontal_container->set_background_color(Color::WarmGray);
auto vertical_container = GWidget::construct(horizontal_container);
auto vertical_container = GWidget::construct(horizontal_container.ptr());
vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
vertical_container->layout()->set_spacing(4);
auto upper_container = GWidget::construct(vertical_container);
auto upper_container = GWidget::construct(vertical_container.ptr());
upper_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
upper_container->layout()->set_spacing(4);
upper_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
@ -118,13 +118,13 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie
});
toolbar->add_action(*mkdir_action);
auto lower_container = GWidget::construct(vertical_container);
auto lower_container = GWidget::construct(vertical_container.ptr());
lower_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
lower_container->layout()->set_spacing(4);
lower_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
lower_container->set_preferred_size(0, 60);
auto filename_container = GWidget::construct(lower_container);
auto filename_container = GWidget::construct(lower_container.ptr());
filename_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
filename_container->set_preferred_size(0, 20);
filename_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
@ -132,7 +132,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie
filename_label->set_text_alignment(TextAlignment::CenterLeft);
filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
filename_label->set_preferred_size(60, 0);
m_filename_textbox = GTextBox::construct(filename_container);
m_filename_textbox = GTextBox::construct(filename_container.ptr());
if (m_mode == Mode::Save) {
m_filename_textbox->set_text(file_name);
m_filename_textbox->set_focus(true);
@ -155,7 +155,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie
set_preview(path);
};
auto button_container = GWidget::construct(lower_container);
auto button_container = GWidget::construct(lower_container.ptr());
button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
button_container->set_preferred_size(0, 20);
button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));

View file

@ -42,12 +42,12 @@ void GInputBox::build()
m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
m_text_editor->set_preferred_size(0, 19);
auto button_container_outer = GWidget::construct(widget);
auto button_container_outer = GWidget::construct(widget.ptr());
button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
button_container_outer->set_preferred_size(0, 20);
button_container_outer->set_layout(make<GBoxLayout>(Orientation::Vertical));
auto button_container_inner = GWidget::construct(button_container_outer);
auto button_container_inner = GWidget::construct(button_container_outer.ptr());
button_container_inner->set_layout(make<GBoxLayout>(Orientation::Horizontal));
button_container_inner->layout()->set_spacing(8);

View file

@ -64,7 +64,7 @@ void GMessageBox::build()
ObjectPtr<GWidget> message_container = widget;
if (m_type != Type::None) {
message_container = GWidget::construct(widget);
message_container = GWidget::construct(widget.ptr());
message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
message_container->layout()->set_margins({ 8, 0, 8, 0 });
message_container->layout()->set_spacing(8);
@ -80,7 +80,7 @@ void GMessageBox::build()
label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
label->set_preferred_size(text_width, 16);
auto button_container = GWidget::construct(widget);
auto button_container = GWidget::construct(widget.ptr());
button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
button_container->layout()->set_spacing(5);
button_container->layout()->set_margins({ 15, 0, 15, 0 });

View file

@ -325,13 +325,13 @@ Rect GWidget::screen_relative_rect() const
GWidget* GWidget::child_at(const Point& point) const
{
for (int i = children().size() - 1; i >= 0; --i) {
if (!is<GWidget>(*children()[i]))
if (!is<GWidget>(children()[i]))
continue;
auto& child = to<GWidget>(*children()[i]);
auto& child = to<GWidget>(children()[i]);
if (!child.is_visible())
continue;
if (child.relative_rect().contains(point))
return &child;
return const_cast<GWidget*>(&child);
}
return nullptr;
}
@ -492,7 +492,7 @@ void GWidget::move_to_front()
parent->children().remove_first_matching([this](auto& entry) {
return entry == this;
});
parent->children().append(this);
parent->children().append(*this);
parent->update();
}
@ -506,7 +506,7 @@ void GWidget::move_to_back()
parent->children().remove_first_matching([this](auto& entry) {
return entry == this;
});
parent->children().prepend(this);
parent->children().prepend(*this);
parent->update();
}
@ -515,7 +515,7 @@ bool GWidget::is_frontmost() const
auto* parent = parent_widget();
if (!parent)
return true;
return parent->children().last() == this;
return &parent->children().last() == this;
}
bool GWidget::is_backmost() const
@ -523,7 +523,7 @@ bool GWidget::is_backmost() const
auto* parent = parent_widget();
if (!parent)
return true;
return parent->children().first() == this;
return &parent->children().first() == this;
}
GAction* GWidget::action_for_key_event(const GKeyEvent& event)