forked from quickshell/quickshell
		
	dbus/properties: allow removing to/from wire transforms
Useful when properties are only read/written in one direction.
This commit is contained in:
		
							parent
							
								
									ac50767873
								
							
						
					
					
						commit
						0e9e593078
					
				
					 3 changed files with 23 additions and 21 deletions
				
			
		| 
						 | 
				
			
			@ -143,9 +143,6 @@ template <typename T>
 | 
			
		|||
struct DBusDataTransform {
 | 
			
		||||
	using Wire = T;
 | 
			
		||||
	using Data = T;
 | 
			
		||||
 | 
			
		||||
	static DBusResult<Data> fromWire(Wire&& wire) { return DBusResult<T>(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 <typename T, typename = void>
 | 
			
		||||
struct HasFromWire: std::false_type {};
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
struct HasFromWire<T, std::void_t<decltype(T::fromWire(std::declval<typename T::Wire>()))>>
 | 
			
		||||
    : std::true_type {};
 | 
			
		||||
 | 
			
		||||
template <typename T, typename = void>
 | 
			
		||||
struct HasToWire: std::false_type {};
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
struct HasToWire<T, std::void_t<decltype(T::toWire(std::declval<typename T::Data>()))>>
 | 
			
		||||
    : std::true_type {};
 | 
			
		||||
 | 
			
		||||
} // namespace bindable_p
 | 
			
		||||
 | 
			
		||||
template <
 | 
			
		||||
| 
						 | 
				
			
			@ -206,12 +217,14 @@ protected:
 | 
			
		|||
	QDBusError store(const QVariant& variant) override {
 | 
			
		||||
		DBusResult<DataType> result;
 | 
			
		||||
 | 
			
		||||
		if constexpr (std::is_same_v<WireType, BaseType>) {
 | 
			
		||||
			result = demarshallVariant<DataType>(variant);
 | 
			
		||||
		} else {
 | 
			
		||||
		if constexpr (bindable_p::HasFromWire<Transform>::value) {
 | 
			
		||||
			auto wireResult = demarshallVariant<WireType>(variant);
 | 
			
		||||
			if (!wireResult.isValid()) return wireResult.error;
 | 
			
		||||
			result = Transform::fromWire(std::move(wireResult.value));
 | 
			
		||||
		} else if constexpr (std::is_same_v<WireType, BaseType>) {
 | 
			
		||||
			result = demarshallVariant<DataType>(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<WireType, BaseType>) {
 | 
			
		||||
		if constexpr (bindable_p::HasToWire<Transform>::value) {
 | 
			
		||||
			return QVariant::fromValue(Transform::toWire(this->bindable()->value()));
 | 
			
		||||
		} else if constexpr (std::is_same_v<WireType, BaseType>) {
 | 
			
		||||
			return QVariant::fromValue(this->bindable()->value());
 | 
			
		||||
		} else {
 | 
			
		||||
			return QVariant::fromValue(Transform::toWire(this->bindable()->value()));
 | 
			
		||||
			return QVariant();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -105,8 +105,6 @@ DBusResult<qreal> DBusDataTransform<PowerPercentage>::fromWire(qreal wire) {
 | 
			
		|||
	return DBusResult(wire * 0.01);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
qreal DBusDataTransform<PowerPercentage>::toWire(const qreal& value) { return value * 100; }
 | 
			
		||||
 | 
			
		||||
DBusResult<UPowerDeviceState::Enum>
 | 
			
		||||
DBusDataTransform<UPowerDeviceState::Enum>::fromWire(quint32 wire) {
 | 
			
		||||
	if (wire != UPowerDeviceType::Battery && wire >= UPowerDeviceState::Unknown
 | 
			
		||||
| 
						 | 
				
			
			@ -120,10 +118,6 @@ DBusDataTransform<UPowerDeviceState::Enum>::fromWire(quint32 wire) {
 | 
			
		|||
	);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
quint32 DBusDataTransform<UPowerDeviceState::Enum>::toWire(const UPowerDeviceState::Enum& value) {
 | 
			
		||||
	return static_cast<quint32>(value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
DBusResult<UPowerDeviceType::Enum> DBusDataTransform<UPowerDeviceType::Enum>::fromWire(quint32 wire
 | 
			
		||||
) {
 | 
			
		||||
	if (wire >= UPowerDeviceType::Unknown && wire <= UPowerDeviceType::BluetoothGeneric) {
 | 
			
		||||
| 
						 | 
				
			
			@ -135,8 +129,4 @@ DBusResult<UPowerDeviceType::Enum> DBusDataTransform<UPowerDeviceType::Enum>::fr
 | 
			
		|||
	);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
quint32 DBusDataTransform<UPowerDeviceType::Enum>::toWire(const UPowerDeviceType::Enum& value) {
 | 
			
		||||
	return static_cast<quint32>(value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace qs::dbus
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -91,7 +91,6 @@ struct DBusDataTransform<qs::service::upower::UPowerDeviceState::Enum> {
 | 
			
		|||
	using Wire = quint32;
 | 
			
		||||
	using Data = qs::service::upower::UPowerDeviceState::Enum;
 | 
			
		||||
	static DBusResult<Data> fromWire(Wire wire);
 | 
			
		||||
	static Wire toWire(const Data& value);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template <>
 | 
			
		||||
| 
						 | 
				
			
			@ -99,7 +98,6 @@ struct DBusDataTransform<qs::service::upower::UPowerDeviceType::Enum> {
 | 
			
		|||
	using Wire = quint32;
 | 
			
		||||
	using Data = qs::service::upower::UPowerDeviceType::Enum;
 | 
			
		||||
	static DBusResult<Data> fromWire(Wire wire);
 | 
			
		||||
	static Wire toWire(const Data& value);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template <>
 | 
			
		||||
| 
						 | 
				
			
			@ -107,7 +105,6 @@ struct DBusDataTransform<qs::service::upower::PowerPercentage> {
 | 
			
		|||
	using Wire = qreal;
 | 
			
		||||
	using Data = qreal;
 | 
			
		||||
	static DBusResult<Data> fromWire(Wire wire);
 | 
			
		||||
	static Wire toWire(const Data& value);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace qs::dbus
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue