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č
2024-04-24 20:20:29 +02:00
parent fe21ee9656
commit 42f0bd07c0
60 changed files with 1085 additions and 621 deletions
+37 -35
View File
@@ -28,6 +28,7 @@
#include "sdbus-c++/Error.h"
#include "sdbus-c++/Message.h"
#include "sdbus-c++/Types.h"
#include "MessageUtils.h"
#include "ScopeGuard.h"
@@ -108,7 +109,7 @@ Connection::~Connection()
Connection::leaveEventLoop();
}
void Connection::requestName(const std::string& name)
void Connection::requestName(const ServiceName& name)
{
SDBUS_CHECK_SERVICE_NAME(name);
@@ -120,7 +121,7 @@ void Connection::requestName(const std::string& name)
wakeUpEventLoopIfMessagesInQueue();
}
void Connection::releaseName(const std::string& name)
void Connection::releaseName(const ServiceName& name)
{
auto r = sdbus_->sd_bus_release_name(bus_.get(), name.c_str());
SDBUS_THROW_ERROR_IF(r < 0, "Failed to release bus name", -r);
@@ -130,12 +131,12 @@ void Connection::releaseName(const std::string& name)
wakeUpEventLoopIfMessagesInQueue();
}
std::string Connection::getUniqueName() const
BusName Connection::getUniqueName() const
{
const char* unique = nullptr;
auto r = sdbus_->sd_bus_get_unique_name(bus_.get(), &unique);
SDBUS_THROW_ERROR_IF(r < 0 || unique == nullptr, "Failed to get unique bus name", -r);
return unique;
const char* name{};
auto r = sdbus_->sd_bus_get_unique_name(bus_.get(), &name);
SDBUS_THROW_ERROR_IF(r < 0 || name == nullptr, "Failed to get unique bus name", -r);
return BusName{name};
}
void Connection::enterEventLoop()
@@ -188,14 +189,14 @@ ISdBus& Connection::getSdBusInterface()
return *sdbus_.get();
}
void Connection::addObjectManager(const std::string& objectPath, floating_slot_t)
void Connection::addObjectManager(const ObjectPath& objectPath, floating_slot_t)
{
auto r = sdbus_->sd_bus_add_object_manager(bus_.get(), nullptr, objectPath.c_str());
SDBUS_THROW_ERROR_IF(r < 0, "Failed to add object manager", -r);
}
Slot Connection::addObjectManager(const std::string& objectPath, return_slot_t)
Slot Connection::addObjectManager(const ObjectPath& objectPath, return_slot_t)
{
sd_bus_slot *slot{};
@@ -456,8 +457,8 @@ void Connection::deleteSdEventSource(sd_event_source *s)
#endif // SDBUS_basu
Slot Connection::addObjectVTable( const std::string& objectPath
, const std::string& interfaceName
Slot Connection::addObjectVTable( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const sd_bus_vtable* vtable
, void* userData )
{
@@ -486,10 +487,10 @@ PlainMessage Connection::createPlainMessage() const
return Message::Factory::create<PlainMessage>(sdbusMsg, sdbus_.get(), adopt_message);
}
MethodCall Connection::createMethodCall( const std::string& destination
, const std::string& objectPath
, const std::string& interfaceName
, const std::string& methodName ) const
MethodCall Connection::createMethodCall( const ServiceName& destination
, const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const MethodName& methodName ) const
{
sd_bus_message *sdbusMsg{};
@@ -505,9 +506,9 @@ MethodCall Connection::createMethodCall( const std::string& destination
return Message::Factory::create<MethodCall>(sdbusMsg, sdbus_.get(), adopt_message);
}
Signal Connection::createSignal( const std::string& objectPath
, const std::string& interfaceName
, const std::string& signalName ) const
Signal Connection::createSignal( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const SignalName& signalName ) const
{
sd_bus_message *sdbusMsg{};
@@ -562,9 +563,9 @@ Slot Connection::callMethod(const MethodCall& message, void* callback, void* use
return slot;
}
void Connection::emitPropertiesChangedSignal( const std::string& objectPath
, const std::string& interfaceName
, const std::vector<std::string>& propNames )
void Connection::emitPropertiesChangedSignal( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const std::vector<PropertyName>& propNames )
{
auto names = to_strv(propNames);
@@ -576,15 +577,15 @@ void Connection::emitPropertiesChangedSignal( const std::string& objectPath
SDBUS_THROW_ERROR_IF(r < 0, "Failed to emit PropertiesChanged signal", -r);
}
void Connection::emitInterfacesAddedSignal(const std::string& objectPath)
void Connection::emitInterfacesAddedSignal(const ObjectPath& objectPath)
{
auto r = sdbus_->sd_bus_emit_object_added(bus_.get(), objectPath.c_str());
SDBUS_THROW_ERROR_IF(r < 0, "Failed to emit InterfacesAdded signal for all registered interfaces", -r);
}
void Connection::emitInterfacesAddedSignal( const std::string& objectPath
, const std::vector<std::string>& interfaces )
void Connection::emitInterfacesAddedSignal( const ObjectPath& objectPath
, const std::vector<InterfaceName>& interfaces )
{
auto names = to_strv(interfaces);
@@ -595,15 +596,15 @@ void Connection::emitInterfacesAddedSignal( const std::string& objectPath
SDBUS_THROW_ERROR_IF(r < 0, "Failed to emit InterfacesAdded signal", -r);
}
void Connection::emitInterfacesRemovedSignal(const std::string& objectPath)
void Connection::emitInterfacesRemovedSignal(const ObjectPath& objectPath)
{
auto r = sdbus_->sd_bus_emit_object_removed(bus_.get(), objectPath.c_str());
SDBUS_THROW_ERROR_IF(r < 0, "Failed to emit InterfacesRemoved signal for all registered interfaces", -r);
}
void Connection::emitInterfacesRemovedSignal( const std::string& objectPath
, const std::vector<std::string>& interfaces )
void Connection::emitInterfacesRemovedSignal( const ObjectPath& objectPath
, const std::vector<InterfaceName>& interfaces )
{
auto names = to_strv(interfaces);
@@ -614,10 +615,10 @@ void Connection::emitInterfacesRemovedSignal( const std::string& objectPath
SDBUS_THROW_ERROR_IF(r < 0, "Failed to emit InterfacesRemoved signal", -r);
}
Slot Connection::registerSignalHandler( const std::string& sender
, const std::string& objectPath
, const std::string& interfaceName
, const std::string& signalName
Slot Connection::registerSignalHandler( const ServiceName& sender
, const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const SignalName& signalName
, sd_bus_message_handler_t callback
, void* userData )
{
@@ -777,7 +778,8 @@ Message Connection::getCurrentlyProcessedMessage() const
return Message::Factory::create<Message>(sdbusMsg, sdbus_.get());
}
std::vector</*const */char*> Connection::to_strv(const std::vector<std::string>& strings)
template <typename StringBasedType>
std::vector</*const */char*> Connection::to_strv(const std::vector<StringBasedType>& strings)
{
std::vector</*const */char*> strv;
for (auto& str : strings)
@@ -890,7 +892,7 @@ std::unique_ptr<sdbus::IConnection> createBusConnection()
return std::make_unique<sdbus::internal::Connection>(std::move(interface), Connection::default_bus);
}
std::unique_ptr<sdbus::IConnection> createBusConnection(const std::string& name)
std::unique_ptr<sdbus::IConnection> createBusConnection(const ServiceName& name)
{
auto conn = createBusConnection();
conn->requestName(name);
@@ -903,7 +905,7 @@ std::unique_ptr<sdbus::IConnection> createSystemBusConnection()
return std::make_unique<sdbus::internal::Connection>(std::move(interface), Connection::system_bus);
}
std::unique_ptr<sdbus::IConnection> createSystemBusConnection(const std::string& name)
std::unique_ptr<sdbus::IConnection> createSystemBusConnection(const ServiceName& name)
{
auto conn = createSystemBusConnection();
conn->requestName(name);
@@ -916,7 +918,7 @@ std::unique_ptr<sdbus::IConnection> createSessionBusConnection()
return std::make_unique<sdbus::internal::Connection>(std::move(interface), Connection::session_bus);
}
std::unique_ptr<sdbus::IConnection> createSessionBusConnection(const std::string& name)
std::unique_ptr<sdbus::IConnection> createSessionBusConnection(const ServiceName& name)
{
auto conn = createSessionBusConnection();
conn->requestName(name);
+40 -28
View File
@@ -41,7 +41,18 @@
#include <thread>
#include <vector>
// Forward declarations
struct sd_event_source;
namespace sdbus {
class ObjectPath;
class InterfaceName;
class BusName;
using ServiceName = BusName;
class MemberName;
using MethodName = MemberName;
using SignalName = MemberName;
using PropertyName = MemberName;
}
namespace sdbus::internal {
@@ -81,9 +92,9 @@ namespace sdbus::internal {
Connection(std::unique_ptr<ISdBus>&& interface, pseudo_bus_t);
~Connection() override;
void requestName(const std::string& name) override;
void releaseName(const std::string& name) override;
[[nodiscard]] std::string getUniqueName() const override;
void requestName(const ServiceName & name) override;
void releaseName(const ServiceName& name) override;
[[nodiscard]] BusName getUniqueName() const override;
void enterEventLoop() override;
void enterEventLoopAsync() override;
void leaveEventLoop() override;
@@ -91,8 +102,8 @@ namespace sdbus::internal {
bool processPendingEvent() override;
Message getCurrentlyProcessedMessage() const override;
void addObjectManager(const std::string& objectPath, floating_slot_t) override;
Slot addObjectManager(const std::string& objectPath, return_slot_t) override;
void addObjectManager(const ObjectPath& objectPath, floating_slot_t) override;
Slot addObjectManager(const ObjectPath& objectPath, return_slot_t) override;
void setMethodCallTimeout(uint64_t timeout) override;
[[nodiscard]] uint64_t getMethodCallTimeout() const override;
@@ -109,38 +120,38 @@ namespace sdbus::internal {
[[nodiscard]] const ISdBus& getSdBusInterface() const override;
[[nodiscard]] ISdBus& getSdBusInterface() override;
Slot addObjectVTable( const std::string& objectPath
, const std::string& interfaceName
Slot addObjectVTable( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const sd_bus_vtable* vtable
, void* userData ) override;
[[nodiscard]] PlainMessage createPlainMessage() const override;
[[nodiscard]] MethodCall createMethodCall( const std::string& destination
, const std::string& objectPath
, const std::string& interfaceName
, const std::string& methodName ) const override;
[[nodiscard]] Signal createSignal( const std::string& objectPath
, const std::string& interfaceName
, const std::string& signalName ) const override;
[[nodiscard]] MethodCall createMethodCall( const ServiceName& destination
, const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const MethodName& methodName ) const override;
[[nodiscard]] Signal createSignal( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const SignalName& signalName ) const override;
MethodReply callMethod(const MethodCall& message, uint64_t timeout) override;
void callMethod(const MethodCall& message, void* callback, void* userData, uint64_t timeout, floating_slot_t) override;
Slot callMethod(const MethodCall& message, void* callback, void* userData, uint64_t timeout) override;
void emitPropertiesChangedSignal( const std::string& objectPath
, const std::string& interfaceName
, const std::vector<std::string>& propNames ) override;
void emitInterfacesAddedSignal(const std::string& objectPath) override;
void emitInterfacesAddedSignal( const std::string& objectPath
, const std::vector<std::string>& interfaces ) override;
void emitInterfacesRemovedSignal(const std::string& objectPath) override;
void emitInterfacesRemovedSignal( const std::string& objectPath
, const std::vector<std::string>& interfaces ) override;
void emitPropertiesChangedSignal( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const std::vector<PropertyName>& propNames ) override;
void emitInterfacesAddedSignal(const ObjectPath& objectPath) override;
void emitInterfacesAddedSignal( const ObjectPath& objectPath
, const std::vector<InterfaceName>& interfaces ) override;
void emitInterfacesRemovedSignal(const ObjectPath& objectPath) override;
void emitInterfacesRemovedSignal( const ObjectPath& objectPath
, const std::vector<InterfaceName>& interfaces ) override;
Slot registerSignalHandler( const std::string& sender
, const std::string& objectPath
, const std::string& interfaceName
, const std::string& signalName
Slot registerSignalHandler( const ServiceName& sender
, const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const SignalName& signalName
, sd_bus_message_handler_t callback
, void* userData ) override;
@@ -161,7 +172,8 @@ namespace sdbus::internal {
void wakeUpEventLoopIfMessagesInQueue();
void joinWithEventLoop();
static std::vector</*const */char*> to_strv(const std::vector<std::string>& strings);
template <typename StringBasedType>
static std::vector</*const */char*> to_strv(const std::vector<StringBasedType>& strings);
static int sdbus_match_callback(sd_bus_message *sdbusMessage, void *userData, sd_bus_error *retError);
static int sdbus_match_install_callback(sd_bus_message *sdbusMessage, void *userData, sd_bus_error *retError);
+8 -4
View File
@@ -32,14 +32,18 @@
namespace sdbus
{
sdbus::Error createError(int errNo, const std::string& customMsg)
sdbus::Error createError(int errNo, std::string customMsg)
{
sd_bus_error sdbusError = SD_BUS_ERROR_NULL;
sd_bus_error_set_errno(&sdbusError, errNo);
SCOPE_EXIT{ sd_bus_error_free(&sdbusError); };
std::string name(sdbusError.name);
std::string message(customMsg + " (" + sdbusError.message + ")");
return sdbus::Error(name, message);
Error::Name name(sdbusError.name);
std::string message(std::move(customMsg));
message.append(" (");
message.append(sdbusError.message);
message.append(")");
return Error(std::move(name), std::move(message));
}
} // namespace sdbus
+32 -24
View File
@@ -37,12 +37,20 @@
#include SDBUS_HEADER
#include <vector>
// Forward declaration
// Forward declarations
namespace sdbus {
class MethodCall;
class MethodReply;
class Signal;
class PlainMessage;
class ObjectPath;
class InterfaceName;
class BusName;
using ServiceName = BusName;
class MemberName;
using MethodName = MemberName;
using SignalName = MemberName;
using PropertyName = MemberName;
namespace internal {
class ISdBus;
}
@@ -59,41 +67,41 @@ namespace sdbus::internal {
[[nodiscard]] virtual const ISdBus& getSdBusInterface() const = 0;
[[nodiscard]] virtual ISdBus& getSdBusInterface() = 0;
[[nodiscard]] virtual Slot addObjectVTable( const std::string& objectPath
, const std::string& interfaceName
[[nodiscard]] virtual Slot addObjectVTable( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const sd_bus_vtable* vtable
, void* userData ) = 0;
[[nodiscard]] virtual PlainMessage createPlainMessage() const = 0;
[[nodiscard]] virtual MethodCall createMethodCall( const std::string& destination
, const std::string& objectPath
, const std::string& interfaceName
, const std::string& methodName ) const = 0;
[[nodiscard]] virtual Signal createSignal( const std::string& objectPath
, const std::string& interfaceName
, const std::string& signalName ) const = 0;
[[nodiscard]] virtual MethodCall createMethodCall( const ServiceName& destination
, const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const MethodName& methodName ) const = 0;
[[nodiscard]] virtual Signal createSignal( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const SignalName& signalName ) const = 0;
virtual MethodReply callMethod(const MethodCall& message, uint64_t timeout) = 0;
virtual void callMethod(const MethodCall& message, void* callback, void* userData, uint64_t timeout, floating_slot_t) = 0;
[[nodiscard]] virtual Slot callMethod(const MethodCall& message, void* callback, void* userData, uint64_t timeout) = 0;
virtual void emitPropertiesChangedSignal( const std::string& objectPath
, const std::string& interfaceName
, const std::vector<std::string>& propNames ) = 0;
virtual void emitInterfacesAddedSignal(const std::string& objectPath) = 0;
virtual void emitInterfacesAddedSignal( const std::string& objectPath
, const std::vector<std::string>& interfaces ) = 0;
virtual void emitInterfacesRemovedSignal(const std::string& objectPath) = 0;
virtual void emitInterfacesRemovedSignal( const std::string& objectPath
, const std::vector<std::string>& interfaces ) = 0;
virtual void emitPropertiesChangedSignal( const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const std::vector<PropertyName>& propNames ) = 0;
virtual void emitInterfacesAddedSignal(const ObjectPath& objectPath) = 0;
virtual void emitInterfacesAddedSignal( const ObjectPath& objectPath
, const std::vector<InterfaceName>& interfaces ) = 0;
virtual void emitInterfacesRemovedSignal(const ObjectPath& objectPath) = 0;
virtual void emitInterfacesRemovedSignal( const ObjectPath& objectPath
, const std::vector<InterfaceName>& interfaces ) = 0;
using sdbus::IConnection::addObjectManager;
[[nodiscard]] virtual Slot addObjectManager(const std::string& objectPath, return_slot_t) = 0;
[[nodiscard]] virtual Slot addObjectManager(const ObjectPath& objectPath, return_slot_t) = 0;
[[nodiscard]] virtual Slot registerSignalHandler( const std::string& sender
, const std::string& objectPath
, const std::string& interfaceName
, const std::string& signalName
[[nodiscard]] virtual Slot registerSignalHandler( const ServiceName& sender
, const ObjectPath& objectPath
, const InterfaceName& interfaceName
, const SignalName& signalName
, sd_bus_message_handler_t callback
, void* userData ) = 0;
};
+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);
+20 -16
View File
@@ -43,20 +43,20 @@
namespace sdbus::internal {
Object::Object(sdbus::internal::IConnection& connection, std::string objectPath)
Object::Object(sdbus::internal::IConnection& connection, ObjectPath objectPath)
: connection_(connection), objectPath_(std::move(objectPath))
{
SDBUS_CHECK_OBJECT_PATH(objectPath_);
}
void Object::addVTable(std::string interfaceName, std::vector<VTableItem> vtable)
void Object::addVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable)
{
auto slot = Object::addVTable(std::move(interfaceName), std::move(vtable), return_slot);
vtables_.push_back(std::move(slot));
}
Slot Object::addVTable(std::string interfaceName, std::vector<VTableItem> vtable, return_slot_t)
Slot Object::addVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable, return_slot_t)
{
SDBUS_CHECK_INTERFACE_NAME(interfaceName);
@@ -79,7 +79,7 @@ void Object::unregister()
removeObjectManager();
}
sdbus::Signal Object::createSignal(const std::string& interfaceName, const std::string& signalName)
sdbus::Signal Object::createSignal(const InterfaceName& interfaceName, const SignalName& signalName)
{
return connection_.createSignal(objectPath_, interfaceName, signalName);
}
@@ -91,12 +91,12 @@ void Object::emitSignal(const sdbus::Signal& message)
message.send();
}
void Object::emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& propNames)
void Object::emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& propNames)
{
connection_.emitPropertiesChangedSignal(objectPath_, interfaceName, propNames);
}
void Object::emitPropertiesChangedSignal(const std::string& interfaceName)
void Object::emitPropertiesChangedSignal(const InterfaceName& interfaceName)
{
Object::emitPropertiesChangedSignal(interfaceName, {});
}
@@ -106,7 +106,7 @@ void Object::emitInterfacesAddedSignal()
connection_.emitInterfacesAddedSignal(objectPath_);
}
void Object::emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
void Object::emitInterfacesAddedSignal(const std::vector<InterfaceName>& interfaces)
{
connection_.emitInterfacesAddedSignal(objectPath_, interfaces);
}
@@ -116,7 +116,7 @@ void Object::emitInterfacesRemovedSignal()
connection_.emitInterfacesRemovedSignal(objectPath_);
}
void Object::emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
void Object::emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces)
{
connection_.emitInterfacesRemovedSignal(objectPath_, interfaces);
}
@@ -141,7 +141,7 @@ sdbus::IConnection& Object::getConnection() const
return connection_;
}
const std::string& Object::getObjectPath() const
const ObjectPath& Object::getObjectPath() const
{
return objectPath_;
}
@@ -151,7 +151,7 @@ Message Object::getCurrentlyProcessedMessage() const
return connection_.getCurrentlyProcessedMessage();
}
Object::VTable Object::createInternalVTable(std::string interfaceName, std::vector<VTableItem> vtable)
Object::VTable Object::createInternalVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable)
{
VTable internalVTable;
@@ -241,8 +241,8 @@ void Object::startSdBusVTable(const Flags& interfaceFlags, std::vector<sd_bus_vt
void Object::writeMethodRecordToSdBusVTable(const VTable::MethodItem& method, std::vector<sd_bus_vtable>& vtable)
{
auto vtableItem = createSdBusVTableMethodItem( method.name.c_str()
, method.inputArgs.c_str()
, method.outputArgs.c_str()
, method.inputSignature.c_str()
, method.outputSignature.c_str()
, method.paramNames.c_str()
, &Object::sdbus_method_callback
, method.flags.toSdBusMethodFlags() );
@@ -278,23 +278,27 @@ void Object::finalizeSdBusVTable(std::vector<sd_bus_vtable>& vtable)
vtable.push_back(createSdBusVTableEndItem());
}
const Object::VTable::MethodItem* Object::findMethod(const VTable& vtable, const std::string& methodName)
const Object::VTable::MethodItem* Object::findMethod(const VTable& vtable, std::string_view methodName)
{
auto it = std::lower_bound(vtable.methods.begin(), vtable.methods.end(), methodName, [](const auto& methodItem, const auto& methodName)
{
return methodItem.name < methodName;
});
(void)it;
return it != vtable.methods.end() && it->name == methodName ? &*it : nullptr;
}
const Object::VTable::PropertyItem* Object::findProperty(const VTable& vtable, const std::string& propertyName)
const Object::VTable::PropertyItem* Object::findProperty(const VTable& vtable, std::string_view propertyName)
{
auto it = std::lower_bound(vtable.properties.begin(), vtable.properties.end(), propertyName, [](const auto& propertyItem, const auto& propertyName)
{
return propertyItem.name < propertyName;
});
(void)it;
return it != vtable.properties.end() && it->name == propertyName ? &*it : nullptr;
}
@@ -314,7 +318,7 @@ int Object::sdbus_method_callback(sd_bus_message *sdbusMessage, void *userData,
auto message = Message::Factory::create<MethodCall>(sdbusMessage, &vtable->object->connection_.getSdBusInterface());
const auto* methodItem = findMethod(*vtable, message.getMemberName());
const auto* methodItem = findMethod(*vtable, message.getMemberName()); // TODO: optimize the situation around getMemberName()
assert(methodItem != nullptr);
assert(methodItem->callback);
@@ -379,7 +383,7 @@ int Object::sdbus_property_set_callback( sd_bus */*bus*/
namespace sdbus {
std::unique_ptr<sdbus::IObject> createObject(sdbus::IConnection& connection, std::string objectPath)
std::unique_ptr<sdbus::IObject> createObject(sdbus::IConnection& connection, ObjectPath objectPath)
{
auto* sdbusConnection = dynamic_cast<sdbus::internal::IConnection*>(&connection);
SDBUS_THROW_ERROR_IF(!sdbusConnection, "Connection is not a real sdbus-c++ connection", EINVAL);
+23 -21
View File
@@ -30,12 +30,14 @@
#include "sdbus-c++/IObject.h"
#include "IConnection.h"
#include "sdbus-c++/Types.h"
#include <cassert>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include SDBUS_HEADER
#include <vector>
@@ -45,27 +47,27 @@ namespace sdbus::internal {
: public IObject
{
public:
Object(sdbus::internal::IConnection& connection, std::string objectPath);
Object(sdbus::internal::IConnection& connection, ObjectPath objectPath);
void addVTable(std::string interfaceName, std::vector<VTableItem> vtable) override;
Slot addVTable(std::string interfaceName, std::vector<VTableItem> vtable, return_slot_t) override;
void addVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable) override;
Slot addVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable, return_slot_t) override;
void unregister() override;
sdbus::Signal createSignal(const std::string& interfaceName, const std::string& signalName) override;
sdbus::Signal createSignal(const InterfaceName& interfaceName, const SignalName& signalName) override;
void emitSignal(const sdbus::Signal& message) override;
void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& propNames) override;
void emitPropertiesChangedSignal(const std::string& interfaceName) override;
void emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& propNames) override;
void emitPropertiesChangedSignal(const InterfaceName& interfaceName) override;
void emitInterfacesAddedSignal() override;
void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces) override;
void emitInterfacesAddedSignal(const std::vector<InterfaceName>& interfaces) override;
void emitInterfacesRemovedSignal() override;
void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces) override;
void emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces) override;
void addObjectManager() override;
void removeObjectManager() override;
[[nodiscard]] bool hasObjectManager() const override;
[[nodiscard]] sdbus::IConnection& getConnection() const override;
[[nodiscard]] const std::string& getObjectPath() const override;
[[nodiscard]] const ObjectPath& getObjectPath() const override;
[[nodiscard]] Message getCurrentlyProcessedMessage() const override;
private:
@@ -74,14 +76,14 @@ namespace sdbus::internal {
// An interface can have any number of vtables attached to it, not only one.
struct VTable
{
std::string interfaceName;
InterfaceName interfaceName;
Flags interfaceFlags;
struct MethodItem
{
std::string name;
std::string inputArgs;
std::string outputArgs;
MethodName name;
Signature inputSignature;
Signature outputSignature;
std::string paramNames;
method_callback callback;
Flags flags;
@@ -91,8 +93,8 @@ namespace sdbus::internal {
struct SignalItem
{
std::string name;
std::string signature;
SignalName name;
Signature signature;
std::string paramNames;
Flags flags;
};
@@ -101,8 +103,8 @@ namespace sdbus::internal {
struct PropertyItem
{
std::string name;
std::string signature;
PropertyName name;
Signature signature;
property_get_callback getCallback;
property_set_callback setCallback;
Flags flags;
@@ -121,7 +123,7 @@ namespace sdbus::internal {
Slot slot;
};
VTable createInternalVTable(std::string interfaceName, std::vector<VTableItem> vtable);
VTable createInternalVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable);
void writeInterfaceFlagsToVTable(InterfaceFlagsVTableItem flags, VTable& vtable);
void writeMethodRecordToVTable(MethodVTableItem method, VTable& vtable);
void writeSignalRecordToVTable(SignalVTableItem signal, VTable& vtable);
@@ -134,8 +136,8 @@ namespace sdbus::internal {
static void writePropertyRecordToSdBusVTable(const VTable::PropertyItem& property, std::vector<sd_bus_vtable>& vtable);
static void finalizeSdBusVTable(std::vector<sd_bus_vtable>& vtable);
static const VTable::MethodItem* findMethod(const VTable& vtable, const std::string& methodName);
static const VTable::PropertyItem* findProperty(const VTable& vtable, const std::string& propertyName);
static const VTable::MethodItem* findMethod(const VTable& vtable, std::string_view methodName);
static const VTable::PropertyItem* findProperty(const VTable& vtable, std::string_view propertyName);
static std::string paramNamesToString(const std::vector<std::string>& paramNames);
@@ -157,7 +159,7 @@ namespace sdbus::internal {
private:
sdbus::internal::IConnection& connection_;
std::string objectPath_;
ObjectPath objectPath_;
std::vector<Slot> vtables_;
Slot objectManagerSlot_;
};
+22 -22
View File
@@ -41,7 +41,7 @@
namespace sdbus::internal {
Proxy::Proxy(sdbus::internal::IConnection& connection, std::string destination, std::string objectPath)
Proxy::Proxy(sdbus::internal::IConnection& connection, ServiceName destination, ObjectPath objectPath)
: connection_(&connection, [](sdbus::internal::IConnection *){ /* Intentionally left empty */ })
, destination_(std::move(destination))
, objectPath_(std::move(objectPath))
@@ -54,8 +54,8 @@ Proxy::Proxy(sdbus::internal::IConnection& connection, std::string destination,
}
Proxy::Proxy( std::unique_ptr<sdbus::internal::IConnection>&& connection
, std::string destination
, std::string objectPath )
, ServiceName destination
, ObjectPath objectPath )
: connection_(std::move(connection))
, destination_(std::move(destination))
, objectPath_(std::move(objectPath))
@@ -69,8 +69,8 @@ Proxy::Proxy( std::unique_ptr<sdbus::internal::IConnection>&& connection
}
Proxy::Proxy( std::unique_ptr<sdbus::internal::IConnection>&& connection
, std::string destination
, std::string objectPath
, ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t )
: connection_(std::move(connection))
, destination_(std::move(destination))
@@ -83,7 +83,7 @@ Proxy::Proxy( std::unique_ptr<sdbus::internal::IConnection>&& connection
// This proxy is meant to be created, used for simple synchronous D-Bus call(s) and then dismissed.
}
MethodCall Proxy::createMethodCall(const std::string& interfaceName, const std::string& methodName)
MethodCall Proxy::createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName)
{
return connection_->createMethodCall(destination_, objectPath_, interfaceName, methodName);
}
@@ -134,8 +134,8 @@ std::future<MethodReply> Proxy::callMethodAsync(const MethodCall& message, uint6
return future;
}
void Proxy::registerSignalHandler( const std::string& interfaceName
, const std::string& signalName
void Proxy::registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler )
{
auto slot = Proxy::registerSignalHandler(interfaceName, signalName, std::move(signalHandler), return_slot);
@@ -143,8 +143,8 @@ void Proxy::registerSignalHandler( const std::string& interfaceName
floatingSignalSlots_.push_back(std::move(slot));
}
Slot Proxy::registerSignalHandler( const std::string& interfaceName
, const std::string& signalName
Slot Proxy::registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler
, return_slot_t )
{
@@ -175,7 +175,7 @@ sdbus::IConnection& Proxy::getConnection() const
return *connection_;
}
const std::string& Proxy::getObjectPath() const
const ObjectPath& Proxy::getObjectPath() const
{
return objectPath_;
}
@@ -211,7 +211,7 @@ int Proxy::sdbus_async_reply_handler(sd_bus_message *sdbusMessage, void *userDat
}
else
{
Error exception(error->name, error->message);
Error exception(Error::Name{error->name}, error->message);
asyncCallData->callback(std::move(message), std::move(exception));
}
}, retError);
@@ -266,8 +266,8 @@ bool PendingAsyncCall::isPending() const
namespace sdbus {
std::unique_ptr<sdbus::IProxy> createProxy( IConnection& connection
, std::string destination
, std::string objectPath )
, ServiceName destination
, ObjectPath objectPath )
{
auto* sdbusConnection = dynamic_cast<sdbus::internal::IConnection*>(&connection);
SDBUS_THROW_ERROR_IF(!sdbusConnection, "Connection is not a real sdbus-c++ connection", EINVAL);
@@ -278,8 +278,8 @@ std::unique_ptr<sdbus::IProxy> createProxy( IConnection& connection
}
std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<IConnection>&& connection
, std::string destination
, std::string objectPath )
, ServiceName destination
, ObjectPath objectPath )
{
auto* sdbusConnection = dynamic_cast<sdbus::internal::IConnection*>(connection.get());
SDBUS_THROW_ERROR_IF(!sdbusConnection, "Connection is not a real sdbus-c++ connection", EINVAL);
@@ -292,8 +292,8 @@ std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<IConnection>&& conne
}
std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<IConnection>&& connection
, std::string destination
, std::string objectPath
, ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t )
{
auto* sdbusConnection = dynamic_cast<sdbus::internal::IConnection*>(connection.get());
@@ -307,8 +307,8 @@ std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<IConnection>&& conne
, dont_run_event_loop_thread );
}
std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
, std::string objectPath )
std::unique_ptr<sdbus::IProxy> createProxy( ServiceName destination
, ObjectPath objectPath )
{
auto connection = sdbus::createBusConnection();
@@ -320,8 +320,8 @@ std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
, std::move(objectPath) );
}
std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
, std::string objectPath
std::unique_ptr<sdbus::IProxy> createProxy( ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t )
{
auto connection = sdbus::createBusConnection();
+15 -14
View File
@@ -30,6 +30,7 @@
#include "sdbus-c++/IProxy.h"
#include "IConnection.h"
#include "sdbus-c++/Types.h"
#include <deque>
#include <memory>
@@ -45,33 +46,33 @@ namespace sdbus::internal {
{
public:
Proxy( sdbus::internal::IConnection& connection
, std::string destination
, std::string objectPath );
, ServiceName destination
, ObjectPath objectPath );
Proxy( std::unique_ptr<sdbus::internal::IConnection>&& connection
, std::string destination
, std::string objectPath );
, ServiceName destination
, ObjectPath objectPath );
Proxy( std::unique_ptr<sdbus::internal::IConnection>&& connection
, std::string destination
, std::string objectPath
, ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t );
MethodCall createMethodCall(const std::string& interfaceName, const std::string& methodName) override;
MethodCall createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName) override;
MethodReply callMethod(const MethodCall& message, uint64_t timeout) override;
PendingAsyncCall callMethodAsync(const MethodCall& message, async_reply_handler asyncReplyCallback, uint64_t timeout) override;
std::future<MethodReply> callMethodAsync(const MethodCall& message, with_future_t) override;
std::future<MethodReply> callMethodAsync(const MethodCall& message, uint64_t timeout, with_future_t) override;
void registerSignalHandler( const std::string& interfaceName
, const std::string& signalName
void registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler ) override;
Slot registerSignalHandler( const std::string& interfaceName
, const std::string& signalName
Slot registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler
, return_slot_t ) override;
void unregister() override;
[[nodiscard]] sdbus::IConnection& getConnection() const override;
[[nodiscard]] const std::string& getObjectPath() const override;
[[nodiscard]] const ObjectPath& getObjectPath() const override;
[[nodiscard]] Message getCurrentlyProcessedMessage() const override;
private:
@@ -84,8 +85,8 @@ namespace sdbus::internal {
std::unique_ptr< sdbus::internal::IConnection
, std::function<void(sdbus::internal::IConnection*)>
> connection_;
std::string destination_;
std::string objectPath_;
ServiceName destination_;
ObjectPath objectPath_;
std::vector<Slot> floatingSignalSlots_;
+3 -3
View File
@@ -41,7 +41,7 @@
SDBUS_THROW_ERROR_IF(!_NAME.empty() && !sd_bus_service_name_is_valid(_NAME.c_str()), "Invalid service name '" + _NAME + "' provided", EINVAL) \
/**/
#define SDBUS_CHECK_MEMBER_NAME(_NAME) \
SDBUS_THROW_ERROR_IF(!sd_bus_member_name_is_valid(_NAME.c_str()), "Invalid member name '" + _NAME + "' provided", EINVAL) \
SDBUS_THROW_ERROR_IF(!sd_bus_member_name_is_valid(_NAME.c_str()), std::string("Invalid member name '") + _NAME.c_str() + "' provided", EINVAL) \
/**/
#else
#define SDBUS_CHECK_OBJECT_PATH(_PATH)
@@ -66,12 +66,12 @@ namespace sdbus::internal {
}
catch (const std::exception& e)
{
sd_bus_error_set(retError, SDBUSCPP_ERROR_NAME, e.what());
sd_bus_error_set(retError, SDBUSCPP_ERROR_NAME.c_str(), e.what());
return false;
}
catch (...)
{
sd_bus_error_set(retError, SDBUSCPP_ERROR_NAME, "Unknown error occurred");
sd_bus_error_set(retError, SDBUSCPP_ERROR_NAME.c_str(), "Unknown error occurred");
return false;
}