sdbus-c++ 2.3.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
26
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
41 class Peer_proxy
42 {
43 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Peer";
44
45 protected:
46 explicit Peer_proxy(IProxy& proxy)
47 : m_proxy(proxy)
48 {
49 }
50
51 ~Peer_proxy() = default;
52
53 void registerProxy()
54 {
55 }
56
57 public:
58 Peer_proxy(const Peer_proxy&) = delete;
59 Peer_proxy& operator=(const Peer_proxy&) = delete;
60 Peer_proxy(Peer_proxy&&) = delete;
61 Peer_proxy& operator=(Peer_proxy&&) = delete;
62
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 IProxy& m_proxy;
77 };
78
79 // Proxy for introspection
80 class Introspectable_proxy
81 {
82 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
83
84 protected:
85 explicit Introspectable_proxy(IProxy& proxy)
86 : m_proxy(proxy)
87 {
88 }
89
90 ~Introspectable_proxy() = default;
91
92 void registerProxy()
93 {
94 }
95
96 public:
97 Introspectable_proxy(const Introspectable_proxy&) = delete;
98 Introspectable_proxy& operator=(const Introspectable_proxy&) = delete;
99 Introspectable_proxy(Introspectable_proxy&&) = delete;
100 Introspectable_proxy& operator=(Introspectable_proxy&&) = delete;
101
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 IProxy& m_proxy;
111 };
112
113 // Proxy for properties
114 class Properties_proxy
115 {
116 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
117
118 protected:
119 explicit Properties_proxy(IProxy& proxy)
120 : m_proxy(proxy)
121 {
122 }
123
124 ~Properties_proxy() = default;
125
126 void registerProxy()
127 {
128 m_proxy
129 .uponSignal("PropertiesChanged")
130 .onInterface(INTERFACE_NAME)
131 .call([this]( const InterfaceName& interfaceName
132 , const std::map<PropertyName, Variant>& changedProperties
133 , const std::vector<PropertyName>& invalidatedProperties )
134 {
135 this->onPropertiesChanged(interfaceName, changedProperties, invalidatedProperties);
136 });
137 }
138
139 virtual void onPropertiesChanged( const InterfaceName& interfaceName
140 , const std::map<PropertyName, Variant>& changedProperties
141 , const std::vector<PropertyName>& invalidatedProperties ) = 0;
142
143 public:
144 Properties_proxy(const Properties_proxy&) = delete;
145 Properties_proxy& operator=(const Properties_proxy&) = delete;
146 Properties_proxy(Properties_proxy&&) = delete;
147 Properties_proxy& operator=(Properties_proxy&&) = delete;
148
149 Variant Get(const InterfaceName& interfaceName, const PropertyName& propertyName)
150 {
151 return m_proxy.getProperty(propertyName).onInterface(interfaceName);
152 }
153
154 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 std::future<Variant> GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, with_future_t)
172 {
173 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
174 }
175
176 Awaitable<Variant> GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, with_awaitable_t)
177 {
178 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsAwaitable();
179 }
180
181 template <typename Function>
182 PendingAsyncCall GetAsync(std::string_view interfaceName, std::string_view propertyName, Function&& callback)
183 {
184 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<Function>(callback));
185 }
186
187 template <typename Function>
188 [[nodiscard]] Slot GetAsync(std::string_view interfaceName, std::string_view propertyName, Function&& callback, return_slot_t)
189 {
190 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<Function>(callback), return_slot);
191 }
192
193 std::future<Variant> GetAsync(std::string_view interfaceName, std::string_view propertyName, with_future_t)
194 {
195 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
196 }
197
198 Awaitable<Variant> GetAsync(std::string_view interfaceName, std::string_view propertyName, with_awaitable_t)
199 {
200 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsAwaitable();
201 }
202
203 void Set(const InterfaceName& interfaceName, const PropertyName& propertyName, const Variant& value)
204 {
205 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value);
206 }
207
208 void Set(std::string_view interfaceName, const std::string_view propertyName, const Variant& value)
209 {
210 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value);
211 }
212
213 void Set(const InterfaceName& interfaceName, const PropertyName& propertyName, const Variant& value, dont_expect_reply_t)
214 {
215 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
216 }
217
218 void Set(std::string_view interfaceName, const std::string_view propertyName, const Variant& value, dont_expect_reply_t)
219 {
220 m_proxy.setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
221 }
222
223 template <typename Function>
224 PendingAsyncCall SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const Variant& value, Function&& callback)
225 {
226 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<Function>(callback));
227 }
228
229 template <typename Function>
230 [[nodiscard]] Slot SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const Variant& value, Function&& callback, return_slot_t)
231 {
232 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<Function>(callback), return_slot);
233 }
234
235 std::future<void> SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const Variant& value, with_future_t)
236 {
237 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
238 }
239
240 Awaitable<void> SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const Variant& value, with_awaitable_t)
241 {
242 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsAwaitable();
243 }
244
245 template <typename Function>
246 PendingAsyncCall SetAsync(std::string_view interfaceName, std::string_view propertyName, const Variant& value, Function&& callback)
247 {
248 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<Function>(callback));
249 }
250
251 template <typename Function>
252 [[nodiscard]] Slot SetAsync(std::string_view interfaceName, std::string_view propertyName, const Variant& value, Function&& callback, return_slot_t)
253 {
254 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<Function>(callback), return_slot);
255 }
256
257 std::future<void> SetAsync(std::string_view interfaceName, std::string_view propertyName, const Variant& value, with_future_t)
258 {
259 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
260 }
261
262 Awaitable<void> SetAsync(std::string_view interfaceName, std::string_view propertyName, const Variant& value, with_awaitable_t)
263 {
264 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsAwaitable();
265 }
266
267 std::map<PropertyName, Variant> GetAll(const InterfaceName& interfaceName)
268 {
269 return m_proxy.getAllProperties().onInterface(interfaceName);
270 }
271
272 std::map<PropertyName, Variant> GetAll(std::string_view interfaceName)
273 {
274 return m_proxy.getAllProperties().onInterface(interfaceName);
275 }
276
277 template <typename Function>
278 PendingAsyncCall GetAllAsync(const InterfaceName& interfaceName, Function&& callback)
279 {
280 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<Function>(callback));
281 }
282
283 template <typename Function>
284 [[nodiscard]] Slot GetAllAsync(const InterfaceName& interfaceName, Function&& callback, return_slot_t)
285 {
286 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<Function>(callback), return_slot);
287 }
288
289 std::future<std::map<PropertyName, Variant>> GetAllAsync(const InterfaceName& interfaceName, with_future_t)
290 {
291 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
292 }
293
295 {
296 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsAwaitable();
297 }
298
299 template <typename Function>
300 PendingAsyncCall GetAllAsync(std::string_view interfaceName, Function&& callback)
301 {
302 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<Function>(callback));
303 }
304
305 template <typename Function>
306 [[nodiscard]] Slot GetAllAsync(std::string_view interfaceName, Function&& callback, return_slot_t)
307 {
308 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<Function>(callback), return_slot);
309 }
310
311 std::future<std::map<PropertyName, Variant>> GetAllAsync(std::string_view interfaceName, with_future_t)
312 {
313 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
314 }
315
316 Awaitable<std::map<PropertyName, Variant>> GetAllAsync(std::string_view interfaceName, with_awaitable_t)
317 {
318 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsAwaitable();
319 }
320
321 private:
322 IProxy& m_proxy;
323 };
324
325 // Proxy for object manager
326 class ObjectManager_proxy
327 {
328 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
329
330 protected:
331 explicit ObjectManager_proxy(IProxy& proxy)
332 : m_proxy(proxy)
333 {
334 }
335
336 ~ObjectManager_proxy() = default;
337
338 void registerProxy()
339 {
340 m_proxy
341 .uponSignal("InterfacesAdded")
342 .onInterface(INTERFACE_NAME)
343 .call([this]( const ObjectPath& objectPath
344 , const std::map<InterfaceName, std::map<PropertyName, Variant>>& interfacesAndProperties )
345 {
346 this->onInterfacesAdded(objectPath, interfacesAndProperties);
347 });
348
349 m_proxy
350 .uponSignal("InterfacesRemoved")
351 .onInterface(INTERFACE_NAME)
352 .call([this]( const ObjectPath& objectPath
353 , const std::vector<InterfaceName>& interfaces )
354 {
355 this->onInterfacesRemoved(objectPath, interfaces);
356 });
357 }
358
359 virtual void onInterfacesAdded( const ObjectPath& objectPath
360 , const std::map<InterfaceName, std::map<PropertyName, Variant>>& interfacesAndProperties) = 0;
361 virtual void onInterfacesRemoved( const ObjectPath& objectPath
362 , const std::vector<InterfaceName>& interfaces) = 0;
363
364 public:
365 ObjectManager_proxy(const ObjectManager_proxy&) = delete;
366 ObjectManager_proxy& operator=(const ObjectManager_proxy&) = delete;
367 ObjectManager_proxy(ObjectManager_proxy&&) = delete;
368 ObjectManager_proxy& operator=(ObjectManager_proxy&&) = delete;
369
370 std::map<ObjectPath, std::map<InterfaceName, std::map<PropertyName, Variant>>> GetManagedObjects()
371 {
372 std::map<ObjectPath, std::map<InterfaceName, std::map<PropertyName, Variant>>> objectsInterfacesAndProperties;
373 m_proxy.callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
374 return objectsInterfacesAndProperties;
375 }
376
377 template <typename Function>
378 PendingAsyncCall GetManagedObjectsAsync(Function&& callback)
379 {
380 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).uponReplyInvoke(std::forward<Function>(callback));
381 }
382
383 template <typename Function>
384 [[nodiscard]] Slot GetManagedObjectsAsync(Function&& callback, return_slot_t)
385 {
386 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).uponReplyInvoke(std::forward<Function>(callback), return_slot);
387 }
388
389 std::future<std::map<ObjectPath, std::map<InterfaceName, std::map<PropertyName, Variant>>>> GetManagedObjectsAsync(with_future_t)
390 {
391 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).getResultAsFuture<std::map<ObjectPath, std::map<InterfaceName, std::map<PropertyName, Variant>>>>();
392 }
393
395 {
396 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).getResultAsAwaitable<std::map<ObjectPath, std::map<InterfaceName, std::map<PropertyName, Variant>>>>();
397 }
398
399 private:
400 IProxy& m_proxy;
401 };
402
403 // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
404 // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
405 // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
406
407 // Adaptor for properties
408 class Properties_adaptor
409 {
410 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
411
412 protected:
413 explicit Properties_adaptor(IObject& object) : m_object(object)
414 {
415 }
416
417 ~Properties_adaptor() = default;
418
419 void registerAdaptor()
420 {
421 }
422
423 public:
424 Properties_adaptor(const Properties_adaptor&) = delete;
425 Properties_adaptor& operator=(const Properties_adaptor&) = delete;
426 Properties_adaptor(Properties_adaptor&&) = delete;
427 Properties_adaptor& operator=(Properties_adaptor&&) = delete;
428
429 void emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& properties)
430 {
431 m_object.emitPropertiesChangedSignal(interfaceName, properties);
432 }
433
434 void emitPropertiesChangedSignal(const char* interfaceName, const std::vector<PropertyName>& properties)
435 {
436 m_object.emitPropertiesChangedSignal(interfaceName, properties);
437 }
438
439 void emitPropertiesChangedSignal(const InterfaceName& interfaceName)
440 {
441 m_object.emitPropertiesChangedSignal(interfaceName);
442 }
443
444 void emitPropertiesChangedSignal(const char* interfaceName)
445 {
446 m_object.emitPropertiesChangedSignal(interfaceName);
447 }
448
449 private:
450 IObject& m_object;
451 };
452
463 class ObjectManager_adaptor
464 {
465 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
466
467 protected:
468 explicit ObjectManager_adaptor(IObject& object) : m_object(object)
469 {
470 }
471
472 ~ObjectManager_adaptor() = default;
473
474 void registerAdaptor()
475 {
476 m_object.addObjectManager();
477 }
478
479 public:
480 ObjectManager_adaptor(const ObjectManager_adaptor&) = delete;
481 ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
482 ObjectManager_adaptor(ObjectManager_adaptor&&) = delete;
483 ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = delete;
484
485 private:
486 IObject& m_object;
487 };
488
500 class ManagedObject_adaptor
501 {
502 protected:
503 explicit ManagedObject_adaptor(IObject& object)
504 : m_object(object)
505 {
506 }
507
508 ~ManagedObject_adaptor() = default;
509
510 void registerAdaptor()
511 {
512 }
513
514 public:
515 ManagedObject_adaptor(const ManagedObject_adaptor&) = delete;
516 ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
517 ManagedObject_adaptor(ManagedObject_adaptor&&) = delete;
518 ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = delete;
519
526 {
527 m_object.emitInterfacesAddedSignal();
528 }
529
535 void emitInterfacesAddedSignal(const std::vector<InterfaceName>& interfaces)
536 {
537 m_object.emitInterfacesAddedSignal(interfaces);
538 }
539
546 {
547 m_object.emitInterfacesRemovedSignal();
548 }
549
555 void emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces)
556 {
557 m_object.emitInterfacesRemovedSignal(interfaces);
558 }
559
560 private:
561 IObject& m_object;
562 };
563
564} // namespace sdbus
565
566#endif /* SDBUS_CXX_STANDARDINTERFACES_H_ */
Definition Awaitable.h:110
Definition IObject.h:63
Definition IProxy.h:70
Definition Types.h:251
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:545
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:525
void emitInterfacesRemovedSignal(const std::vector< InterfaceName > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:555
void emitInterfacesAddedSignal(const std::vector< InterfaceName > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:535
Definition Types.h:208
Definition IProxy.h:721
Definition Types.h:56
Definition TypeTraits.h:107
Definition TypeTraits.h:88
Definition TypeTraits.h:113
Definition TypeTraits.h:104