From d9e459293b58ce1c79e7c4b89dd927546a691880 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 23 Feb 2020 07:13:09 +0100 Subject: [PATCH] LibCore: Add Core::Object::add helper for creating child objects Consider the old pattern for creating a Core::Object parent and child: auto parent = Core::Object::construct(...); auto child = Core::Object::construct(..., parent); The above was an artifact of the pre-reference-counting Object era. Now that objects have less esoteric lifetime management, we can replace the old pattern with something more expressive: auto parent = Core::Object::construct(...); auto child = parent->add(...); This reads a lot more naturally, and it also means we can get rid of all the parent pointer arguments to Core::Object subclass constructors. --- Libraries/LibCore/Object.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Libraries/LibCore/Object.h b/Libraries/LibCore/Object.h index b76b7ff7cd..d007818b0e 100644 --- a/Libraries/LibCore/Object.h +++ b/Libraries/LibCore/Object.h @@ -120,6 +120,14 @@ public: m_parent->remove_child(*this); } + template + inline NonnullRefPtr add(Args&&... args) + { + auto t = T::construct(forward(args)...); + add_child(*t); + return t; + } + virtual bool is_visible_for_timer_purposes() const; protected: