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
+1 -1
View File
@@ -102,7 +102,7 @@ namespace sdbus {
*
* For more information, consult @ref createObject(sdbus::IConnection&,std::string)
*/
AdaptorInterfaces(IConnection& connection, std::string objectPath)
AdaptorInterfaces(IConnection& connection, ObjectPath objectPath)
: ObjectHolder(createObject(connection, std::move(objectPath)))
, _Interfaces(getObject())...
{
+53 -23
View File
@@ -53,7 +53,9 @@ namespace sdbus {
{
public:
VTableAdder(IObject& object, std::vector<VTableItem> vtable);
void forInterface(InterfaceName interfaceName);
void forInterface(std::string interfaceName);
[[nodiscard]] Slot forInterface(InterfaceName interfaceName, return_slot_t);
[[nodiscard]] Slot forInterface(std::string interfaceName, return_slot_t);
private:
@@ -64,15 +66,16 @@ namespace sdbus {
class SignalEmitter
{
public:
SignalEmitter(IObject& object, const std::string& signalName);
SignalEmitter(IObject& object, const SignalName& signalName);
SignalEmitter(SignalEmitter&& other) = default;
~SignalEmitter() noexcept(false);
SignalEmitter& onInterface(const InterfaceName& interfaceName);
SignalEmitter& onInterface(const std::string& interfaceName);
template <typename... _Args> void withArguments(_Args&&... args);
private:
IObject& object_;
const std::string& signalName_;
const SignalName& signalName_;
Signal signal_;
int exceptions_{}; // Number of active exceptions when SignalEmitter is constructed
};
@@ -80,10 +83,11 @@ namespace sdbus {
class MethodInvoker
{
public:
MethodInvoker(IProxy& proxy, const std::string& methodName);
MethodInvoker(IProxy& proxy, const MethodName& methodName);
MethodInvoker(MethodInvoker&& other) = default;
~MethodInvoker() noexcept(false);
MethodInvoker& onInterface(const InterfaceName& interfaceName);
MethodInvoker& onInterface(const std::string& interfaceName);
MethodInvoker& withTimeout(uint64_t usec);
template <typename _Rep, typename _Period>
@@ -95,7 +99,7 @@ namespace sdbus {
private:
IProxy& proxy_;
const std::string& methodName_;
const MethodName& methodName_;
uint64_t timeout_{};
MethodCall method_;
int exceptions_{}; // Number of active exceptions when MethodInvoker is constructed
@@ -105,7 +109,8 @@ namespace sdbus {
class AsyncMethodInvoker
{
public:
AsyncMethodInvoker(IProxy& proxy, const std::string& methodName);
AsyncMethodInvoker(IProxy& proxy, const MethodName& methodName);
AsyncMethodInvoker& onInterface(const InterfaceName& interfaceName);
AsyncMethodInvoker& onInterface(const std::string& interfaceName);
AsyncMethodInvoker& withTimeout(uint64_t usec);
template <typename _Rep, typename _Period>
@@ -119,7 +124,7 @@ namespace sdbus {
private:
IProxy& proxy_;
const std::string& methodName_;
const MethodName& methodName_;
uint64_t timeout_{};
MethodCall method_;
};
@@ -127,7 +132,8 @@ namespace sdbus {
class SignalSubscriber
{
public:
SignalSubscriber(IProxy& proxy, const std::string& signalName);
SignalSubscriber(IProxy& proxy, const SignalName& signalName);
SignalSubscriber& onInterface(InterfaceName interfaceName);
SignalSubscriber& onInterface(std::string interfaceName);
template <typename _Function> void call(_Function&& callback);
template <typename _Function> [[nodiscard]] Slot call(_Function&& callback, return_slot_t);
@@ -137,65 +143,81 @@ namespace sdbus {
private:
IProxy& proxy_;
const std::string& signalName_;
std::string interfaceName_;
const SignalName& signalName_;
InterfaceName interfaceName_;
};
class PropertyGetter
{
public:
PropertyGetter(IProxy& proxy, const std::string& propertyName);
PropertyGetter(IProxy& proxy, const PropertyName& propertyName);
Variant onInterface(const InterfaceName& interfaceName);
Variant onInterface(const std::string& interfaceName);
private:
static inline const InterfaceName DBUS_PROPERTIES_INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
private:
IProxy& proxy_;
const std::string& propertyName_;
const PropertyName& propertyName_;
};
class AsyncPropertyGetter
{
public:
AsyncPropertyGetter(IProxy& proxy, const std::string& propertyName);
AsyncPropertyGetter(IProxy& proxy, const PropertyName& propertyName);
AsyncPropertyGetter& onInterface(const InterfaceName& interfaceName);
AsyncPropertyGetter& onInterface(const std::string& interfaceName);
template <typename _Function> PendingAsyncCall uponReplyInvoke(_Function&& callback);
std::future<Variant> getResultAsFuture();
private:
static inline const InterfaceName DBUS_PROPERTIES_INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
private:
IProxy& proxy_;
const std::string& propertyName_;
const std::string* interfaceName_{};
const PropertyName& propertyName_;
const InterfaceName* interfaceName_{};
};
class PropertySetter
{
public:
PropertySetter(IProxy& proxy, const std::string& propertyName);
PropertySetter(IProxy& proxy, const PropertyName& propertyName);
PropertySetter& onInterface(const InterfaceName& interfaceName);
PropertySetter& onInterface(const std::string& interfaceName);
template <typename _Value> void toValue(const _Value& value);
template <typename _Value> void toValue(const _Value& value, dont_expect_reply_t);
void toValue(const Variant& value);
void toValue(const Variant& value, dont_expect_reply_t);
private:
static inline const InterfaceName DBUS_PROPERTIES_INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
private:
IProxy& proxy_;
const std::string& propertyName_;
const std::string* interfaceName_{};
const PropertyName& propertyName_;
const InterfaceName* interfaceName_{};
};
class AsyncPropertySetter
{
public:
AsyncPropertySetter(IProxy& proxy, const std::string& propertyName);
AsyncPropertySetter(IProxy& proxy, const PropertyName& propertyName);
AsyncPropertySetter& onInterface(const InterfaceName& interfaceName);
AsyncPropertySetter& onInterface(const std::string& interfaceName);
template <typename _Value> AsyncPropertySetter& toValue(_Value&& value);
AsyncPropertySetter& toValue(Variant value);
template <typename _Function> PendingAsyncCall uponReplyInvoke(_Function&& callback);
std::future<void> getResultAsFuture();
private:
static inline const InterfaceName DBUS_PROPERTIES_INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
private:
IProxy& proxy_;
const std::string& propertyName_;
const std::string* interfaceName_{};
const PropertyName& propertyName_;
const InterfaceName* interfaceName_{};
Variant value_;
};
@@ -203,7 +225,11 @@ namespace sdbus {
{
public:
AllPropertiesGetter(IProxy& proxy);
std::map<std::string, Variant> onInterface(const std::string& interfaceName);
std::map<PropertyName, Variant> onInterface(const InterfaceName& interfaceName);
std::map<PropertyName, Variant> onInterface(const std::string& interfaceName);
private:
static inline const InterfaceName DBUS_PROPERTIES_INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
private:
IProxy& proxy_;
@@ -213,13 +239,17 @@ namespace sdbus {
{
public:
AsyncAllPropertiesGetter(IProxy& proxy);
AsyncAllPropertiesGetter& onInterface(const InterfaceName& interfaceName);
AsyncAllPropertiesGetter& onInterface(const std::string& interfaceName);
template <typename _Function> PendingAsyncCall uponReplyInvoke(_Function&& callback);
std::future<std::map<std::string, Variant>> getResultAsFuture();
std::future<std::map<PropertyName, Variant>> getResultAsFuture();
private:
static inline const InterfaceName DBUS_PROPERTIES_INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
private:
IProxy& proxy_;
const std::string* interfaceName_{};
const InterfaceName* interfaceName_{};
};
} // namespace sdbus
+112 -34
View File
@@ -53,21 +53,31 @@ namespace sdbus {
{
}
inline void VTableAdder::forInterface(std::string interfaceName)
inline void VTableAdder::forInterface(InterfaceName interfaceName)
{
object_.addVTable(std::move(interfaceName), std::move(vtable_));
}
[[nodiscard]] inline Slot VTableAdder::forInterface(std::string interfaceName, return_slot_t)
inline void VTableAdder::forInterface(std::string interfaceName)
{
forInterface(InterfaceName{std::move(interfaceName)});
}
[[nodiscard]] inline Slot VTableAdder::forInterface(InterfaceName interfaceName, return_slot_t)
{
return object_.addVTable(std::move(interfaceName), std::move(vtable_), return_slot);
}
[[nodiscard]] inline Slot VTableAdder::forInterface(std::string interfaceName, return_slot_t)
{
return forInterface(InterfaceName{std::move(interfaceName)}, return_slot);
}
/*** ------------- ***/
/*** SignalEmitter ***/
/*** ------------- ***/
inline SignalEmitter::SignalEmitter(IObject& object, const std::string& signalName)
inline SignalEmitter::SignalEmitter(IObject& object, const SignalName& signalName)
: object_(object)
, signalName_(signalName)
, exceptions_(std::uncaught_exceptions())
@@ -92,13 +102,20 @@ namespace sdbus {
object_.emitSignal(signal_);
}
inline SignalEmitter& SignalEmitter::onInterface(const std::string& interfaceName)
inline SignalEmitter& SignalEmitter::onInterface(const InterfaceName& interfaceName)
{
signal_ = object_.createSignal(interfaceName, signalName_);
return *this;
}
inline SignalEmitter& SignalEmitter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
template <typename... _Args>
inline void SignalEmitter::withArguments(_Args&&... args)
{
@@ -111,7 +128,7 @@ namespace sdbus {
/*** MethodInvoker ***/
/*** ------------- ***/
inline MethodInvoker::MethodInvoker(IProxy& proxy, const std::string& methodName)
inline MethodInvoker::MethodInvoker(IProxy& proxy, const MethodName& methodName)
: proxy_(proxy)
, methodName_(methodName)
, exceptions_(std::uncaught_exceptions())
@@ -137,13 +154,20 @@ namespace sdbus {
proxy_.callMethod(method_, timeout_);
}
inline MethodInvoker& MethodInvoker::onInterface(const std::string& interfaceName)
inline MethodInvoker& MethodInvoker::onInterface(const InterfaceName& interfaceName)
{
method_ = proxy_.createMethodCall(interfaceName, methodName_);
return *this;
}
inline MethodInvoker& MethodInvoker::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
inline MethodInvoker& MethodInvoker::withTimeout(uint64_t usec)
{
timeout_ = usec;
@@ -190,19 +214,26 @@ namespace sdbus {
/*** AsyncMethodInvoker ***/
/*** ------------------ ***/
inline AsyncMethodInvoker::AsyncMethodInvoker(IProxy& proxy, const std::string& methodName)
inline AsyncMethodInvoker::AsyncMethodInvoker(IProxy& proxy, const MethodName& methodName)
: proxy_(proxy)
, methodName_(methodName)
{
}
inline AsyncMethodInvoker& AsyncMethodInvoker::onInterface(const std::string& interfaceName)
inline AsyncMethodInvoker& AsyncMethodInvoker::onInterface(const InterfaceName& interfaceName)
{
method_ = proxy_.createMethodCall(interfaceName, methodName_);
return *this;
}
inline AsyncMethodInvoker& AsyncMethodInvoker::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
inline AsyncMethodInvoker& AsyncMethodInvoker::withTimeout(uint64_t usec)
{
timeout_ = usec;
@@ -288,13 +319,18 @@ namespace sdbus {
/*** SignalSubscriber ***/
/*** ---------------- ***/
inline SignalSubscriber::SignalSubscriber(IProxy& proxy, const std::string& signalName)
inline SignalSubscriber::SignalSubscriber(IProxy& proxy, const SignalName& signalName)
: proxy_(proxy)
, signalName_(signalName)
{
}
inline SignalSubscriber& SignalSubscriber::onInterface(std::string interfaceName)
{
return onInterface(InterfaceName{std::move(interfaceName)});
}
inline SignalSubscriber& SignalSubscriber::onInterface(InterfaceName interfaceName)
{
interfaceName_ = std::move(interfaceName);
@@ -368,39 +404,53 @@ namespace sdbus {
/*** PropertyGetter ***/
/*** -------------- ***/
inline PropertyGetter::PropertyGetter(IProxy& proxy, const std::string& propertyName)
inline PropertyGetter::PropertyGetter(IProxy& proxy, const PropertyName& propertyName)
: proxy_(proxy)
, propertyName_(propertyName)
{
}
inline Variant PropertyGetter::onInterface(const std::string& interfaceName)
inline Variant PropertyGetter::onInterface(const InterfaceName& interfaceName)
{
Variant var;
proxy_.callMethod("Get")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(interfaceName, propertyName_)
.storeResultsTo(var);
return var;
}
inline Variant PropertyGetter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
/*** ------------------- ***/
/*** AsyncPropertyGetter ***/
/*** ------------------- ***/
inline AsyncPropertyGetter::AsyncPropertyGetter(IProxy& proxy, const std::string& propertyName)
inline AsyncPropertyGetter::AsyncPropertyGetter(IProxy& proxy, const PropertyName& propertyName)
: proxy_(proxy)
, propertyName_(propertyName)
{
}
inline AsyncPropertyGetter& AsyncPropertyGetter::onInterface(const std::string& interfaceName)
inline AsyncPropertyGetter& AsyncPropertyGetter::onInterface(const InterfaceName& interfaceName)
{
interfaceName_ = &interfaceName;
return *this;
}
inline AsyncPropertyGetter& AsyncPropertyGetter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
template <typename _Function>
PendingAsyncCall AsyncPropertyGetter::uponReplyInvoke(_Function&& callback)
{
@@ -409,7 +459,7 @@ namespace sdbus {
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
return proxy_.callMethodAsync("Get")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_, propertyName_)
.uponReplyInvoke(std::forward<_Function>(callback));
}
@@ -419,7 +469,7 @@ namespace sdbus {
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
return proxy_.callMethodAsync("Get")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_, propertyName_)
.getResultAsFuture<Variant>();
}
@@ -428,19 +478,26 @@ namespace sdbus {
/*** PropertySetter ***/
/*** -------------- ***/
inline PropertySetter::PropertySetter(IProxy& proxy, const std::string& propertyName)
inline PropertySetter::PropertySetter(IProxy& proxy, const PropertyName& propertyName)
: proxy_(proxy)
, propertyName_(propertyName)
{
}
inline PropertySetter& PropertySetter::onInterface(const std::string& interfaceName)
inline PropertySetter& PropertySetter::onInterface(const InterfaceName& interfaceName)
{
interfaceName_ = &interfaceName;
return *this;
}
inline PropertySetter& PropertySetter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
template <typename _Value>
inline void PropertySetter::toValue(const _Value& value)
{
@@ -458,7 +515,7 @@ namespace sdbus {
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
proxy_.callMethod("Set")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_, propertyName_, value);
}
@@ -467,7 +524,7 @@ namespace sdbus {
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
proxy_.callMethod("Set")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_, propertyName_, value)
.dontExpectReply();
}
@@ -476,19 +533,26 @@ namespace sdbus {
/*** AsyncPropertySetter ***/
/*** ------------------- ***/
inline AsyncPropertySetter::AsyncPropertySetter(IProxy& proxy, const std::string& propertyName)
inline AsyncPropertySetter::AsyncPropertySetter(IProxy& proxy, const PropertyName& propertyName)
: proxy_(proxy)
, propertyName_(propertyName)
{
}
inline AsyncPropertySetter& AsyncPropertySetter::onInterface(const std::string& interfaceName)
inline AsyncPropertySetter& AsyncPropertySetter::onInterface(const InterfaceName& interfaceName)
{
interfaceName_ = &interfaceName;
return *this;
}
inline AsyncPropertySetter& AsyncPropertySetter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
template <typename _Value>
inline AsyncPropertySetter& AsyncPropertySetter::toValue(_Value&& value)
{
@@ -510,7 +574,7 @@ namespace sdbus {
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
return proxy_.callMethodAsync("Set")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_, propertyName_, std::move(value_))
.uponReplyInvoke(std::forward<_Function>(callback));
}
@@ -520,7 +584,7 @@ namespace sdbus {
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
return proxy_.callMethodAsync("Set")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_, propertyName_, std::move(value_))
.getResultAsFuture<>();
}
@@ -534,16 +598,23 @@ namespace sdbus {
{
}
inline std::map<std::string, Variant> AllPropertiesGetter::onInterface(const std::string& interfaceName)
inline std::map<PropertyName, Variant> AllPropertiesGetter::onInterface(const InterfaceName& interfaceName)
{
std::map<std::string, Variant> props;
std::map<PropertyName, Variant> props;
proxy_.callMethod("GetAll")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(interfaceName)
.storeResultsTo(props);
return props;
}
inline std::map<PropertyName, Variant> AllPropertiesGetter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
/*** ------------------------ ***/
/*** AsyncAllPropertiesGetter ***/
/*** ------------------------ ***/
@@ -553,35 +624,42 @@ namespace sdbus {
{
}
inline AsyncAllPropertiesGetter& AsyncAllPropertiesGetter::onInterface(const std::string& interfaceName)
inline AsyncAllPropertiesGetter& AsyncAllPropertiesGetter::onInterface(const InterfaceName& interfaceName)
{
interfaceName_ = &interfaceName;
return *this;
}
inline AsyncAllPropertiesGetter& AsyncAllPropertiesGetter::onInterface(const std::string& interfaceName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(interfaceName) == sizeof(InterfaceName));
return onInterface(static_cast<const InterfaceName&>(interfaceName));
}
template <typename _Function>
PendingAsyncCall AsyncAllPropertiesGetter::uponReplyInvoke(_Function&& callback)
{
static_assert( std::is_invocable_r_v<void, _Function, std::optional<Error>, std::map<std::string, Variant>>
, "All properties get callback function must accept std::optional<Error< and a map of property names to their values" );
static_assert( std::is_invocable_r_v<void, _Function, std::optional<Error>, std::map<PropertyName, Variant>>
, "All properties get callback function must accept std::optional<Error> and a map of property names to their values" );
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
return proxy_.callMethodAsync("GetAll")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_)
.uponReplyInvoke(std::forward<_Function>(callback));
}
inline std::future<std::map<std::string, Variant>> AsyncAllPropertiesGetter::getResultAsFuture()
inline std::future<std::map<PropertyName, Variant>> AsyncAllPropertiesGetter::getResultAsFuture()
{
assert(interfaceName_ != nullptr); // onInterface() must be placed/called prior to this function
return proxy_.callMethodAsync("GetAll")
.onInterface("org.freedesktop.DBus.Properties")
.onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
.withArguments(*interfaceName_)
.getResultAsFuture<std::map<std::string, Variant>>();
.getResultAsFuture<std::map<PropertyName, Variant>>();
}
} // namespace sdbus
+24 -9
View File
@@ -43,19 +43,34 @@ namespace sdbus {
: public std::runtime_error
{
public:
explicit Error(const std::string& name, const char* message = nullptr)
: Error(name, std::string(message ? message : ""))
// Strong type representing the D-Bus error name
class Name : public std::string
{
public:
Name() = default;
explicit Name(std::string value)
: std::string(std::move(value))
{}
explicit Name(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
explicit Error(Name name, const char* message = nullptr)
: Error(std::move(name), std::string(message ? message : ""))
{
}
Error(const std::string& name, const std::string& message)
Error(Name name, std::string message)
: std::runtime_error("[" + name + "] " + message)
, name_(name)
, message_(message)
, name_(std::move(name))
, message_(std::move(message))
{
}
[[nodiscard]] const std::string& getName() const
[[nodiscard]] const Name& getName() const
{
return name_;
}
@@ -71,13 +86,13 @@ namespace sdbus {
}
private:
std::string name_;
Name name_;
std::string message_;
};
sdbus::Error createError(int errNo, const std::string& customMsg);
Error createError(int errNo, std::string customMsg);
inline const char* SDBUSCPP_ERROR_NAME = "org.sdbuscpp.Error";
inline const Error::Name SDBUSCPP_ERROR_NAME{"org.sdbuscpp.Error"};
}
#define SDBUS_THROW_ERROR(_MSG, _ERRNO) \
+14 -10
View File
@@ -35,10 +35,14 @@
#include <optional>
#include <string>
// Forward declarations
struct sd_bus;
struct sd_event;
namespace sdbus {
class Message;
class Message;
class ObjectPath;
class BusName;
using ServiceName = BusName;
}
namespace sdbus {
@@ -61,29 +65,29 @@ namespace sdbus {
virtual ~IConnection() = default;
/*!
* @brief Requests D-Bus name on the connection
* @brief Requests a well-known D-Bus service name on a bus
*
* @param[in] name Name to request
*
* @throws sdbus::Error in case of failure
*/
virtual void requestName(const std::string& name) = 0;
virtual void requestName(const ServiceName& name) = 0;
/*!
* @brief Releases D-Bus name on the connection
* @brief Releases an acquired well-known D-Bus service name on a bus
*
* @param[in] name Name to release
*
* @throws sdbus::Error in case of failure
*/
virtual void releaseName(const std::string& name) = 0;
virtual void releaseName(const ServiceName& name) = 0;
/*!
* @brief Retrieves the unique name of a connection. E.g. ":1.xx"
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] virtual std::string getUniqueName() const = 0;
[[nodiscard]] virtual BusName getUniqueName() const = 0;
/*!
* @brief Enters I/O event loop on this bus connection
@@ -257,7 +261,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void addObjectManager(const std::string& objectPath, floating_slot_t) = 0;
virtual void addObjectManager(const ObjectPath& objectPath, floating_slot_t) = 0;
/*!
* @brief Installs a match rule for messages received on this bus connection
@@ -412,7 +416,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] std::unique_ptr<sdbus::IConnection> createBusConnection(const std::string& name);
[[nodiscard]] std::unique_ptr<sdbus::IConnection> createBusConnection(const ServiceName& name);
/*!
* @brief Creates/opens D-Bus system bus connection
@@ -431,7 +435,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] std::unique_ptr<sdbus::IConnection> createSystemBusConnection(const std::string& name);
[[nodiscard]] std::unique_ptr<sdbus::IConnection> createSystemBusConnection(const ServiceName& name);
/*!
* @brief Creates/opens D-Bus session bus connection
@@ -450,7 +454,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] std::unique_ptr<sdbus::IConnection> createSessionBusConnection(const std::string& name);
[[nodiscard]] std::unique_ptr<sdbus::IConnection> createSessionBusConnection(const ServiceName& name);
/*!
* @brief Creates/opens D-Bus session bus connection at a custom address
+42 -12
View File
@@ -41,6 +41,7 @@
namespace sdbus {
class Signal;
class IConnection;
class ObjectPath;
}
namespace sdbus {
@@ -89,7 +90,7 @@ namespace sdbus {
*/
template < typename... VTableItems
, typename = std::enable_if_t<(is_one_of_variants_types<VTableItem, std::decay_t<VTableItems>> && ...)> >
void addVTable(std::string interfaceName, VTableItems&&... items);
void addVTable(InterfaceName interfaceName, VTableItems&&... items);
/*!
* @brief Adds a declaration of methods, properties and signals of the object at a given interface
@@ -114,7 +115,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void addVTable(std::string interfaceName, std::vector<VTableItem> vtable) = 0;
virtual void addVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable) = 0;
/*!
* @brief Adds a declaration of methods, properties and signals of the object at a given interface
@@ -141,7 +142,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] virtual Slot addVTable(std::string interfaceName, std::vector<VTableItem> vtable, return_slot_t) = 0;
[[nodiscard]] virtual Slot addVTable(InterfaceName interfaceName, std::vector<VTableItem> vtable, return_slot_t) = 0;
/*!
* @brief A little more convenient overload of addVTable() above
@@ -188,7 +189,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] virtual Signal createSignal(const std::string& interfaceName, const std::string& signalName) = 0;
[[nodiscard]] virtual Signal createSignal(const InterfaceName& interfaceName, const SignalName& signalName) = 0;
/*!
* @brief Emits signal for this object path
@@ -201,6 +202,28 @@ namespace sdbus {
*/
virtual void emitSignal(const sdbus::Signal& message) = 0;
/*!
* @brief Emits signal on D-Bus
*
* @param[in] signalName Name of the signal
* @return A helper object for convenient emission of signals
*
* This is a high-level, convenience way of emitting D-Bus signals that abstracts
* from the D-Bus message concept. Signal arguments are automatically serialized
* in a message and D-Bus signatures automatically deduced from the provided native arguments.
*
* Example of use:
* @code
* int arg1 = ...;
* double arg2 = ...;
* SignalName fooSignal{"fooSignal"};
* object_.emitSignal(fooSignal).onInterface("com.kistler.foo").withArguments(arg1, arg2);
* @endcode
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] SignalEmitter emitSignal(const SignalName& signalName);
/*!
* @brief Emits signal on D-Bus
*
@@ -230,7 +253,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& propNames) = 0;
virtual void emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& propNames) = 0;
/*!
* @brief Emits PropertyChanged signal for all properties on a given interface of this object path
@@ -239,7 +262,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void emitPropertiesChangedSignal(const std::string& interfaceName) = 0;
virtual void emitPropertiesChangedSignal(const InterfaceName& interfaceName) = 0;
/*!
* @brief Emits InterfacesAdded signal on this object path
@@ -264,7 +287,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces) = 0;
virtual void emitInterfacesAddedSignal(const std::vector<InterfaceName>& interfaces) = 0;
/*!
* @brief Emits InterfacesRemoved signal on this object path
@@ -286,7 +309,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces) = 0;
virtual void emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces) = 0;
/*!
* @brief Adds an ObjectManager interface at the path of this D-Bus object
@@ -322,7 +345,7 @@ namespace sdbus {
/*!
* @brief Returns object path of the underlying DBus object
*/
[[nodiscard]] virtual const std::string& getObjectPath() const = 0;
[[nodiscard]] virtual const ObjectPath& getObjectPath() const = 0;
/*!
* @brief Provides access to the currently processed D-Bus message
@@ -342,13 +365,20 @@ namespace sdbus {
// Out-of-line member definitions
inline SignalEmitter IObject::emitSignal(const std::string& signalName)
inline SignalEmitter IObject::emitSignal(const SignalName& signalName)
{
return SignalEmitter(*this, signalName);
}
inline SignalEmitter IObject::emitSignal(const std::string& signalName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(signalName) == sizeof(SignalName));
return emitSignal(static_cast<const SignalName&>(signalName));
}
template <typename... VTableItems, typename>
void IObject::addVTable(std::string interfaceName, VTableItems&&... items)
void IObject::addVTable(InterfaceName interfaceName, VTableItems&&... items)
{
addVTable(std::move(interfaceName), {std::forward<VTableItems>(items)...});
}
@@ -382,7 +412,7 @@ namespace sdbus {
* auto proxy = sdbus::createObject(connection, "/com/kistler/foo");
* @endcode
*/
[[nodiscard]] std::unique_ptr<sdbus::IObject> createObject(sdbus::IConnection& connection, std::string objectPath);
[[nodiscard]] std::unique_ptr<sdbus::IObject> createObject(sdbus::IConnection& connection, ObjectPath objectPath);
}
+120 -27
View File
@@ -41,6 +41,7 @@ namespace sdbus {
class MethodCall;
class MethodReply;
class IConnection;
class ObjectPath;
class PendingAsyncCall;
namespace internal {
class Proxy;
@@ -81,7 +82,7 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] virtual MethodCall createMethodCall(const std::string& interfaceName, const std::string& methodName) = 0;
[[nodiscard]] virtual MethodCall createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName) = 0;
/*!
* @brief Calls method on the remote D-Bus object
@@ -211,11 +212,17 @@ namespace sdbus {
* Example of use:
* @code
* int result, a = ..., b = ...;
* object_.callMethod("multiply").onInterface(INTERFACE_NAME).withArguments(a, b).storeResultsTo(result);
* MethodName multiply{"multiply"};
* object_.callMethod(multiply).onInterface(INTERFACE_NAME).withArguments(a, b).storeResultsTo(result);
* @endcode
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] MethodInvoker callMethod(const MethodName& methodName);
/*!
* @copydoc IProxy::callMethod(const MethodName&)
*/
[[nodiscard]] MethodInvoker callMethod(const std::string& methodName);
/*!
@@ -232,7 +239,8 @@ namespace sdbus {
* Example of use:
* @code
* int a = ..., b = ...;
* object_.callMethodAsync("multiply").onInterface(INTERFACE_NAME).withArguments(a, b).uponReplyInvoke([](int result)
* MethodName multiply{"multiply"};
* object_.callMethodAsync(multiply).onInterface(INTERFACE_NAME).withArguments(a, b).uponReplyInvoke([](int result)
* {
* std::cout << "Got result of multiplying " << a << " and " << b << ": " << result << std::endl;
* });
@@ -240,6 +248,11 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] AsyncMethodInvoker callMethodAsync(const MethodName& methodName);
/*!
* @copydoc IProxy::callMethodAsync(const MethodName&)
*/
[[nodiscard]] AsyncMethodInvoker callMethodAsync(const std::string& methodName);
/*!
@@ -254,8 +267,8 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
virtual void registerSignalHandler( const std::string& interfaceName
, const std::string& signalName
virtual void registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler ) = 0;
/*!
@@ -273,8 +286,8 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] virtual Slot registerSignalHandler( const std::string& interfaceName
, const std::string& signalName
[[nodiscard]] virtual Slot registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler
, return_slot_t ) = 0;
@@ -294,11 +307,19 @@ namespace sdbus {
*
* Example of use:
* @code
* object_.uponSignal("fooSignal").onInterface("com.kistler.foo").call([this](int arg1, double arg2){ this->onFooSignal(arg1, arg2); });
* object_.uponSignal("stateChanged").onInterface("com.kistler.foo").call([this](int arg1, double arg2){ this->onStateChanged(arg1, arg2); });
* sdbus::InterfaceName foo{"com.kistler.foo"};
* sdbus::SignalName levelChanged{"levelChanged"};
* object_.uponSignal(levelChanged).onInterface(foo).call([this](uint16_t level){ this->onLevelChanged(level); });
* @endcode
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] SignalSubscriber uponSignal(const SignalName& signalName);
/*!
* @copydoc IProxy::uponSignal(const SignalName&)
*/
[[nodiscard]] SignalSubscriber uponSignal(const std::string& signalName);
/*!
@@ -325,10 +346,18 @@ namespace sdbus {
* Example of use:
* @code
* int state = object.getProperty("state").onInterface("com.kistler.foo");
* sdbus::InterfaceName foo{"com.kistler.foo"};
* sdbus::PropertyName level{"level"};
* int level = object.getProperty(level).onInterface(foo);
* @endcode
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] PropertyGetter getProperty(const PropertyName& propertyName);
/*!
* @copydoc IProxy::getProperty(const PropertyName&)
*/
[[nodiscard]] PropertyGetter getProperty(const std::string& propertyName);
/*!
@@ -349,6 +378,11 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] AsyncPropertyGetter getPropertyAsync(const PropertyName& propertyName);
/*!
* @copydoc IProxy::getPropertyAsync(const PropertyName&)
*/
[[nodiscard]] AsyncPropertyGetter getPropertyAsync(const std::string& propertyName);
/*!
@@ -371,6 +405,11 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] PropertySetter setProperty(const PropertyName& propertyName);
/*!
* @copydoc IProxy::setProperty(const PropertyName&)
*/
[[nodiscard]] PropertySetter setProperty(const std::string& propertyName);
/*!
@@ -391,6 +430,11 @@ namespace sdbus {
*
* @throws sdbus::Error in case of failure
*/
[[nodiscard]] AsyncPropertySetter setPropertyAsync(const PropertyName& propertyName);
/*!
* @copydoc IProxy::setPropertyAsync(const PropertyName&)
*/
[[nodiscard]] AsyncPropertySetter setPropertyAsync(const std::string& propertyName);
/*!
@@ -420,7 +464,7 @@ namespace sdbus {
*
* Example of use:
* @code
* auto callback = [](std::optional<sdbus::Error> err, const std::map<std::string, Variant>>& properties){ ... };
* auto callback = [](std::optional<sdbus::Error> err, const std::map<PropertyName, Variant>>& properties){ ... };
* auto props = object.getAllPropertiesAsync().onInterface("com.kistler.foo").uponReplyInvoke(std::move(callback));
* @endcode
*
@@ -438,7 +482,7 @@ namespace sdbus {
/*!
* @brief Returns object path of the underlying DBus object
*/
[[nodiscard]] virtual const std::string& getObjectPath() const = 0;
[[nodiscard]] virtual const ObjectPath& getObjectPath() const = 0;
/*!
* @brief Provides access to the currently processed D-Bus message
@@ -525,41 +569,90 @@ namespace sdbus {
return callMethodAsync(message, microsecs.count(), with_future);
}
inline MethodInvoker IProxy::callMethod(const std::string& methodName)
inline MethodInvoker IProxy::callMethod(const MethodName& methodName)
{
return MethodInvoker(*this, methodName);
}
inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
inline MethodInvoker IProxy::callMethod(const std::string& methodName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(methodName) == sizeof(MethodName));
return callMethod(static_cast<const MethodName&>(methodName));
}
inline AsyncMethodInvoker IProxy::callMethodAsync(const MethodName& methodName)
{
return AsyncMethodInvoker(*this, methodName);
}
inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(methodName) == sizeof(MethodName));
return callMethodAsync(static_cast<const MethodName&>(methodName));
}
inline SignalSubscriber IProxy::uponSignal(const SignalName& signalName)
{
return SignalSubscriber(*this, signalName);
}
inline PropertyGetter IProxy::getProperty(const std::string& propertyName)
inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(signalName) == sizeof(SignalName));
return uponSignal(static_cast<const SignalName&>(signalName));
}
inline PropertyGetter IProxy::getProperty(const PropertyName& propertyName)
{
return PropertyGetter(*this, propertyName);
}
inline AsyncPropertyGetter IProxy::getPropertyAsync(const std::string& propertyName)
inline PropertyGetter IProxy::getProperty(const std::string& propertyName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(propertyName) == sizeof(PropertyName));
return getProperty(static_cast<const PropertyName&>(propertyName));
}
inline AsyncPropertyGetter IProxy::getPropertyAsync(const PropertyName& propertyName)
{
return AsyncPropertyGetter(*this, propertyName);
}
inline PropertySetter IProxy::setProperty(const std::string& propertyName)
inline AsyncPropertyGetter IProxy::getPropertyAsync(const std::string& propertyName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(propertyName) == sizeof(PropertyName));
return getPropertyAsync(static_cast<const PropertyName&>(propertyName));
}
inline PropertySetter IProxy::setProperty(const PropertyName& propertyName)
{
return PropertySetter(*this, propertyName);
}
inline AsyncPropertySetter IProxy::setPropertyAsync(const std::string& propertyName)
inline PropertySetter IProxy::setProperty(const std::string& propertyName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(propertyName) == sizeof(PropertyName));
return setProperty(static_cast<const PropertyName&>(propertyName));
}
inline AsyncPropertySetter IProxy::setPropertyAsync(const PropertyName& propertyName)
{
return AsyncPropertySetter(*this, propertyName);
}
inline AsyncPropertySetter IProxy::setPropertyAsync(const std::string& propertyName)
{
// Down-cast through static cast for performance reasons (no extra copy and object construction needed)
static_assert(sizeof(propertyName) == sizeof(PropertyName));
return setPropertyAsync(static_cast<const PropertyName&>(propertyName));
}
inline AllPropertiesGetter IProxy::getAllProperties()
{
return AllPropertiesGetter(*this);
@@ -593,8 +686,8 @@ namespace sdbus {
* @endcode
*/
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( sdbus::IConnection& connection
, std::string destination
, std::string objectPath );
, ServiceName destination
, ObjectPath objectPath );
/*!
* @brief Creates a proxy object for a specific remote D-Bus object
@@ -619,8 +712,8 @@ namespace sdbus {
* @endcode
*/
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<sdbus::IConnection>&& connection
, std::string destination
, std::string objectPath );
, ServiceName destination
, ObjectPath objectPath );
/*!
* @brief Creates a proxy object for a specific remote D-Bus object
@@ -646,8 +739,8 @@ namespace sdbus {
* @endcode
*/
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<sdbus::IConnection>&& connection
, std::string destination
, std::string objectPath
, ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t );
/*!
@@ -667,8 +760,8 @@ namespace sdbus {
* auto proxy = sdbus::createProxy("com.kistler.foo", "/com/kistler/foo");
* @endcode
*/
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
, std::string objectPath );
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( ServiceName destination
, ObjectPath objectPath );
/*!
* @brief Creates a proxy object for a specific remote D-Bus object
@@ -688,8 +781,8 @@ namespace sdbus {
* auto proxy = sdbus::createProxy("com.kistler.foo", "/com/kistler/foo", sdbus::dont_run_event_loop_thread );
* @endcode
*/
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
, std::string objectPath
[[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t );
}
+10 -6
View File
@@ -52,10 +52,14 @@
namespace sdbus {
class Variant;
class ObjectPath;
class InterfaceName;
class MemberName;
class Signature;
template <typename... _ValueTypes> class Struct;
class UnixFd;
class MethodReply;
class BusName;
using ConnectionName = BusName;
namespace internal {
class ISdBus;
}
@@ -181,11 +185,11 @@ namespace sdbus {
explicit operator bool() const;
void clearFlags();
std::string getInterfaceName() const;
std::string getMemberName() const;
std::string getSender() const;
std::string getPath() const;
std::string getDestination() const;
InterfaceName getInterfaceName() const;
MemberName getMemberName() const;
ConnectionName getSender() const;
ObjectPath getPath() const;
ConnectionName getDestination() const;
void peekType(std::string& type, std::string& contents) const;
bool isValid() const;
bool isEmpty() const;
@@ -281,7 +285,7 @@ namespace sdbus {
public:
Signal() = default;
void setDestination(const std::string& destination);
void setDestination(const ConnectionName& destination);
void send() const;
};
+5 -5
View File
@@ -104,7 +104,7 @@ namespace sdbus {
* This constructor overload creates a proxy that manages its own D-Bus connection(s).
* For more information on its behavior, consult @ref createProxy(std::string,std::string)
*/
ProxyInterfaces(std::string destination, std::string objectPath)
ProxyInterfaces(ServiceName destination, ObjectPath objectPath)
: ProxyObjectHolder(createProxy(std::move(destination), std::move(objectPath)))
, _Interfaces(getProxy())...
{
@@ -119,7 +119,7 @@ namespace sdbus {
* This constructor overload creates a proxy that manages its own D-Bus connection(s).
* For more information on its behavior, consult @ref createProxy(std::string,std::string,sdbus::dont_run_event_loop_thread_t)
*/
ProxyInterfaces(std::string destination, std::string objectPath, dont_run_event_loop_thread_t)
ProxyInterfaces(ServiceName destination, ObjectPath objectPath, dont_run_event_loop_thread_t)
: ProxyObjectHolder(createProxy(std::move(destination), std::move(objectPath), dont_run_event_loop_thread))
, _Interfaces(getProxy())...
{
@@ -135,7 +135,7 @@ namespace sdbus {
* The proxy created this way just references a D-Bus connection owned and managed by the user.
* For more information on its behavior, consult @ref createProxy(IConnection&,std::string,std::string)
*/
ProxyInterfaces(IConnection& connection, std::string destination, std::string objectPath)
ProxyInterfaces(IConnection& connection, ServiceName destination, ObjectPath objectPath)
: ProxyObjectHolder(createProxy(connection, std::move(destination), std::move(objectPath)))
, _Interfaces(getProxy())...
{
@@ -151,7 +151,7 @@ namespace sdbus {
* The proxy created this way becomes an owner of the connection.
* For more information on its behavior, consult @ref createProxy(std::unique_ptr<sdbus::IConnection>&&,std::string,std::string)
*/
ProxyInterfaces(std::unique_ptr<sdbus::IConnection>&& connection, std::string destination, std::string objectPath)
ProxyInterfaces(std::unique_ptr<sdbus::IConnection>&& connection, ServiceName destination, ObjectPath objectPath)
: ProxyObjectHolder(createProxy(std::move(connection), std::move(destination), std::move(objectPath)))
, _Interfaces(getProxy())...
{
@@ -167,7 +167,7 @@ namespace sdbus {
* The proxy created this way becomes an owner of the connection.
* For more information on its behavior, consult @ref createProxy(std::unique_ptr<sdbus::IConnection>&&,std::string,std::string,sdbus::dont_run_event_loop_thread_t)
*/
ProxyInterfaces(std::unique_ptr<sdbus::IConnection>&& connection, std::string destination, std::string objectPath, dont_run_event_loop_thread_t)
ProxyInterfaces(std::unique_ptr<sdbus::IConnection>&& connection, ServiceName destination, ObjectPath objectPath, dont_run_event_loop_thread_t)
: ProxyObjectHolder(createProxy(std::move(connection), std::move(destination), std::move(objectPath), dont_run_event_loop_thread))
, _Interfaces(getProxy())...
{
+70 -32
View File
@@ -39,7 +39,7 @@ namespace sdbus {
// Proxy for peer
class Peer_proxy
{
static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Peer";
static inline const InterfaceName INTERFACE_NAME{"org.freedesktop.DBus.Peer"};
protected:
Peer_proxy(sdbus::IProxy& proxy)
@@ -78,7 +78,7 @@ namespace sdbus {
// Proxy for introspection
class Introspectable_proxy
{
static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
static inline const InterfaceName INTERFACE_NAME{"org.freedesktop.DBus.Introspectable"};
protected:
Introspectable_proxy(sdbus::IProxy& proxy)
@@ -112,7 +112,7 @@ namespace sdbus {
// Proxy for properties
class Properties_proxy
{
static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
static inline const InterfaceName INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
protected:
Properties_proxy(sdbus::IProxy& proxy)
@@ -132,68 +132,106 @@ namespace sdbus {
proxy_
->uponSignal("PropertiesChanged")
.onInterface(INTERFACE_NAME)
.call([this]( const std::string& interfaceName
, const std::map<std::string, sdbus::Variant>& changedProperties
, const std::vector<std::string>& invalidatedProperties )
.call([this]( const InterfaceName& interfaceName
, const std::map<PropertyName, sdbus::Variant>& changedProperties
, const std::vector<PropertyName>& invalidatedProperties )
{
this->onPropertiesChanged(interfaceName, changedProperties, invalidatedProperties);
});
}
virtual void onPropertiesChanged( const std::string& interfaceName
, const std::map<std::string, sdbus::Variant>& changedProperties
, const std::vector<std::string>& invalidatedProperties ) = 0;
virtual void onPropertiesChanged( const InterfaceName& interfaceName
, const std::map<PropertyName, sdbus::Variant>& changedProperties
, const std::vector<PropertyName>& invalidatedProperties ) = 0;
public:
sdbus::Variant Get(const std::string& interfaceName, const std::string& propertyName)
sdbus::Variant Get(const InterfaceName& interfaceName, const PropertyName& propertyName)
{
return proxy_->getProperty(propertyName).onInterface(interfaceName);
}
// TODO: Refactor from std::string to std::string_view before release/v2.0 !!!
sdbus::Variant Get(const InterfaceName& interfaceName, const std::string& propertyName)
{
return proxy_->getProperty(propertyName).onInterface(interfaceName);
}
template <typename _Function>
PendingAsyncCall GetAsync(const std::string& interfaceName, const std::string& propertyName, _Function&& callback)
PendingAsyncCall GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, _Function&& callback)
{
return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
}
std::future<sdbus::Variant> GetAsync(const std::string& interfaceName, const std::string& propertyName, with_future_t)
template <typename _Function>
PendingAsyncCall GetAsync(const InterfaceName& interfaceName, const std::string& propertyName, _Function&& callback)
{
return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
}
std::future<sdbus::Variant> GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, with_future_t)
{
return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
}
void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
std::future<sdbus::Variant> GetAsync(const InterfaceName& interfaceName, const std::string& propertyName, with_future_t)
{
return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
}
void Set(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value)
{
proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value);
}
void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, dont_expect_reply_t)
void Set(const InterfaceName& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
{
proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value);
}
void Set(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, dont_expect_reply_t)
{
proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
}
void Set(const InterfaceName& interfaceName, const std::string& propertyName, const sdbus::Variant& value, dont_expect_reply_t)
{
proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
}
template <typename _Function>
PendingAsyncCall SetAsync(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, _Function&& callback)
PendingAsyncCall SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, _Function&& callback)
{
return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
}
std::future<void> SetAsync(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, with_future_t)
template <typename _Function>
PendingAsyncCall SetAsync(const InterfaceName& interfaceName, const std::string& propertyName, const sdbus::Variant& value, _Function&& callback)
{
return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
}
std::future<void> SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, with_future_t)
{
return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
}
std::map<std::string, sdbus::Variant> GetAll(const std::string& interfaceName)
std::future<void> SetAsync(const InterfaceName& interfaceName, const std::string& propertyName, const sdbus::Variant& value, with_future_t)
{
return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
}
std::map<PropertyName, sdbus::Variant> GetAll(const InterfaceName& interfaceName)
{
return proxy_->getAllProperties().onInterface(interfaceName);
}
template <typename _Function>
PendingAsyncCall GetAllAsync(const std::string& interfaceName, _Function&& callback)
PendingAsyncCall GetAllAsync(const InterfaceName& interfaceName, _Function&& callback)
{
return proxy_->getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
}
std::future<std::map<std::string, sdbus::Variant>> GetAllAsync(const std::string& interfaceName, with_future_t)
std::future<std::map<PropertyName, sdbus::Variant>> GetAllAsync(const InterfaceName& interfaceName, with_future_t)
{
return proxy_->getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
}
@@ -205,7 +243,7 @@ namespace sdbus {
// Proxy for object manager
class ObjectManager_proxy
{
static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
static inline const InterfaceName INTERFACE_NAME{"org.freedesktop.DBus.ObjectManager"};
protected:
ObjectManager_proxy(sdbus::IProxy& proxy)
@@ -226,7 +264,7 @@ namespace sdbus {
->uponSignal("InterfacesAdded")
.onInterface(INTERFACE_NAME)
.call([this]( const sdbus::ObjectPath& objectPath
, const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties )
, const std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>& interfacesAndProperties )
{
this->onInterfacesAdded(objectPath, interfacesAndProperties);
});
@@ -234,21 +272,21 @@ namespace sdbus {
proxy_->uponSignal("InterfacesRemoved")
.onInterface(INTERFACE_NAME)
.call([this]( const sdbus::ObjectPath& objectPath
, const std::vector<std::string>& interfaces )
, const std::vector<sdbus::InterfaceName>& interfaces )
{
this->onInterfacesRemoved(objectPath, interfaces);
});
}
virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
, const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) = 0;
, const std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>& interfacesAndProperties) = 0;
virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
, const std::vector<std::string>& interfaces) = 0;
, const std::vector<sdbus::InterfaceName>& interfaces) = 0;
public:
std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> GetManagedObjects()
std::map<sdbus::ObjectPath, std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>> GetManagedObjects()
{
std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
std::map<sdbus::ObjectPath, std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>> objectsInterfacesAndProperties;
proxy_->callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
return objectsInterfacesAndProperties;
}
@@ -264,7 +302,7 @@ namespace sdbus {
// Adaptor for properties
class Properties_adaptor
{
static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
static inline const InterfaceName INTERFACE_NAME{"org.freedesktop.DBus.Properties"};
protected:
Properties_adaptor(sdbus::IObject& object) : object_(&object)
@@ -283,12 +321,12 @@ namespace sdbus {
}
public:
void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& properties)
void emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& properties)
{
object_->emitPropertiesChangedSignal(interfaceName, properties);
}
void emitPropertiesChangedSignal(const std::string& interfaceName)
void emitPropertiesChangedSignal(const InterfaceName& interfaceName)
{
object_->emitPropertiesChangedSignal(interfaceName);
}
@@ -309,7 +347,7 @@ namespace sdbus {
*/
class ObjectManager_adaptor
{
static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
static inline const InterfaceName INTERFACE_NAME{"org.freedesktop.DBus.ObjectManager"};
protected:
explicit ObjectManager_adaptor(sdbus::IObject& object) : object_(&object)
@@ -378,7 +416,7 @@ namespace sdbus {
*
* See IObject::emitInterfacesAddedSignal().
*/
void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
void emitInterfacesAddedSignal(const std::vector<sdbus::InterfaceName>& interfaces)
{
object_->emitInterfacesAddedSignal(interfaces);
}
@@ -398,7 +436,7 @@ namespace sdbus {
*
* See IObject::emitInterfacesRemovedSignal().
*/
void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
void emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces)
{
object_->emitInterfacesRemovedSignal(interfaces);
}
+18
View File
@@ -54,6 +54,9 @@ namespace sdbus {
class ObjectPath;
class Signature;
class UnixFd;
class BusName;
class InterfaceName;
class MemberName;
class MethodCall;
class MethodReply;
class Signal;
@@ -309,6 +312,21 @@ namespace sdbus {
}
};
template <>
struct signature_of<BusName> : signature_of<std::string>
{
};
template <>
struct signature_of<InterfaceName> : signature_of<std::string>
{
};
template <>
struct signature_of<MemberName> : signature_of<std::string>
{
};
template <typename... _ValueTypes>
struct signature_of<Struct<_ValueTypes...>>
{
+83 -18
View File
@@ -171,42 +171,107 @@ namespace sdbus {
/********************************************//**
* @class ObjectPath
*
* Representation of object path D-Bus type
* Strong type representing the D-Bus object path
*
***********************************************/
class ObjectPath : public std::string
{
public:
using std::string::string;
ObjectPath() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
ObjectPath(const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
ObjectPath(ObjectPath&&) = default; // Enable move - user-declared copy ctor prevents implicit creation
ObjectPath& operator = (const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
ObjectPath& operator = (ObjectPath&&) = default; // Enable move - user-declared copy assign prevents implicit creation
ObjectPath(std::string path)
: std::string(std::move(path))
ObjectPath() = default;
explicit ObjectPath(std::string value)
: std::string(std::move(value))
{}
explicit ObjectPath(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
/********************************************//**
* @class BusName
*
* Strong type representing the D-Bus bus/service/connection name
*
***********************************************/
class BusName : public std::string
{
public:
BusName() = default;
explicit BusName(std::string value)
: std::string(std::move(value))
{}
explicit BusName(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
using ServiceName = BusName;
using ConnectionName = BusName;
/********************************************//**
* @class InterfaceName
*
* Strong type representing the D-Bus interface name
*
***********************************************/
class InterfaceName : public std::string
{
public:
InterfaceName() = default;
explicit InterfaceName(std::string value)
: std::string(std::move(value))
{}
explicit InterfaceName(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
/********************************************//**
* @class MemberName
*
* Strong type representing the D-Bus member name
*
***********************************************/
class MemberName : public std::string
{
public:
MemberName() = default;
explicit MemberName(std::string value)
: std::string(std::move(value))
{}
explicit MemberName(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
using MethodName = MemberName;
using SignalName = MemberName;
using PropertyName = MemberName;
/********************************************//**
* @class Signature
*
* Representation of Signature D-Bus type
* Strong type representing the D-Bus object path
*
***********************************************/
class Signature : public std::string
{
public:
using std::string::string;
Signature() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
Signature(const Signature&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
Signature(Signature&&) = default; // Enable move - user-declared copy ctor prevents implicit creation
Signature& operator = (const Signature&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
Signature& operator = (Signature&&) = default; // Enable move - user-declared copy assign prevents implicit creation
Signature(std::string path)
: std::string(std::move(path))
Signature() = default;
explicit Signature(std::string value)
: std::string(std::move(value))
{}
explicit Signature(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
+11 -7
View File
@@ -27,6 +27,7 @@
#define SDBUS_CXX_VTABLEITEMS_H_
#include <sdbus-c++/Flags.h>
#include <sdbus-c++/Types.h>
#include <sdbus-c++/TypeTraits.h>
#include <string>
@@ -46,15 +47,16 @@ namespace sdbus {
MethodVTableItem& markAsPrivileged();
MethodVTableItem& withNoReply();
std::string name;
std::string inputSignature;
MethodName name;
Signature inputSignature;
std::vector<std::string> inputParamNames;
std::string outputSignature;
Signature outputSignature;
std::vector<std::string> outputParamNames;
method_callback callbackHandler;
Flags flags;
};
MethodVTableItem registerMethod(MethodName methodName);
MethodVTableItem registerMethod(std::string methodName);
struct SignalVTableItem
@@ -64,12 +66,13 @@ namespace sdbus {
template <typename... _Args, typename... _String> SignalVTableItem& withParameters(_String... names);
SignalVTableItem& markAsDeprecated();
std::string name;
std::string signature;
SignalName name;
Signature signature;
std::vector<std::string> paramNames;
Flags flags;
};
SignalVTableItem registerSignal(SignalName signalName);
SignalVTableItem registerSignal(std::string signalName);
struct PropertyVTableItem
@@ -80,13 +83,14 @@ namespace sdbus {
PropertyVTableItem& markAsPrivileged();
PropertyVTableItem& withUpdateBehavior(Flags::PropertyUpdateBehaviorFlags behavior);
std::string name;
std::string signature;
PropertyName name;
Signature signature;
property_get_callback getter;
property_set_callback setter;
Flags flags;
};
PropertyVTableItem registerProperty(PropertyName propertyName);
PropertyVTableItem registerProperty(std::string propertyName);
struct InterfaceFlagsVTableItem
+18 -3
View File
@@ -125,11 +125,16 @@ namespace sdbus {
return *this;
}
inline MethodVTableItem registerMethod(std::string methodName)
inline MethodVTableItem registerMethod(MethodName methodName)
{
return {std::move(methodName), {}, {}, {}, {}, {}, {}};
}
inline MethodVTableItem registerMethod(std::string methodName)
{
return registerMethod(MethodName{std::move(methodName)});
}
/*** -------------------- ***/
/*** Signal VTable Item ***/
/*** -------------------- ***/
@@ -166,11 +171,16 @@ namespace sdbus {
return *this;
}
inline SignalVTableItem registerSignal(std::string signalName)
inline SignalVTableItem registerSignal(SignalName signalName)
{
return {std::move(signalName), {}, {}, {}};
}
inline SignalVTableItem registerSignal(std::string signalName)
{
return registerSignal(SignalName{std::move(signalName)});
}
/*** -------------------- ***/
/*** Property VTable Item ***/
/*** -------------------- ***/
@@ -239,11 +249,16 @@ namespace sdbus {
return *this;
}
inline PropertyVTableItem registerProperty(std::string propertyName)
inline PropertyVTableItem registerProperty(PropertyName propertyName)
{
return {std::move(propertyName), {}, {}, {}, {}};
}
inline PropertyVTableItem registerProperty(std::string propertyName)
{
return registerProperty(PropertyName{std::move(propertyName)});
}
/*** --------------------------- ***/
/*** Interface Flags VTable Item ***/
/*** --------------------------- ***/