ladybird/Widgets/Object.cpp

47 lines
883 B
C++
Raw Normal View History

2018-10-10 16:12:38 +03:00
#include "Object.h"
#include "Event.h"
#include <AK/Assertions.h>
Object::Object(Object* parent)
: m_parent(parent)
{
2018-10-10 17:49:36 +03:00
if (m_parent)
m_parent->addChild(*this);
2018-10-10 16:12:38 +03:00
}
Object::~Object()
{
2018-10-10 17:49:36 +03:00
if (m_parent)
m_parent->removeChild(*this);
auto childrenToDelete = std::move(m_children);
for (auto* child : childrenToDelete)
2018-10-10 17:49:36 +03:00
delete child;
2018-10-10 16:12:38 +03:00
}
void Object::event(Event& event)
{
switch (event.type()) {
case Event::Invalid:
ASSERT_NOT_REACHED();
break;
default:
break;
}
}
2018-10-10 17:49:36 +03:00
void Object::addChild(Object& object)
{
m_children.append(&object);
}
void Object::removeChild(Object& object)
{
// Oh geez, Vector needs a remove() huh...
Vector<Object*> newList;
for (auto* child : m_children) {
if (child != &object)
newList.append(child);
}
m_children = std::move(newList);
}