Skip to content

Commit 0e9e593

Browse files
committed
dbus/properties: allow removing to/from wire transforms
Useful when properties are only read/written in one direction.
1 parent ac50767 commit 0e9e593

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/dbus/properties.hpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ template <typename T>
143143
struct DBusDataTransform {
144144
using Wire = T;
145145
using Data = T;
146-
147-
static DBusResult<Data> fromWire(Wire&& wire) { return DBusResult<T>(std::move(wire)); }
148-
static Wire toWire(const Data& value) { return value; }
149146
};
150147

151148
namespace bindable_p {
@@ -166,6 +163,20 @@ struct BindableType {
166163
using Type = Meta::Type;
167164
};
168165

166+
template <typename T, typename = void>
167+
struct HasFromWire: std::false_type {};
168+
169+
template <typename T>
170+
struct HasFromWire<T, std::void_t<decltype(T::fromWire(std::declval<typename T::Wire>()))>>
171+
: std::true_type {};
172+
173+
template <typename T, typename = void>
174+
struct HasToWire: std::false_type {};
175+
176+
template <typename T>
177+
struct HasToWire<T, std::void_t<decltype(T::toWire(std::declval<typename T::Data>()))>>
178+
: std::true_type {};
179+
169180
} // namespace bindable_p
170181

171182
template <
@@ -206,12 +217,14 @@ class DBusBindableProperty: public DBusPropertyCore {
206217
QDBusError store(const QVariant& variant) override {
207218
DBusResult<DataType> result;
208219

209-
if constexpr (std::is_same_v<WireType, BaseType>) {
210-
result = demarshallVariant<DataType>(variant);
211-
} else {
220+
if constexpr (bindable_p::HasFromWire<Transform>::value) {
212221
auto wireResult = demarshallVariant<WireType>(variant);
213222
if (!wireResult.isValid()) return wireResult.error;
214223
result = Transform::fromWire(std::move(wireResult.value));
224+
} else if constexpr (std::is_same_v<WireType, BaseType>) {
225+
result = demarshallVariant<DataType>(variant);
226+
} else {
227+
return QDBusError(QDBusError::NotSupported, "This property cannot be deserialized");
215228
}
216229

217230
if (!result.isValid()) return result.error;
@@ -225,10 +238,12 @@ class DBusBindableProperty: public DBusPropertyCore {
225238
}
226239

227240
QVariant serialize() override {
228-
if constexpr (std::is_same_v<WireType, BaseType>) {
241+
if constexpr (bindable_p::HasToWire<Transform>::value) {
242+
return QVariant::fromValue(Transform::toWire(this->bindable()->value()));
243+
} else if constexpr (std::is_same_v<WireType, BaseType>) {
229244
return QVariant::fromValue(this->bindable()->value());
230245
} else {
231-
return QVariant::fromValue(Transform::toWire(this->bindable()->value()));
246+
return QVariant();
232247
}
233248
}
234249

src/services/upower/device.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ DBusResult<qreal> DBusDataTransform<PowerPercentage>::fromWire(qreal wire) {
105105
return DBusResult(wire * 0.01);
106106
}
107107

108-
qreal DBusDataTransform<PowerPercentage>::toWire(const qreal& value) { return value * 100; }
109-
110108
DBusResult<UPowerDeviceState::Enum>
111109
DBusDataTransform<UPowerDeviceState::Enum>::fromWire(quint32 wire) {
112110
if (wire != UPowerDeviceType::Battery && wire >= UPowerDeviceState::Unknown
@@ -120,10 +118,6 @@ DBusDataTransform<UPowerDeviceState::Enum>::fromWire(quint32 wire) {
120118
);
121119
}
122120

123-
quint32 DBusDataTransform<UPowerDeviceState::Enum>::toWire(const UPowerDeviceState::Enum& value) {
124-
return static_cast<quint32>(value);
125-
}
126-
127121
DBusResult<UPowerDeviceType::Enum> DBusDataTransform<UPowerDeviceType::Enum>::fromWire(quint32 wire
128122
) {
129123
if (wire >= UPowerDeviceType::Unknown && wire <= UPowerDeviceType::BluetoothGeneric) {
@@ -135,8 +129,4 @@ DBusResult<UPowerDeviceType::Enum> DBusDataTransform<UPowerDeviceType::Enum>::fr
135129
);
136130
}
137131

138-
quint32 DBusDataTransform<UPowerDeviceType::Enum>::toWire(const UPowerDeviceType::Enum& value) {
139-
return static_cast<quint32>(value);
140-
}
141-
142132
} // namespace qs::dbus

src/services/upower/device.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,20 @@ struct DBusDataTransform<qs::service::upower::UPowerDeviceState::Enum> {
9191
using Wire = quint32;
9292
using Data = qs::service::upower::UPowerDeviceState::Enum;
9393
static DBusResult<Data> fromWire(Wire wire);
94-
static Wire toWire(const Data& value);
9594
};
9695

9796
template <>
9897
struct DBusDataTransform<qs::service::upower::UPowerDeviceType::Enum> {
9998
using Wire = quint32;
10099
using Data = qs::service::upower::UPowerDeviceType::Enum;
101100
static DBusResult<Data> fromWire(Wire wire);
102-
static Wire toWire(const Data& value);
103101
};
104102

105103
template <>
106104
struct DBusDataTransform<qs::service::upower::PowerPercentage> {
107105
using Wire = qreal;
108106
using Data = qreal;
109107
static DBusResult<Data> fromWire(Wire wire);
110-
static Wire toWire(const Data& value);
111108
};
112109

113110
} // namespace qs::dbus

0 commit comments

Comments
 (0)