1
Fork 0
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:
Andreas Kling 2020-02-23 07:13:09 +01:00
parent 66bf10acef
commit d9e459293b

View file

@ -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: