summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-12-08 10:59:30 -0500
committerAndreas Kling <kling@serenityos.org>2022-12-09 00:05:30 +0100
commit746364d7c19cde27bf1a30b8dace8202eeb27389 (patch)
tree16aeb64ddf0c84b8ad68eb6fb951eeed569d6f3d
parent58f5deba70c0e9d21836fda4b1fee041fef69e16 (diff)
downloadserenity-746364d7c19cde27bf1a30b8dace8202eeb27389.zip
LibCore: Support write-only Object properties
Some properties are set and then never retrieved, e.g. GUI icon paths. Add a helper to create such properties, similar to the read-only helper.
-rw-r--r--Userland/Libraries/LibCore/Object.h9
-rw-r--r--Userland/Libraries/LibCore/Property.h7
2 files changed, 15 insertions, 1 deletions
diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h
index 050204af65..36eeae6784 100644
--- a/Userland/Libraries/LibCore/Object.h
+++ b/Userland/Libraries/LibCore/Object.h
@@ -311,6 +311,15 @@ requires IsBaseOf<Object, T>
[this] { return this->getter(); }, \
{});
+#define REGISTER_WRITE_ONLY_STRING_PROPERTY(property_name, setter) \
+ register_property( \
+ property_name, \
+ {}, \
+ [this](auto& value) { \
+ this->setter(value.to_deprecated_string()); \
+ return true; \
+ });
+
#define REGISTER_READONLY_SIZE_PROPERTY(property_name, getter) \
register_property( \
property_name, \
diff --git a/Userland/Libraries/LibCore/Property.h b/Userland/Libraries/LibCore/Property.h
index 6d1d0bdcf6..e34fde9418 100644
--- a/Userland/Libraries/LibCore/Property.h
+++ b/Userland/Libraries/LibCore/Property.h
@@ -26,7 +26,12 @@ public:
return m_setter(value);
}
- JsonValue get() const { return m_getter(); }
+ JsonValue get() const
+ {
+ if (!m_getter)
+ return {};
+ return m_getter();
+ }
DeprecatedString const& name() const { return m_name; }
bool is_readonly() const { return !m_setter; }