refactor: add strong types to public API (#414)

This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.

What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.

These types are:
  * `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
  * `InterfaceName` type for D-Bus interface names,
  * `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
  * `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
  * `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
  * `Error::Name` type for D-Bus error names.
This commit is contained in:
Stanislav Angelovič committed 2024-04-24 20:20:29 +02:00
1 parent fe21ee9656
commit 42f0bd07c0
60 files changed
+1085 -621

No files matched your search

+17 -16
View File
@@ -602,33 +602,34 @@ void Message::rewind(bool complete)
SDBUS_THROW_ERROR_IF(r < 0, "Failed to rewind the message", -r);
}
std::string Message::getInterfaceName() const
InterfaceName Message::getInterfaceName() const
{
auto interface = sd_bus_message_get_interface((sd_bus_message*)msg_);
return interface != nullptr ? interface : "";
const auto* interface = sd_bus_message_get_interface((sd_bus_message*)msg_);
return interface != nullptr ? InterfaceName{interface} : InterfaceName{};
}
std::string Message::getMemberName() const
MemberName Message::getMemberName() const
{
auto member = sd_bus_message_get_member((sd_bus_message*)msg_);
return member != nullptr ? member : "";
const auto* member = sd_bus_message_get_member((sd_bus_message*)msg_);
return member != nullptr ? MemberName{member} : MemberName{};
}
std::string Message::getSender() const
ConnectionName Message::getSender() const
{
return sd_bus_message_get_sender((sd_bus_message*)msg_);
const auto* sender = sd_bus_message_get_sender((sd_bus_message*)msg_);
return ConnectionName{sender};
}
std::string Message::getPath() const
ObjectPath Message::getPath() const
{
auto path = sd_bus_message_get_path((sd_bus_message*)msg_);
return path != nullptr ? path : "";
const auto* path = sd_bus_message_get_path((sd_bus_message*)msg_);
return path != nullptr ? ObjectPath{path} : ObjectPath{};
}
std::string Message::getDestination() const
ConnectionName Message::getDestination() const
{
auto destination = sd_bus_message_get_destination((sd_bus_message*)msg_);
return destination != nullptr ? destination : "";
const auto* destination = sd_bus_message_get_destination((sd_bus_message*)msg_);
return destination != nullptr ? ConnectionName{destination} : ConnectionName{};
}
void Message::peekType(std::string& type, std::string& contents) const
@@ -801,7 +802,7 @@ MethodReply MethodCall::sendWithReply(uint64_t timeout) const
auto r = sdbus_->sd_bus_call(nullptr, (sd_bus_message*)msg_, timeout, &sdbusError, &sdbusReply);
if (sd_bus_error_is_set(&sdbusError))
throw sdbus::Error(sdbusError.name, sdbusError.message);
throw Error(Error::Name{sdbusError.name}, sdbusError.message);
SDBUS_THROW_ERROR_IF(r < 0, "Failed to call method", -r);
@@ -866,7 +867,7 @@ void Signal::send() const
SDBUS_THROW_ERROR_IF(r < 0, "Failed to emit signal", -r);
}
void Signal::setDestination(const std::string& destination)
void Signal::setDestination(const ConnectionName& destination)
{
auto r = sdbus_->sd_bus_message_set_destination((sd_bus_message*)msg_, destination.c_str());
SDBUS_THROW_ERROR_IF(r < 0, "Failed to set signal destination", -r);