2019-01-20 04:49:48 +01:00
|
|
|
#include "GObject.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GEventLoop.h"
|
2018-10-10 15:12:38 +02:00
|
|
|
#include <AK/Assertions.h>
|
2019-03-19 00:01:02 +01:00
|
|
|
#include <stdio.h>
|
2018-10-10 15:12:38 +02:00
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
GObject::GObject(GObject* parent)
|
2018-10-10 15:12:38 +02:00
|
|
|
: m_parent(parent)
|
|
|
|
{
|
2018-10-10 16:49:36 +02:00
|
|
|
if (m_parent)
|
2019-01-31 17:31:23 +01:00
|
|
|
m_parent->add_child(*this);
|
2018-10-10 15:12:38 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
GObject::~GObject()
|
2018-10-10 15:12:38 +02:00
|
|
|
{
|
2019-02-05 10:31:37 +01:00
|
|
|
stop_timer();
|
2018-10-10 16:49:36 +02:00
|
|
|
if (m_parent)
|
2019-01-31 17:31:23 +01:00
|
|
|
m_parent->remove_child(*this);
|
|
|
|
auto children_to_delete = move(m_children);
|
|
|
|
for (auto* child : children_to_delete)
|
2018-10-10 16:49:36 +02:00
|
|
|
delete child;
|
2018-10-10 15:12:38 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
void GObject::event(GEvent& event)
|
2018-10-10 15:12:38 +02:00
|
|
|
{
|
|
|
|
switch (event.type()) {
|
2019-01-20 04:49:48 +01:00
|
|
|
case GEvent::Timer:
|
2019-01-31 16:37:43 +01:00
|
|
|
return timer_event(static_cast<GTimerEvent&>(event));
|
2019-01-20 04:49:48 +01:00
|
|
|
case GEvent::DeferredDestroy:
|
2018-10-14 01:23:01 +02:00
|
|
|
delete this;
|
|
|
|
break;
|
2019-03-15 16:12:06 +01:00
|
|
|
case GEvent::ChildAdded:
|
|
|
|
case GEvent::ChildRemoved:
|
|
|
|
return child_event(static_cast<GChildEvent&>(event));
|
2019-01-20 04:49:48 +01:00
|
|
|
case GEvent::Invalid:
|
2018-10-10 15:12:38 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:49:36 +02:00
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
void GObject::add_child(GObject& object)
|
2018-10-10 16:49:36 +02:00
|
|
|
{
|
|
|
|
m_children.append(&object);
|
2019-03-19 00:01:02 +01:00
|
|
|
GEventLoop::current().post_event(*this, make<GChildEvent>(GEvent::ChildAdded, object));
|
2018-10-10 16:49:36 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
void GObject::remove_child(GObject& object)
|
2018-10-10 16:49:36 +02:00
|
|
|
{
|
2019-02-25 22:06:55 +01:00
|
|
|
for (ssize_t i = 0; i < m_children.size(); ++i) {
|
2018-10-13 01:19:25 +02:00
|
|
|
if (m_children[i] == &object) {
|
|
|
|
m_children.remove(i);
|
2019-03-19 00:01:02 +01:00
|
|
|
GEventLoop::current().post_event(*this, make<GChildEvent>(GEvent::ChildRemoved, object));
|
2018-10-13 01:19:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-10 16:49:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 12:18:59 +02:00
|
|
|
|
2019-01-31 16:37:43 +01:00
|
|
|
void GObject::timer_event(GTimerEvent&)
|
2018-10-12 12:18:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:12:06 +01:00
|
|
|
void GObject::child_event(GChildEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
void GObject::start_timer(int ms)
|
2018-10-12 12:18:59 +02:00
|
|
|
{
|
2019-01-31 17:31:23 +01:00
|
|
|
if (m_timer_id) {
|
2019-01-20 04:49:48 +01:00
|
|
|
dbgprintf("GObject{%p} already has a timer!\n", this);
|
2018-10-12 12:18:59 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-02-01 03:50:06 +01:00
|
|
|
|
2019-03-19 00:01:02 +01:00
|
|
|
m_timer_id = GEventLoop::register_timer(*this, ms, true);
|
2018-10-12 12:18:59 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
void GObject::stop_timer()
|
2018-10-12 12:18:59 +02:00
|
|
|
{
|
2019-01-31 17:31:23 +01:00
|
|
|
if (!m_timer_id)
|
2018-10-12 12:18:59 +02:00
|
|
|
return;
|
2019-03-19 00:01:02 +01:00
|
|
|
bool success = GEventLoop::unregister_timer(m_timer_id);
|
2019-02-01 03:50:06 +01:00
|
|
|
ASSERT(success);
|
2019-01-31 17:31:23 +01:00
|
|
|
m_timer_id = 0;
|
2018-10-12 12:18:59 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:03:52 +01:00
|
|
|
void GObject::delete_later()
|
2018-10-14 01:23:01 +02:00
|
|
|
{
|
2019-03-19 00:01:02 +01:00
|
|
|
GEventLoop::current().post_event(*this, make<GEvent>(GEvent::DeferredDestroy));
|
2018-10-14 01:23:01 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 00:01:02 +01:00
|
|
|
void GObject::dump_tree(int indent)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < indent; ++i) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("%s{%p}\n", class_name(), this);
|
|
|
|
|
|
|
|
for (auto* child : children()) {
|
|
|
|
child->dump_tree(indent + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|