forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CObject.cpp
112 lines (98 loc) · 2.5 KB
/
CObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <AK/Assertions.h>
#include <LibCore/CEvent.h>
#include <LibCore/CEventLoop.h>
#include <LibCore/CObject.h>
#include <stdio.h>
CObject::CObject(CObject* parent, bool is_widget)
: m_parent(parent)
, m_widget(is_widget)
{
if (m_parent)
m_parent->add_child(*this);
}
CObject::~CObject()
{
stop_timer();
if (m_parent)
m_parent->remove_child(*this);
auto children_to_delete = move(m_children);
for (auto* child : children_to_delete)
delete child;
}
void CObject::event(CEvent& event)
{
switch (event.type()) {
case CEvent::Timer:
return timer_event(static_cast<CTimerEvent&>(event));
case CEvent::DeferredDestroy:
delete this;
break;
case CEvent::ChildAdded:
case CEvent::ChildRemoved:
return child_event(static_cast<CChildEvent&>(event));
case CEvent::Invalid:
ASSERT_NOT_REACHED();
break;
default:
break;
}
}
void CObject::add_child(CObject& object)
{
// FIXME: Should we support reparenting objects?
ASSERT(!object.parent() || object.parent() == this);
object.m_parent = this;
m_children.append(&object);
event(*make<CChildEvent>(CEvent::ChildAdded, object));
}
void CObject::remove_child(CObject& object)
{
for (ssize_t i = 0; i < m_children.size(); ++i) {
if (m_children[i] == &object) {
m_children.remove(i);
event(*make<CChildEvent>(CEvent::ChildRemoved, object));
return;
}
}
}
void CObject::timer_event(CTimerEvent&)
{
}
void CObject::child_event(CChildEvent&)
{
}
void CObject::start_timer(int ms)
{
if (m_timer_id) {
dbgprintf("CObject{%p} already has a timer!\n", this);
ASSERT_NOT_REACHED();
}
m_timer_id = CEventLoop::register_timer(*this, ms, true);
}
void CObject::stop_timer()
{
if (!m_timer_id)
return;
bool success = CEventLoop::unregister_timer(m_timer_id);
ASSERT(success);
m_timer_id = 0;
}
void CObject::delete_later()
{
CEventLoop::current().post_event(*this, make<CEvent>(CEvent::DeferredDestroy));
}
void CObject::dump_tree(int indent)
{
for (int i = 0; i < indent; ++i) {
printf(" ");
}
printf("%s{%p}\n", class_name(), this);
for_each_child([&](auto& child) {
child.dump_tree(indent + 2);
return IterationDecision::Continue;
});
}
void CObject::deferred_invoke(Function<void(CObject&)> invokee)
{
CEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
}