summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/Object.cpp
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2021-04-04 15:40:34 -0600
committerAndreas Kling <kling@serenityos.org>2021-05-06 08:50:39 +0200
commit3aaffa2c47e39583848cfe9b7b9f846e5119fe1b (patch)
treeeb5a5535bec0952a95b19b9c494d9c6049896107 /Userland/Libraries/LibCore/Object.cpp
parent6e101adc28c9c682f023386831d632a477768cb8 (diff)
downloadserenity-3aaffa2c47e39583848cfe9b7b9f846e5119fe1b.zip
LibGUI: Move widget registration to LibCore
This also moves Widget::load_from_json into Core::Object as a virtual function in order to allow loading non-widget objects in GML (e.g. BoxLayout). Co-authored-by: Gunnar Beutner <gbeutner@serenityos.org>
Diffstat (limited to 'Userland/Libraries/LibCore/Object.cpp')
-rw-r--r--Userland/Libraries/LibCore/Object.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Object.cpp b/Userland/Libraries/LibCore/Object.cpp
index fffeeb955f..4c316406fd 100644
--- a/Userland/Libraries/LibCore/Object.cpp
+++ b/Userland/Libraries/LibCore/Object.cpp
@@ -250,4 +250,44 @@ void Object::set_event_filter(Function<bool(Core::Event&)> filter)
m_event_filter = move(filter);
}
+static HashMap<String, ObjectClassRegistration*>& object_classes()
+{
+ static HashMap<String, ObjectClassRegistration*>* map;
+ if (!map)
+ map = new HashMap<String, ObjectClassRegistration*>;
+ return *map;
+}
+
+ObjectClassRegistration::ObjectClassRegistration(const String& class_name, Function<NonnullRefPtr<Object>()> factory, ObjectClassRegistration* parent_class)
+ : m_class_name(class_name)
+ , m_factory(move(factory))
+ , m_parent_class(parent_class)
+{
+ object_classes().set(class_name, this);
+}
+
+ObjectClassRegistration::~ObjectClassRegistration()
+{
+}
+
+bool ObjectClassRegistration::is_derived_from(const ObjectClassRegistration& base_class) const
+{
+ if (&base_class == this)
+ return true;
+ if (!m_parent_class)
+ return false;
+ return m_parent_class->is_derived_from(base_class);
+}
+
+void ObjectClassRegistration::for_each(Function<void(const ObjectClassRegistration&)> callback)
+{
+ for (auto& it : object_classes()) {
+ callback(*it.value);
+ }
+}
+
+const ObjectClassRegistration* ObjectClassRegistration::find(const String& class_name)
+{
+ return object_classes().get(class_name).value_or(nullptr);
+}
}