From c2213449c04792a496510b75268305720e692e89 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 18 Aug 2019 20:36:37 +0200 Subject: [PATCH] LibCore: Move CObject serialization into CObject::save_to(JsonObject&) The idea is for subclasses to override this and add whatever properties are relevant for them, then call up to their base class, etc. --- Libraries/LibCore/CEventLoop.cpp | 5 +---- Libraries/LibCore/CObject.cpp | 9 +++++++++ Libraries/LibCore/CObject.h | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Libraries/LibCore/CEventLoop.cpp b/Libraries/LibCore/CEventLoop.cpp index 26adf6fd77..ac4b7c6ae0 100644 --- a/Libraries/LibCore/CEventLoop.cpp +++ b/Libraries/LibCore/CEventLoop.cpp @@ -61,10 +61,7 @@ CEventLoop::CEventLoop() JsonArray objects; for (auto& object : CObject::all_objects()) { JsonObject json_object; - json_object.set("class_name", object.class_name()); - json_object.set("address", String::format("%p", &object)); - json_object.set("name", object.name()); - json_object.set("parent", String::format("%p", object.parent())); + object.save_to(json_object); objects.append(move(json_object)); } client->write(objects.to_string()); diff --git a/Libraries/LibCore/CObject.cpp b/Libraries/LibCore/CObject.cpp index 28928ac9ba..2d9d4f505b 100644 --- a/Libraries/LibCore/CObject.cpp +++ b/Libraries/LibCore/CObject.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -125,3 +126,11 @@ void CObject::deferred_invoke(Function invokee) { CEventLoop::current().post_event(*this, make(move(invokee))); } + +void CObject::save_to(JsonObject& json) +{ + json.set("class_name", class_name()); + json.set("address", String::format("%p", this)); + json.set("name", name()); + json.set("parent", String::format("%p", parent())); +} diff --git a/Libraries/LibCore/CObject.h b/Libraries/LibCore/CObject.h index c8c6f743c9..f35403d1ef 100644 --- a/Libraries/LibCore/CObject.h +++ b/Libraries/LibCore/CObject.h @@ -7,6 +7,10 @@ #include #include +namespace AK { +class JsonObject; +} + class CEvent; class CChildEvent; class CCustomEvent; @@ -63,6 +67,8 @@ public: bool is_widget() const { return m_widget; } virtual bool is_window() const { return false; } + virtual void save_to(AK::JsonObject&); + static IntrusiveList& all_objects(); protected: