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

sdbus-c++ 1.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
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 void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
142 {
143 proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value);
144 }
145
146 std::map<std::string, sdbus::Variant> GetAll(const std::string& interfaceName)
147 {
148 std::map<std::string, sdbus::Variant> props;
149 proxy_->callMethod("GetAll").onInterface(INTERFACE_NAME).withArguments(interfaceName).storeResultsTo(props);
150 return props;
151 }
152
153 private:
154 sdbus::IProxy* proxy_;
155 };
156
157 // Proxy for object manager
158 class ObjectManager_proxy
159 {
160 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
161
162 protected:
163 ObjectManager_proxy(sdbus::IProxy& proxy)
164 : proxy_(&proxy)
165 {
166 proxy_
167 ->uponSignal("InterfacesAdded")
168 .onInterface(INTERFACE_NAME)
169 .call([this]( const sdbus::ObjectPath& objectPath
170 , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties )
171 {
172 this->onInterfacesAdded(objectPath, interfacesAndProperties);
173 });
174
175 proxy_->uponSignal("InterfacesRemoved")
176 .onInterface(INTERFACE_NAME)
177 .call([this]( const sdbus::ObjectPath& objectPath
178 , const std::vector<std::string>& interfaces )
179 {
180 this->onInterfacesRemoved(objectPath, interfaces);
181 });
182 }
183
184 ObjectManager_proxy(const ObjectManager_proxy&) = delete;
185 ObjectManager_proxy& operator=(const ObjectManager_proxy&) = delete;
186 ObjectManager_proxy(ObjectManager_proxy&&) = default;
187 ObjectManager_proxy& operator=(ObjectManager_proxy&&) = default;
188
189 ~ObjectManager_proxy() = default;
190
191 virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
192 , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) = 0;
193 virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
194 , const std::vector<std::string>& interfaces) = 0;
195
196 public:
197 std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> GetManagedObjects()
198 {
199 std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
200 proxy_->callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
201 return objectsInterfacesAndProperties;
202 }
203
204 private:
205 sdbus::IProxy* proxy_;
206 };
207
208 // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
209 // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
210 // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
211
212 // Adaptor for properties
213 class Properties_adaptor
214 {
215 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
216
217 protected:
218 Properties_adaptor(sdbus::IObject& object)
219 : object_(&object)
220 {
221 }
222
223 Properties_adaptor(const Properties_adaptor&) = delete;
224 Properties_adaptor& operator=(const Properties_adaptor&) = delete;
225 Properties_adaptor(Properties_adaptor&&) = default;
226 Properties_adaptor& operator=(Properties_adaptor&&) = default;
227
228 ~Properties_adaptor() = default;
229
230 public:
231 void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& properties)
232 {
233 object_->emitPropertiesChangedSignal(interfaceName, properties);
234 }
235
236 void emitPropertiesChangedSignal(const std::string& interfaceName)
237 {
238 object_->emitPropertiesChangedSignal(interfaceName);
239 }
240
241 private:
242 sdbus::IObject* object_;
243 };
244
255 class ObjectManager_adaptor
256 {
257 static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
258
259 protected:
260 explicit ObjectManager_adaptor(sdbus::IObject& object)
261 : object_(&object)
262 {
263 object_->addObjectManager();
264 }
265
266 ObjectManager_adaptor(const ObjectManager_adaptor&) = delete;
267 ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
268 ObjectManager_adaptor(ObjectManager_adaptor&&) = default;
269 ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = default;
270
271 ~ObjectManager_adaptor() = default;
272
273 private:
274 sdbus::IObject* object_;
275 };
276
288 class ManagedObject_adaptor
289 {
290 protected:
291 explicit ManagedObject_adaptor(sdbus::IObject& object)
292 : object_(&object)
293 {
294 }
295
296 ManagedObject_adaptor(const ManagedObject_adaptor&) = delete;
297 ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
298 ManagedObject_adaptor(ManagedObject_adaptor&&) = default;
299 ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = default;
300
301 ~ManagedObject_adaptor() = default;
302
303 public:
309 void emitInterfacesAddedSignal()
310 {
311 object_->emitInterfacesAddedSignal();
312 }
313
319 void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
320 {
321 object_->emitInterfacesAddedSignal(interfaces);
322 }
323
329 void emitInterfacesRemovedSignal()
330 {
331 object_->emitInterfacesRemovedSignal();
332 }
333
339 void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
340 {
341 object_->emitInterfacesRemovedSignal(interfaces);
342 }
343
344 private:
345 sdbus::IObject* object_;
346 };
347
348}
349
350#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::setProperty
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the proxied D-Bus object.
Definition IProxy.h:448
sdbus::IProxy::callMethod
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the proxied D-Bus object.
sdbus::IProxy::getProperty
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the proxied D-Bus object.
Definition IProxy.h:443
sdbus::IProxy::uponSignal
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the proxied D-Bus object.
Definition IProxy.h:433
sdbus::Introspectable_proxy
Definition StandardInterfaces.h:76
sdbus::ManagedObject_adaptor
Managed Object Convenience Adaptor.
Definition StandardInterfaces.h:289
sdbus::ManagedObject_adaptor::emitInterfacesRemovedSignal
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:329
sdbus::ManagedObject_adaptor::emitInterfacesRemovedSignal
void emitInterfacesRemovedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition StandardInterfaces.h:339
sdbus::ManagedObject_adaptor::emitInterfacesAddedSignal
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:309
sdbus::ManagedObject_adaptor::emitInterfacesAddedSignal
void emitInterfacesAddedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition StandardInterfaces.h:319
sdbus::ObjectManager_adaptor
Object Manager Convenience Adaptor.
Definition StandardInterfaces.h:256
sdbus::ObjectManager_proxy
Definition StandardInterfaces.h:159
sdbus::ObjectPath
Definition Types.h:160
sdbus::Peer_proxy
Definition StandardInterfaces.h:41
sdbus::Properties_adaptor
Definition StandardInterfaces.h:214
sdbus::Properties_proxy
Definition StandardInterfaces.h:106
sdbus::Variant
Definition Types.h:54