sdbus-c++ 2.0.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 <string_view>
35#include <map>
36#include <vector>
37
38namespace sdbus {
39
40 // Proxy for peer
42 {
43 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Peer";
44
45 protected:
47 : m_proxy(proxy)
48 {
49 }
50
51 Peer_proxy(const Peer_proxy&) = delete;
52 Peer_proxy& operator=(const Peer_proxy&) = delete;
53 Peer_proxy(Peer_proxy&&) = delete;
54 Peer_proxy& operator=(Peer_proxy&&) = delete;
55
56 ~Peer_proxy() = default;
57
58 void registerProxy()
59 {
60 }
61
62 public:
63 void Ping()
64 {
65 m_proxy.callMethod("Ping").onInterface(INTERFACE_NAME);
66 }
67
68 std::string GetMachineId()
69 {
70 std::string machineUUID;
71 m_proxy.callMethod("GetMachineId").onInterface(INTERFACE_NAME).storeResultsTo(machineUUID);
72 return machineUUID;
73 }
74
75 private:
76 sdbus::IProxy& m_proxy;
77 };
78
79 // Proxy for introspection
81 {
82 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
83
84 protected:
86 : m_proxy(proxy)
87 {
88 }
89
91 Introspectable_proxy& operator=(const Introspectable_proxy&) = delete;
93 Introspectable_proxy& operator=(Introspectable_proxy&&) = delete;
94
95 ~Introspectable_proxy() = default;
96
97 void registerProxy()
98 {
99 }
100
101 public:
102 std::string Introspect()
103 {
104 std::string xml;
105 m_proxy.callMethod("Introspect").onInterface(INTERFACE_NAME).storeResultsTo(xml);
106 return xml;
107 }
108
109 private:
110 sdbus::IProxy& m_proxy;
111 };
112
113 // Proxy for properties
115 {
116 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
117
118 protected:
120 : m_proxy(proxy)
121 {
122 }
123
124 Properties_proxy(const Properties_proxy&) = delete;
125 Properties_proxy& operator=(const Properties_proxy&) = delete;
127 Properties_proxy& operator=(Properties_proxy&&) = delete;
128
129 ~Properties_proxy() = default;
130
131 void registerProxy()
132 {
133 m_proxy
134 .uponSignal("PropertiesChanged")
135 .onInterface(INTERFACE_NAME)
136 .call([this]( const InterfaceName& interfaceName
137 , const std::map<PropertyName, sdbus::Variant>& changedProperties
138 , const std::vector<PropertyName>& invalidatedProperties )
139 {
140 this->onPropertiesChanged(interfaceName, changedProperties, invalidatedProperties);
141 });
142 }
143
144 virtual void onPropertiesChanged( const InterfaceName& interfaceName
145 , const std::map<PropertyName, sdbus::Variant>& changedProperties
146 , const std::vector<PropertyName>& invalidatedProperties ) = 0;
147
148 public:
149 sdbus::Variant Get(const InterfaceName& interfaceName, const PropertyName& propertyName)
150 {
151 return m_proxy.getProperty(propertyName).onInterface(interfaceName);
152 }
153
154 sdbus::Variant Get(std::string_view interfaceName, std::string_view propertyName)
155 {
156 return m_proxy.getProperty(propertyName).onInterface(interfaceName);
157 }
158
159 template <typename _Function>
160 PendingAsyncCall GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, _Function&& callback)
161 {
162 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
163 }
164
165 template <typename _Function>
166 [[nodiscard]] Slot GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, _Function&& callback, return_slot_t)
167 {
168 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
169 }
170
171 template <typename _Function>
172 PendingAsyncCall GetAsync(std::string_view interfaceName, std::string_view propertyName, _Function&& callback)
173 {
174 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
175 }
176
177 template <typename _Function>
178 [[nodiscard]] Slot GetAsync(std::string_view interfaceName, std::string_view propertyName, _Function&& callback, return_slot_t)
179 {
180 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
181 }
182
183 std::future<sdbus::Variant> GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, with_future_t)
184 {
185 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
186 }
187
188 std::future<sdbus::Variant> GetAsync(std::string_view interfaceName, std::string_view propertyName, with_future_t)
189 {
190 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
191 }
192
193 void Set(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value)
194 {
195 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value);
196 }
197
198 void Set(std::string_view interfaceName, const std::string_view propertyName, const sdbus::Variant& value)
199 {
200 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value);
201 }
202
203 void Set(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, dont_expect_reply_t)
204 {
205 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
206 }
207
208 void Set(std::string_view interfaceName, const std::string_view propertyName, const sdbus::Variant& value, dont_expect_reply_t)
209 {
210 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
211 }
212
213 template <typename _Function>
214 PendingAsyncCall SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, _Function&& callback)
215 {
216 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
217 }
218
219 template <typename _Function>
220 PendingAsyncCall SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, _Function&& callback, return_slot_t)
221 {
222 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
223 }
224
225 template <typename _Function>
226 PendingAsyncCall SetAsync(std::string_view interfaceName, std::string_view propertyName, const sdbus::Variant& value, _Function&& callback)
227 {
228 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
229 }
230
231 template <typename _Function>
232 PendingAsyncCall SetAsync(std::string_view interfaceName, std::string_view propertyName, const sdbus::Variant& value, _Function&& callback, return_slot_t)
233 {
234 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
235 }
236
237 std::future<void> SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, with_future_t)
238 {
239 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
240 }
241
242 std::future<void> SetAsync(std::string_view interfaceName, std::string_view propertyName, const sdbus::Variant& value, with_future_t)
243 {
244 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
245 }
246
247 std::map<PropertyName, sdbus::Variant> GetAll(const InterfaceName& interfaceName)
248 {
249 return m_proxy.getAllProperties().onInterface(interfaceName);
250 }
251
252 std::map<PropertyName, sdbus::Variant> GetAll(std::string_view interfaceName)
253 {
254 return m_proxy.getAllProperties().onInterface(interfaceName);
255 }
256
257 template <typename _Function>
258 PendingAsyncCall GetAllAsync(const InterfaceName& interfaceName, _Function&& callback)
259 {
260 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
261 }
262
263 template <typename _Function>
264 PendingAsyncCall GetAllAsync(const InterfaceName& interfaceName, _Function&& callback, return_slot_t)
265 {
266 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
267 }
268
269 template <typename _Function>
270 PendingAsyncCall GetAllAsync(std::string_view interfaceName, _Function&& callback)
271 {
272 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
273 }
274
275 template <typename _Function>
276 PendingAsyncCall GetAllAsync(std::string_view interfaceName, _Function&& callback, return_slot_t)
277 {
278 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
279 }
280
281 std::future<std::map<PropertyName, sdbus::Variant>> GetAllAsync(const InterfaceName& interfaceName, with_future_t)
282 {
283 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
284 }
285
286 std::future<std::map<PropertyName, sdbus::Variant>> GetAllAsync(std::string_view interfaceName, with_future_t)
287 {
288 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
289 }
290
291 private:
292 sdbus::IProxy& m_proxy;
293 };
294
295 // Proxy for object manager
297 {
298 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
299
300 protected:
302 : m_proxy(proxy)
303 {
304 }
305
307 ObjectManager_proxy& operator=(const ObjectManager_proxy&) = delete;
309 ObjectManager_proxy& operator=(ObjectManager_proxy&&) = delete;
310
311 ~ObjectManager_proxy() = default;
312
313 void registerProxy()
314 {
315 m_proxy
316 .uponSignal("InterfacesAdded")
317 .onInterface(INTERFACE_NAME)
318 .call([this]( const sdbus::ObjectPath& objectPath
319 , const std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>& interfacesAndProperties )
320 {
321 this->onInterfacesAdded(objectPath, interfacesAndProperties);
322 });
323
324 m_proxy
325 .uponSignal("InterfacesRemoved")
326 .onInterface(INTERFACE_NAME)
327 .call([this]( const sdbus::ObjectPath& objectPath
328 , const std::vector<sdbus::InterfaceName>& interfaces )
329 {
330 this->onInterfacesRemoved(objectPath, interfaces);
331 });
332 }
333
334 virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
335 , const std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>& interfacesAndProperties) = 0;
336 virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
337 , const std::vector<sdbus::InterfaceName>& interfaces) = 0;
338
339 public:
340 std::map<sdbus::ObjectPath, std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>> GetManagedObjects()
341 {
342 std::map<sdbus::ObjectPath, std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>> objectsInterfacesAndProperties;
343 m_proxy.callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
344 return objectsInterfacesAndProperties;
345 }
346
347 private:
348 sdbus::IProxy& m_proxy;
349 };
350
351 // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
352 // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
353 // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
354
355 // Adaptor for properties
357 {
358 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
359
360 protected:
361 Properties_adaptor(sdbus::IObject& object) : m_object(object)
362 {
363 }
364
365 Properties_adaptor(const Properties_adaptor&) = delete;
366 Properties_adaptor& operator=(const Properties_adaptor&) = delete;
368 Properties_adaptor& operator=(Properties_adaptor&&) = delete;
369
370 ~Properties_adaptor() = default;
371
372 void registerAdaptor()
373 {
374 }
375
376 public:
377 void emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& properties)
378 {
379 m_object.emitPropertiesChangedSignal(interfaceName, properties);
380 }
381
382 void emitPropertiesChangedSignal(const char* interfaceName, const std::vector<PropertyName>& properties)
383 {
384 m_object.emitPropertiesChangedSignal(interfaceName, properties);
385 }
386
387 void emitPropertiesChangedSignal(const InterfaceName& interfaceName)
388 {
389 m_object.emitPropertiesChangedSignal(interfaceName);
390 }
391
392 void emitPropertiesChangedSignal(const char* interfaceName)
393 {
394 m_object.emitPropertiesChangedSignal(interfaceName);
395 }
396
397 private:
398 sdbus::IObject& m_object;
399 };
400
412 {
413 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
414
415 protected:
416 explicit ObjectManager_adaptor(sdbus::IObject& object) : m_object(object)
417 {
418 }
419
421 ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
423 ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = delete;
424
425 ~ObjectManager_adaptor() = default;
426
427 void registerAdaptor()
428 {
429 m_object.addObjectManager();
430 }
431
432 private:
433 sdbus::IObject& m_object;
434 };
435
448 {
449 protected:
450 explicit ManagedObject_adaptor(sdbus::IObject& object)
451 : m_object(object)
452 {
453 }
454
456 ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
458 ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = delete;
459
460 ~ManagedObject_adaptor() = default;
461
462 void registerAdaptor()
463 {
464 }
465
466 public:
473 {
474 m_object.emitInterfacesAddedSignal();
475 }
476
482 void emitInterfacesAddedSignal(const std::vector<sdbus::InterfaceName>& interfaces)
483 {
484 m_object.emitInterfacesAddedSignal(interfaces);
485 }
486
493 {
495 }
496
502 void emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces)
503 {
504 m_object.emitInterfacesRemovedSignal(interfaces);
505 }
506
507 private:
508 sdbus::IObject& m_object;
509 };
510
511}
512
513#endif /* SDBUS_CXX_STANDARDINTERFACES_H_ */
Definition IObject.h:63
virtual void emitInterfacesRemovedSignal()=0
Emits InterfacesRemoved signal on this object path.
virtual void emitPropertiesChangedSignal(const InterfaceName &interfaceName, const std::vector< PropertyName > &propNames)=0
Emits PropertyChanged signal for specified properties under a given interface of this object path.
virtual void addObjectManager()=0
Adds an ObjectManager interface at the path of this D-Bus object.
virtual void emitInterfacesAddedSignal()=0
Emits InterfacesAdded signal on this object path.
Definition IProxy.h:69
PropertyGetter getProperty(const PropertyName &propertyName)
Gets value of a property of the D-Bus object.
Definition IProxy.h:789
AsyncPropertyGetter getPropertyAsync(const PropertyName &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:799
AsyncAllPropertiesGetter getAllPropertiesAsync()
Gets values of all properties of the D-Bus object asynchronously.
Definition IProxy.h:834
PropertySetter setProperty(const PropertyName &propertyName)
Sets value of a property of the D-Bus object.
Definition IProxy.h:809
MethodInvoker callMethod(const MethodName &methodName)
Calls method on the D-Bus object.
Definition IProxy.h:744
SignalSubscriber uponSignal(const SignalName &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:774
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:829
AsyncPropertySetter setPropertyAsync(const PropertyName &propertyName)
Sets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:819
Definition Types.h:220
Definition StandardInterfaces.h:81
Managed Object Convenience Adaptor.
Definition StandardInterfaces.h:448
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:492
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:472
void emitInterfacesAddedSignal(const std::vector< sdbus::InterfaceName > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:482
void emitInterfacesRemovedSignal(const std::vector< InterfaceName > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:502
Definition Types.h:240
Object Manager Convenience Adaptor.
Definition StandardInterfaces.h:412
Definition StandardInterfaces.h:297
Definition Types.h:177
Definition StandardInterfaces.h:42
Definition IProxy.h:676
Definition StandardInterfaces.h:357
Definition StandardInterfaces.h:115
Definition Types.h:56
Definition TypeTraits.h:107
Definition TypeTraits.h:88
Definition TypeTraits.h:104