diff options
author | Andreas Kling <kling@serenityos.org> | 2020-02-23 07:13:09 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-02-23 11:10:52 +0100 |
commit | d9e459293b58ce1c79e7c4b89dd927546a691880 (patch) | |
tree | cd4b05aa3a687b838e191b3e672330d3f3b9da0f /Libraries | |
parent | 66bf10acef59d5b082298377caabf8a8d35dda79 (diff) | |
download | serenity-d9e459293b58ce1c79e7c4b89dd927546a691880.zip |
LibCore: Add Core::Object::add<T> 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<Core::Object>(...);
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibCore/Object.h | 8 |
1 files changed, 8 insertions, 0 deletions
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<class T, class... Args> + inline NonnullRefPtr<T> add(Args&&... args) + { + auto t = T::construct(forward<Args>(args)...); + add_child(*t); + return t; + } + virtual bool is_visible_for_timer_purposes() const; protected: |