1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:07:44 +00:00

LibCore: Add Core::Object::try_add<T>(...)

This is a fallible version of add<T>(...) that returns ErrorOr<T>.
It can be used together with TRY() to handle allocation failures when
instantiating new Core::Objects.
This commit is contained in:
Andreas Kling 2021-11-24 13:09:51 +01:00
parent 2efec90fb7
commit b81ce827b6
2 changed files with 18 additions and 2 deletions

View file

@ -73,14 +73,20 @@ void Object::event(Core::Event& event)
}
}
void Object::add_child(Object& object)
ErrorOr<void> Object::try_add_child(Object& object)
{
// FIXME: Should we support reparenting objects?
VERIFY(!object.parent() || object.parent() == this);
TRY(m_children.try_append(object));
object.m_parent = this;
m_children.append(object);
Core::ChildEvent child_event(Core::Event::ChildAdded, object);
event(child_event);
return {};
}
void Object::add_child(Object& object)
{
MUST(try_add_child(object));
}
void Object::insert_child_before(Object& new_child, Object& before_child)