CObject: Put all CObjects on a global IntrusiveList for introspection

This will allow us to easily traverse the entire CObject graph.
This commit is contained in:
Andreas Kling 2019-08-17 11:26:19 +02:00
parent e3f3c980bf
commit 2fa2d72761
Notes: sideshowbarker 2024-07-19 12:39:44 +09:00
2 changed files with 14 additions and 1 deletions

View file

@ -5,16 +5,24 @@
#include <LibCore/CObject.h>
#include <stdio.h>
IntrusiveList<CObject, &CObject::m_all_objects_list_node>& CObject::all_objects()
{
static IntrusiveList<CObject, &CObject::m_all_objects_list_node> objects;
return objects;
}
CObject::CObject(CObject* parent, bool is_widget)
: m_parent(parent)
, m_widget(is_widget)
{
all_objects().append(*this);
if (m_parent)
m_parent->add_child(*this);
}
CObject::~CObject()
{
all_objects().remove(*this);
stop_timer();
if (m_parent)
m_parent->remove_child(*this);

View file

@ -2,6 +2,7 @@
#include <AK/AKString.h>
#include <AK/Function.h>
#include <AK/IntrusiveList.h>
#include <AK/StdLibExtras.h>
#include <AK/Vector.h>
#include <AK/Weakable.h>
@ -18,6 +19,8 @@ public: \
class CObject : public Weakable<CObject> {
// NOTE: No C_OBJECT macro for CObject itself.
public:
IntrusiveListNode m_all_objects_list_node;
virtual ~CObject();
virtual const char* class_name() const = 0;
@ -60,8 +63,10 @@ public:
bool is_widget() const { return m_widget; }
virtual bool is_window() const { return false; }
static IntrusiveList<CObject, &CObject::m_all_objects_list_node>& all_objects();
protected:
CObject(CObject* parent = nullptr, bool is_widget = false);
explicit CObject(CObject* parent = nullptr, bool is_widget = false);
virtual void timer_event(CTimerEvent&);
virtual void custom_event(CCustomEvent&);