From 0e9e5930786dec58b7f53cae36b70197bf98f7c3 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 21 Nov 2024 19:14:06 -0800 Subject: [PATCH] dbus/properties: allow removing to/from wire transforms Useful when properties are only read/written in one direction. --- src/dbus/properties.hpp | 31 +++++++++++++++++++++++-------- src/services/upower/device.cpp | 10 ---------- src/services/upower/device.hpp | 3 --- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/dbus/properties.hpp b/src/dbus/properties.hpp index 9fe5a873..726668f6 100644 --- a/src/dbus/properties.hpp +++ b/src/dbus/properties.hpp @@ -143,9 +143,6 @@ template struct DBusDataTransform { using Wire = T; using Data = T; - - static DBusResult fromWire(Wire&& wire) { return DBusResult(std::move(wire)); } - static Wire toWire(const Data& value) { return value; } }; namespace bindable_p { @@ -166,6 +163,20 @@ struct BindableType { using Type = Meta::Type; }; +template +struct HasFromWire: std::false_type {}; + +template +struct HasFromWire()))>> + : std::true_type {}; + +template +struct HasToWire: std::false_type {}; + +template +struct HasToWire()))>> + : std::true_type {}; + } // namespace bindable_p template < @@ -206,12 +217,14 @@ protected: QDBusError store(const QVariant& variant) override { DBusResult result; - if constexpr (std::is_same_v) { - result = demarshallVariant(variant); - } else { + if constexpr (bindable_p::HasFromWire::value) { auto wireResult = demarshallVariant(variant); if (!wireResult.isValid()) return wireResult.error; result = Transform::fromWire(std::move(wireResult.value)); + } else if constexpr (std::is_same_v) { + result = demarshallVariant(variant); + } else { + return QDBusError(QDBusError::NotSupported, "This property cannot be deserialized"); } if (!result.isValid()) return result.error; @@ -225,10 +238,12 @@ protected: } QVariant serialize() override { - if constexpr (std::is_same_v) { + if constexpr (bindable_p::HasToWire::value) { + return QVariant::fromValue(Transform::toWire(this->bindable()->value())); + } else if constexpr (std::is_same_v) { return QVariant::fromValue(this->bindable()->value()); } else { - return QVariant::fromValue(Transform::toWire(this->bindable()->value())); + return QVariant(); } } diff --git a/src/services/upower/device.cpp b/src/services/upower/device.cpp index cd97fbbb..a31707d9 100644 --- a/src/services/upower/device.cpp +++ b/src/services/upower/device.cpp @@ -105,8 +105,6 @@ DBusResult DBusDataTransform::fromWire(qreal wire) { return DBusResult(wire * 0.01); } -qreal DBusDataTransform::toWire(const qreal& value) { return value * 100; } - DBusResult DBusDataTransform::fromWire(quint32 wire) { if (wire != UPowerDeviceType::Battery && wire >= UPowerDeviceState::Unknown @@ -120,10 +118,6 @@ DBusDataTransform::fromWire(quint32 wire) { ); } -quint32 DBusDataTransform::toWire(const UPowerDeviceState::Enum& value) { - return static_cast(value); -} - DBusResult DBusDataTransform::fromWire(quint32 wire ) { if (wire >= UPowerDeviceType::Unknown && wire <= UPowerDeviceType::BluetoothGeneric) { @@ -135,8 +129,4 @@ DBusResult DBusDataTransform::fr ); } -quint32 DBusDataTransform::toWire(const UPowerDeviceType::Enum& value) { - return static_cast(value); -} - } // namespace qs::dbus diff --git a/src/services/upower/device.hpp b/src/services/upower/device.hpp index 8610bbcf..2f37ea01 100644 --- a/src/services/upower/device.hpp +++ b/src/services/upower/device.hpp @@ -91,7 +91,6 @@ struct DBusDataTransform { using Wire = quint32; using Data = qs::service::upower::UPowerDeviceState::Enum; static DBusResult fromWire(Wire wire); - static Wire toWire(const Data& value); }; template <> @@ -99,7 +98,6 @@ struct DBusDataTransform { using Wire = quint32; using Data = qs::service::upower::UPowerDeviceType::Enum; static DBusResult fromWire(Wire wire); - static Wire toWire(const Data& value); }; template <> @@ -107,7 +105,6 @@ struct DBusDataTransform { using Wire = qreal; using Data = qreal; static DBusResult fromWire(Wire wire); - static Wire toWire(const Data& value); }; } // namespace qs::dbus