mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:38:10 +00:00
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.
This commit is contained in:
parent
66bf10acef
commit
d9e459293b
1 changed files with 8 additions and 0 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue