sdbus-c++ 2.2.1
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 Peer_proxy(sdbus::IProxy& proxy)
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
80 class Introspectable_proxy
81 {
82 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
83
84 protected:
85 Introspectable_proxy(sdbus::IProxy& proxy)
86 : m_proxy(proxy)
87 {
88 }
89
90 Introspectable_proxy(const Introspectable_proxy&) = delete;
91 Introspectable_proxy& operator=(const Introspectable_proxy&) = delete;
92 Introspectable_proxy(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
114 class Properties_proxy
115 {
116 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
117
118 protected:
119 Properties_proxy(sdbus::IProxy& proxy)
120 : m_proxy(proxy)
121 {
122 }
123
124 Properties_proxy(const Properties_proxy&) = delete;
125 Properties_proxy& operator=(const Properties_proxy&) = delete;
126 Properties_proxy(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 std::future<sdbus::Variant> GetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, with_future_t)
172 {
173 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
174 }
175
176 template <typename _Function>
177 PendingAsyncCall GetAsync(std::string_view interfaceName, std::string_view propertyName, _Function&& callback)
178 {
179 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
180 }
181
182 template <typename _Function>
183 [[nodiscard]] Slot GetAsync(std::string_view interfaceName, std::string_view propertyName, _Function&& callback, return_slot_t)
184 {
185 return m_proxy.getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
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 [[nodiscard]] Slot 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 std::future<void> SetAsync(const InterfaceName& interfaceName, const PropertyName& propertyName, const sdbus::Variant& value, with_future_t)
226 {
227 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
228 }
229
230 template <typename _Function>
231 PendingAsyncCall SetAsync(std::string_view interfaceName, std::string_view propertyName, const sdbus::Variant& value, _Function&& callback)
232 {
233 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
234 }
235
236 template <typename _Function>
237 [[nodiscard]] Slot SetAsync(std::string_view interfaceName, std::string_view propertyName, const sdbus::Variant& value, _Function&& callback, return_slot_t)
238 {
239 return m_proxy.setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
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 [[nodiscard]] Slot 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 std::future<std::map<PropertyName, sdbus::Variant>> GetAllAsync(const InterfaceName& interfaceName, with_future_t)
270 {
271 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
272 }
273
274 template <typename _Function>
275 PendingAsyncCall GetAllAsync(std::string_view interfaceName, _Function&& callback)
276 {
277 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
278 }
279
280 template <typename _Function>
281 [[nodiscard]] Slot GetAllAsync(std::string_view interfaceName, _Function&& callback, return_slot_t)
282 {
283 return m_proxy.getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
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
296 class ObjectManager_proxy
297 {
298 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
299
300 protected:
301 ObjectManager_proxy(sdbus::IProxy& proxy)
302 : m_proxy(proxy)
303 {
304 }
305
306 ObjectManager_proxy(const ObjectManager_proxy&) = delete;
307 ObjectManager_proxy& operator=(const ObjectManager_proxy&) = delete;
308 ObjectManager_proxy(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 template <typename _Function>
348 PendingAsyncCall GetManagedObjectsAsync(_Function&& callback)
349 {
350 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).uponReplyInvoke(std::forward<_Function>(callback));
351 }
352
353 template <typename _Function>
354 [[nodiscard]] Slot GetManagedObjectsAsync(_Function&& callback, return_slot_t)
355 {
356 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).uponReplyInvoke(std::forward<_Function>(callback), return_slot);
357 }
358
359 std::future<std::map<sdbus::ObjectPath, std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>>> GetManagedObjectsAsync(with_future_t)
360 {
361 return m_proxy.callMethodAsync("GetManagedObjects").onInterface(INTERFACE_NAME).getResultAsFuture<std::map<sdbus::ObjectPath, std::map<sdbus::InterfaceName, std::map<PropertyName, sdbus::Variant>>>>();
362 }
363
364 private:
365 sdbus::IProxy& m_proxy;
366 };
367
368 // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
369 // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
370 // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
371
372 // Adaptor for properties
373 class Properties_adaptor
374 {
375 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
376
377 protected:
378 Properties_adaptor(sdbus::IObject& object) : m_object(object)
379 {
380 }
381
382 Properties_adaptor(const Properties_adaptor&) = delete;
383 Properties_adaptor& operator=(const Properties_adaptor&) = delete;
384 Properties_adaptor(Properties_adaptor&&) = delete;
385 Properties_adaptor& operator=(Properties_adaptor&&) = delete;
386
387 ~Properties_adaptor() = default;
388
389 void registerAdaptor()
390 {
391 }
392
393 public:
394 void emitPropertiesChangedSignal(const InterfaceName& interfaceName, const std::vector<PropertyName>& properties)
395 {
396 m_object.emitPropertiesChangedSignal(interfaceName, properties);
397 }
398
399 void emitPropertiesChangedSignal(const char* interfaceName, const std::vector<PropertyName>& properties)
400 {
401 m_object.emitPropertiesChangedSignal(interfaceName, properties);
402 }
403
404 void emitPropertiesChangedSignal(const InterfaceName& interfaceName)
405 {
406 m_object.emitPropertiesChangedSignal(interfaceName);
407 }
408
409 void emitPropertiesChangedSignal(const char* interfaceName)
410 {
411 m_object.emitPropertiesChangedSignal(interfaceName);
412 }
413
414 private:
415 sdbus::IObject& m_object;
416 };
417
428 class ObjectManager_adaptor
429 {
430 static inline const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
431
432 protected:
433 explicit ObjectManager_adaptor(sdbus::IObject& object) : m_object(object)
434 {
435 }
436
437 ObjectManager_adaptor(const ObjectManager_adaptor&) = delete;
438 ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
439 ObjectManager_adaptor(ObjectManager_adaptor&&) = delete;
440 ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = delete;
441
442 ~ObjectManager_adaptor() = default;
443
444 void registerAdaptor()
445 {
446 m_object.addObjectManager();
447 }
448
449 private:
450 sdbus::IObject& m_object;
451 };
452
464 class ManagedObject_adaptor
465 {
466 protected:
467 explicit ManagedObject_adaptor(sdbus::IObject& object)
468 : m_object(object)
469 {
470 }
471
472 ManagedObject_adaptor(const ManagedObject_adaptor&) = delete;
473 ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
474 ManagedObject_adaptor(ManagedObject_adaptor&&) = delete;
475 ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = delete;
476
477 ~ManagedObject_adaptor() = default;
478
479 void registerAdaptor()
480 {
481 }
482
483 public:
490 {
491 m_object.emitInterfacesAddedSignal();
492 }
493
499 void emitInterfacesAddedSignal(const std::vector<sdbus::InterfaceName>& interfaces)
500 {
501 m_object.emitInterfacesAddedSignal(interfaces);
502 }
503
510 {
511 m_object.emitInterfacesRemovedSignal();
512 }
513
519 void emitInterfacesRemovedSignal(const std::vector<InterfaceName>& interfaces)
520 {
521 m_object.emitInterfacesRemovedSignal(interfaces);
522 }
523
524 private:
525 sdbus::IObject& m_object;
526 };
527
528}
529
530#endif /* SDBUS_CXX_STANDARDINTERFACES_H_ */
Definition IObject.h:63
Definition IProxy.h:69
Definition Types.h:238
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:509
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:489
void emitInterfacesAddedSignal(const std::vector< sdbus::InterfaceName > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:499
void emitInterfacesRemovedSignal(const std::vector< InterfaceName > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:519
Definition Types.h:195
Definition IProxy.h:676
Definition Types.h:57
Definition TypeTraits.h:107
Definition TypeTraits.h:88
Definition TypeTraits.h:104