forked from Kistler-Group/sdbus-cpp
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:
@@ -17,7 +17,7 @@ namespace ExampleManager {
|
||||
class Planet1_proxy
|
||||
{
|
||||
public:
|
||||
static constexpr const char* INTERFACE_NAME = "org.sdbuscpp.ExampleManager.Planet1";
|
||||
static inline const sdbus::InterfaceName INTERFACE_NAME{"org.sdbuscpp.ExampleManager.Planet1"};
|
||||
|
||||
protected:
|
||||
Planet1_proxy(sdbus::IProxy& proxy)
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace ExampleManager {
|
||||
class Planet1_adaptor
|
||||
{
|
||||
public:
|
||||
static constexpr const char* INTERFACE_NAME = "org.sdbuscpp.ExampleManager.Planet1";
|
||||
static inline const sdbus::InterfaceName INTERFACE_NAME{"org.sdbuscpp.ExampleManager.Planet1"};
|
||||
|
||||
protected:
|
||||
Planet1_adaptor(sdbus::IObject& object)
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
class PlanetProxy final : public sdbus::ProxyInterfaces< org::sdbuscpp::ExampleManager::Planet1_proxy >
|
||||
{
|
||||
public:
|
||||
PlanetProxy(sdbus::IConnection& connection, std::string destination, std::string path)
|
||||
PlanetProxy(sdbus::IConnection& connection, sdbus::ServiceName destination, sdbus::ObjectPath path)
|
||||
: ProxyInterfaces(connection, std::move(destination), std::move(path))
|
||||
{
|
||||
registerProxy();
|
||||
@@ -29,13 +29,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class ManagerProxy final : public sdbus::ProxyInterfaces< sdbus::ObjectManager_proxy >
|
||||
class ManagerProxy final : public sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy>
|
||||
{
|
||||
public:
|
||||
ManagerProxy(sdbus::IConnection& connection, const std::string& destination, std::string path)
|
||||
: ProxyInterfaces(connection, destination, std::move(path))
|
||||
, m_connection(connection)
|
||||
, m_destination(destination)
|
||||
ManagerProxy(sdbus::IConnection& connection, sdbus::ServiceName destination, sdbus::ObjectPath path)
|
||||
: ProxyInterfaces(connection, destination, std::move(path))
|
||||
, m_connection(connection)
|
||||
, m_destination(destination)
|
||||
{
|
||||
registerProxy();
|
||||
}
|
||||
@@ -47,8 +47,7 @@ public:
|
||||
|
||||
void handleExistingObjects()
|
||||
{
|
||||
std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
|
||||
objectsInterfacesAndProperties = GetManagedObjects();
|
||||
auto objectsInterfacesAndProperties = GetManagedObjects();
|
||||
for (const auto& [object, interfacesAndProperties] : objectsInterfacesAndProperties) {
|
||||
onInterfacesAdded(object, interfacesAndProperties);
|
||||
}
|
||||
@@ -56,7 +55,7 @@ public:
|
||||
|
||||
private:
|
||||
void onInterfacesAdded( const sdbus::ObjectPath& objectPath
|
||||
, const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) override
|
||||
, const std::map<sdbus::InterfaceName, std::map<sdbus::PropertyName, sdbus::Variant>>& interfacesAndProperties) override
|
||||
{
|
||||
std::cout << objectPath << " added:\t";
|
||||
for (const auto& [interface, _] : interfacesAndProperties) {
|
||||
@@ -71,14 +70,14 @@ private:
|
||||
}
|
||||
const auto& properties = planetInterface->second;
|
||||
// get a property which was passed as part of the signal.
|
||||
const auto& name = properties.at("Name").get<std::string>();
|
||||
const auto& name = properties.at(sdbus::PropertyName{"Name"}).get<std::string>();
|
||||
// or create a proxy instance to the newly added object.
|
||||
PlanetProxy planet(m_connection, m_destination, objectPath);
|
||||
std::cout << name << " has a population of " << planet.GetPopulation() << ".\n" << std::endl;
|
||||
}
|
||||
|
||||
void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
|
||||
, const std::vector<std::string>& interfaces) override
|
||||
, const std::vector<sdbus::InterfaceName>& interfaces) override
|
||||
{
|
||||
std::cout << objectPath << " removed:\t";
|
||||
for (const auto& interface : interfaces) {
|
||||
@@ -88,14 +87,16 @@ private:
|
||||
}
|
||||
|
||||
sdbus::IConnection& m_connection;
|
||||
std::string m_destination;
|
||||
sdbus::ServiceName m_destination;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
auto connection = sdbus::createSessionBusConnection();
|
||||
|
||||
auto managerProxy = std::make_unique<ManagerProxy>(*connection, "org.sdbuscpp.examplemanager", "/org/sdbuscpp/examplemanager");
|
||||
sdbus::ServiceName destination{"org.sdbuscpp.examplemanager"};
|
||||
sdbus::ObjectPath objectPath{"/org/sdbuscpp/examplemanager"};
|
||||
auto managerProxy = std::make_unique<ManagerProxy>(*connection, std::move(destination), std::move(objectPath));
|
||||
try {
|
||||
managerProxy->handleExistingObjects();
|
||||
}
|
||||
@@ -107,4 +108,4 @@ int main()
|
||||
|
||||
connection->enterEventLoop();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,12 @@
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using sdbus::ObjectPath;
|
||||
|
||||
class ManagerAdaptor : public sdbus::AdaptorInterfaces<sdbus::ObjectManager_adaptor>
|
||||
{
|
||||
public:
|
||||
ManagerAdaptor(sdbus::IConnection& connection, std::string path)
|
||||
ManagerAdaptor(sdbus::IConnection& connection, sdbus::ObjectPath path)
|
||||
: AdaptorInterfaces(connection, std::move(path))
|
||||
{
|
||||
registerAdaptor();
|
||||
@@ -39,7 +41,7 @@ class PlanetAdaptor final : public sdbus::AdaptorInterfaces< org::sdbuscpp::Exam
|
||||
, sdbus::Properties_adaptor >
|
||||
{
|
||||
public:
|
||||
PlanetAdaptor(sdbus::IConnection& connection, std::string path, std::string name, uint64_t population)
|
||||
PlanetAdaptor(sdbus::IConnection& connection, sdbus::ObjectPath path, std::string name, uint64_t population)
|
||||
: AdaptorInterfaces(connection, std::move(path))
|
||||
, m_name(std::move(name))
|
||||
, m_population(population)
|
||||
@@ -82,18 +84,19 @@ void printCountDown(const std::string& message, int seconds)
|
||||
int main()
|
||||
{
|
||||
auto connection = sdbus::createSessionBusConnection();
|
||||
connection->requestName("org.sdbuscpp.examplemanager");
|
||||
sdbus::ServiceName serviceName{"org.sdbuscpp.examplemanager"};
|
||||
connection->requestName(serviceName);
|
||||
connection->enterEventLoopAsync();
|
||||
|
||||
auto manager = std::make_unique<ManagerAdaptor>(*connection, "/org/sdbuscpp/examplemanager");
|
||||
auto manager = std::make_unique<ManagerAdaptor>(*connection, ObjectPath{"/org/sdbuscpp/examplemanager"});
|
||||
while (true)
|
||||
{
|
||||
printCountDown("Creating PlanetAdaptor in ", 5);
|
||||
auto earth = std::make_unique<PlanetAdaptor>(*connection, "/org/sdbuscpp/examplemanager/Planet1/Earth", "Earth", 7'874'965'825);
|
||||
auto earth = std::make_unique<PlanetAdaptor>(*connection, ObjectPath{"/org/sdbuscpp/examplemanager/Planet1/Earth"}, "Earth", 7'874'965'825);
|
||||
printCountDown("Creating PlanetAdaptor in ", 5);
|
||||
auto trantor = std::make_unique<PlanetAdaptor>(*connection, "/org/sdbuscpp/examplemanager/Planet1/Trantor", "Trantor", 40'000'000'000);
|
||||
auto trantor = std::make_unique<PlanetAdaptor>(*connection, ObjectPath{"/org/sdbuscpp/examplemanager/Planet1/Trantor"}, "Trantor", 40'000'000'000);
|
||||
printCountDown("Creating PlanetAdaptor in ", 5);
|
||||
auto laconia = std::make_unique<PlanetAdaptor>(*connection, "/org/sdbuscpp/examplemanager/Planet1/Laconia", "Laconia", 231'721);
|
||||
auto laconia = std::make_unique<PlanetAdaptor>(*connection, ObjectPath{"/org/sdbuscpp/examplemanager/Planet1/Laconia"}, "Laconia", 231'721);
|
||||
printCountDown("Removing PlanetAdaptor in ", 5);
|
||||
earth.reset();
|
||||
printCountDown("Removing PlanetAdaptor in ", 5);
|
||||
@@ -102,7 +105,7 @@ int main()
|
||||
laconia.reset();
|
||||
}
|
||||
|
||||
connection->releaseName("org.sdbuscpp.examplemanager");
|
||||
connection->releaseName(serviceName);
|
||||
connection->leaveEventLoop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user