mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:47:34 +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:
parent
2efec90fb7
commit
b81ce827b6
2 changed files with 18 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -129,6 +129,8 @@ public:
|
|||
void stop_timer();
|
||||
bool has_timer() const { return m_timer_id; }
|
||||
|
||||
ErrorOr<void> try_add_child(Object&);
|
||||
|
||||
void add_child(Object&);
|
||||
void insert_child_before(Object& new_child, Object& before_child);
|
||||
void remove_child(Object&);
|
||||
|
@ -164,6 +166,14 @@ public:
|
|||
return child;
|
||||
}
|
||||
|
||||
template<class T, class... Args>
|
||||
inline ErrorOr<NonnullRefPtr<T>> try_add(Args&&... args)
|
||||
{
|
||||
auto child = TRY(T::try_create(forward<Args>(args)...));
|
||||
TRY(try_add_child(*child));
|
||||
return child;
|
||||
}
|
||||
|
||||
virtual bool is_visible_for_timer_purposes() const;
|
||||
|
||||
bool is_being_inspected() const { return m_inspector_count; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue