sdbus-c++: include/sdbus-c++/StandardInterfaces.h Source File

sdbus-c++ 1.4.0 High-level C++ D-Bus library based on systemd D-Bus implementation

Loading...
Searching...
No Matches
StandardInterfaces.h
Go to the documentation of this file.
1
27#ifndef SDBUS_CXX_STANDARDINTERFACES_H_
28#define SDBUS_CXX_STANDARDINTERFACES_H_
29
30#include <sdbus-c++/IObject.h>
31#include <sdbus-c++/IProxy.h>
32#include <sdbus-c++/Types.h>
33#include <string>
34#include <map>
35#include <vector>
36
37namespace sdbus {
38
39 // Proxy for peer
40 class Peer_proxy
41 {
42 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Peer";
43
44 protected:
45 Peer_proxy(sdbus::IProxy& proxy)
46 : proxy_(&proxy)
47 {
48 }
49
50 Peer_proxy(const Peer_proxy&) = delete;
51 Peer_proxy& operator=(const Peer_proxy&) = delete;
52 Peer_proxy(Peer_proxy&&) = default;
53 Peer_proxy& operator=(Peer_proxy&&) = default;
54
55 ~Peer_proxy() = default;
56
57 public:
58 void Ping()
59 {
60 proxy_->callMethod("Ping").onInterface(INTERFACE_NAME);
61 }
62
63 std::string GetMachineId()
64 {
65 std::string machineUUID;
66 proxy_->callMethod("GetMachineId").onInterface(INTERFACE_NAME).storeResultsTo(machineUUID);
67 return machineUUID;
68 }
69
70 private:
71 sdbus::IProxy* proxy_;
72 };
73
74 // Proxy for introspection
75 class Introspectable_proxy
76 {
77 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
78
79 protected:
80 Introspectable_proxy(sdbus::IProxy& proxy)
81 : proxy_(&proxy)
82 {
83 }
84
85 Introspectable_proxy(const Introspectable_proxy&) = delete;
86 Introspectable_proxy& operator=(const Introspectable_proxy&) = delete;
87 Introspectable_proxy(Introspectable_proxy&&) = default;
88 Introspectable_proxy& operator=(Introspectable_proxy&&) = default;
89
90 ~Introspectable_proxy() = default;
91
92 public:
93 std::string Introspect()
94 {
95 std::string xml;
96 proxy_->callMethod("Introspect").onInterface(INTERFACE_NAME).storeResultsTo(xml);
97 return xml;
98 }
99
100 private:
101 sdbus::IProxy* proxy_;
102 };
103
104 // Proxy for properties
105 class Properties_proxy
106 {
107 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
108
109 protected:
110 Properties_proxy(sdbus::IProxy& proxy)
111 : proxy_(&proxy)
112 {
113 proxy_
114 ->uponSignal("PropertiesChanged")
115 .onInterface(INTERFACE_NAME)
116 .call([this]( const std::string& interfaceName
117 , const std::map<std::string, sdbus::Variant>& changedProperties
118 , const std::vector<std::string>& invalidatedProperties )
119 {
120 this->onPropertiesChanged(interfaceName, changedProperties, invalidatedProperties);
121 });
122 }
123
124 Properties_proxy(const Properties_proxy&) = delete;
125 Properties_proxy& operator=(const Properties_proxy&) = delete;
126 Properties_proxy(Properties_proxy&&) = default;
127 Properties_proxy& operator=(Properties_proxy&&) = default;
128
129 ~Properties_proxy() = default;
130
131 virtual void onPropertiesChanged( const std::string& interfaceName
132 , const std::map<std::string, sdbus::Variant>& changedProperties
133 , const std::vector<std::string>& invalidatedProperties ) = 0;
134
135 public:
136 sdbus::Variant Get(const std::string& interfaceName, const std::string& propertyName)
137 {
138 return proxy_->getProperty(propertyName).onInterface(interfaceName);
139 }
140
141 template <typename _Function>
142 PendingAsyncCall GetAsync(const std::string& interfaceName, const std::string& propertyName, _Function&& callback)
143 {
144 return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
145 }
146
147 std::future<sdbus::Variant> GetAsync(const std::string& interfaceName, const std::string& propertyName, with_future_t)
148 {
149 return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
150 }
151
152 void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
153 {
154 proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value);
155 }
156
157 void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, dont_expect_reply_t)
158 {
159 proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
160 }
161
162 template <typename _Function>
163 PendingAsyncCall SetAsync(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, _Function&& callback)
164 {
165 return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
166 }
167
168 std::future<void> SetAsync(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, with_future_t)
169 {
170 return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
171 }
172
173 std::map<std::string, sdbus::Variant> GetAll(const std::string& interfaceName)
174 {
175 return proxy_->getAllProperties().onInterface(interfaceName);
176 }
177
178 template <typename _Function>
179 PendingAsyncCall GetAllAsync(const std::string& interfaceName, _Function&& callback)
180 {
181 return proxy_->getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
182 }
183
184 std::future<std::map<std::string, sdbus::Variant>> GetAllAsync(const std::string& interfaceName, with_future_t)
185 {
186 return proxy_->getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
187 }
188
189 private:
190 sdbus::IProxy* proxy_;
191 };
192
193 // Proxy for object manager
194 class ObjectManager_proxy
195 {
196 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
197
198 protected:
199 ObjectManager_proxy(sdbus::IProxy& proxy)
200 : proxy_(&proxy)
201 {
202 proxy_
203 ->uponSignal("InterfacesAdded")
204 .onInterface(INTERFACE_NAME)
205 .call([this]( const sdbus::ObjectPath& objectPath
206 , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties )
207 {
208 this->onInterfacesAdded(objectPath, interfacesAndProperties);
209 });
210
211 proxy_->uponSignal("InterfacesRemoved")
212 .onInterface(INTERFACE_NAME)
213 .call([this]( const sdbus::ObjectPath& objectPath
214 , const std::vector<std::string>& interfaces )
215 {
216 this->onInterfacesRemoved(objectPath, interfaces);
217 });
218 }
219
220 ObjectManager_proxy(const ObjectManager_proxy&) = delete;
221 ObjectManager_proxy& operator=(const ObjectManager_proxy&) = delete;
222 ObjectManager_proxy(ObjectManager_proxy&&) = default;
223 ObjectManager_proxy& operator=(ObjectManager_proxy&&) = default;
224
225 ~ObjectManager_proxy() = default;
226
227 virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
228 , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) = 0;
229 virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
230 , const std::vector<std::string>& interfaces) = 0;
231
232 public:
233 std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> GetManagedObjects()
234 {
235 std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
236 proxy_->callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
237 return objectsInterfacesAndProperties;
238 }
239
240 private:
241 sdbus::IProxy* proxy_;
242 };
243
244 // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
245 // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
246 // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
247
248 // Adaptor for properties
249 class Properties_adaptor
250 {
251 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
252
253 protected:
254 Properties_adaptor(sdbus::IObject& object)
255 : object_(&object)
256 {
257 }
258
259 Properties_adaptor(const Properties_adaptor&) = delete;
260 Properties_adaptor& operator=(const Properties_adaptor&) = delete;
261 Properties_adaptor(Properties_adaptor&&) = default;
262 Properties_adaptor& operator=(Properties_adaptor&&) = default;
263
264 ~Properties_adaptor() = default;
265
266 public:
267 void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& properties)
268 {
269 object_->emitPropertiesChangedSignal(interfaceName, properties);
270 }
271
272 void emitPropertiesChangedSignal(const std::string& interfaceName)
273 {
274 object_->emitPropertiesChangedSignal(interfaceName);
275 }
276
277 private:
278 sdbus::IObject* object_;
279 };
280
291 class ObjectManager_adaptor
292 {
293 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
294
295 protected:
296 explicit ObjectManager_adaptor(sdbus::IObject& object)
297 : object_(&object)
298 {
299 object_->addObjectManager();
300 }
301
302 ObjectManager_adaptor(const ObjectManager_adaptor&) = delete;
303 ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
304 ObjectManager_adaptor(ObjectManager_adaptor&&) = default;
305 ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = default;
306
307 ~ObjectManager_adaptor() = default;
308
309 private:
310 sdbus::IObject* object_;
311 };
312
324 class ManagedObject_adaptor
325 {
326 protected:
327 explicit ManagedObject_adaptor(sdbus::IObject& object)
328 : object_(&object)
329 {
330 }
331
332 ManagedObject_adaptor(const ManagedObject_adaptor&) = delete;
333 ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
334 ManagedObject_adaptor(ManagedObject_adaptor&&) = default;
335 ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = default;
336
337 ~ManagedObject_adaptor() = default;
338
339 public:
345 void emitInterfacesAddedSignal()
346 {
347 object_->emitInterfacesAddedSignal();
348 }
349
355 void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
356 {
357 object_->emitInterfacesAddedSignal(interfaces);
358 }
359
365 void emitInterfacesRemovedSignal()
366 {
367 object_->emitInterfacesRemovedSignal();
368 }
369
375 void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
376 {
377 object_->emitInterfacesRemovedSignal(interfaces);
378 }
379
380 private:
381 sdbus::IObject* object_;
382 };
383
384}
385
386#endif /* SDBUS_CXX_STANDARDINTERFACES_H_ */
IObject.h
IProxy.h
Types.h
sdbus::IObject
Definition IObject.h:60
sdbus::IObject::emitInterfacesRemovedSignal
virtual void emitInterfacesRemovedSignal()=0
Emits InterfacesRemoved signal on this object path.
sdbus::IObject::addObjectManager
virtual void addObjectManager()=0
Adds an ObjectManager interface at the path of this D-Bus object.
sdbus::IObject::emitInterfacesAddedSignal
virtual void emitInterfacesAddedSignal()=0
Emits InterfacesAdded signal on this object path.
sdbus::IObject::emitPropertiesChangedSignal
virtual void emitPropertiesChangedSignal(const std::string &interfaceName, const std::vector< std::string > &propNames)=0
Emits PropertyChanged signal for specified properties under a given interface of this object path.
sdbus::IProxy
Definition IProxy.h:66
sdbus::IProxy::getAllPropertiesAsync
AsyncAllPropertiesGetter getAllPropertiesAsync()
Gets values of all properties of the D-Bus object asynchronously.
Definition IProxy.h:546
sdbus::IProxy::getPropertyAsync
AsyncPropertyGetter getPropertyAsync(const std::string &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:526
sdbus::IProxy::setProperty
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the D-Bus object.
Definition IProxy.h:531
sdbus::IProxy::callMethod
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the D-Bus object.
sdbus::IProxy::setPropertyAsync
AsyncPropertySetter setPropertyAsync(const std::string &propertyName)
Sets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:536
sdbus::IProxy::getProperty
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the D-Bus object.
Definition IProxy.h:521
sdbus::IProxy::getAllProperties
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:541
sdbus::IProxy::uponSignal
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:511
sdbus::Introspectable_proxy
Definition StandardInterfaces.h:76
sdbus::ManagedObject_adaptor
Managed Object Convenience Adaptor.
Definition StandardInterfaces.h:325
sdbus::ManagedObject_adaptor::emitInterfacesRemovedSignal
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:365
sdbus::ManagedObject_adaptor::emitInterfacesRemovedSignal
void emitInterfacesRemovedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:375
sdbus::ManagedObject_adaptor::emitInterfacesAddedSignal
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:345
sdbus::ManagedObject_adaptor::emitInterfacesAddedSignal
void emitInterfacesAddedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:355
sdbus::ObjectManager_adaptor
Object Manager Convenience Adaptor.
Definition StandardInterfaces.h:292
sdbus::ObjectManager_proxy
Definition StandardInterfaces.h:195
sdbus::ObjectPath
Definition Types.h:160
sdbus::Peer_proxy
Definition StandardInterfaces.h:41
sdbus::PendingAsyncCall
Definition IProxy.h:445
sdbus::Properties_adaptor
Definition StandardInterfaces.h:250
sdbus::Properties_proxy
Definition StandardInterfaces.h:106
sdbus::Variant
Definition Types.h:54
sdbus::dont_expect_reply_t
Definition TypeTraits.h:97
sdbus::with_future_t
Definition TypeTraits.h:94