sdbus-c++  1.3.0
High-level C++ D-Bus library based on systemd D-Bus implementation
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 
37 namespace 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:
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
76  {
77  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
78 
79  protected:
81  : proxy_(&proxy)
82  {
83  }
84 
86  Introspectable_proxy& operator=(const Introspectable_proxy&) = delete;
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
106  {
107  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
108 
109  protected:
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
159  {
160  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
161 
162  protected:
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;
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
214  {
215  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
216 
217  protected:
219  : object_(&object)
220  {
221  }
222 
223  Properties_adaptor(const Properties_adaptor&) = delete;
224  Properties_adaptor& operator=(const Properties_adaptor&) = delete;
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 
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 
267  ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
269  ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = default;
270 
271  ~ObjectManager_adaptor() = default;
272 
273  private:
274  sdbus::IObject* object_;
275  };
276 
289  {
290  protected:
291  explicit ManagedObject_adaptor(sdbus::IObject& object)
292  : object_(&object)
293  {
294  }
295 
297  ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
299  ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = default;
300 
301  ~ManagedObject_adaptor() = default;
302 
303  public:
310  {
311  object_->emitInterfacesAddedSignal();
312  }
313 
319  void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
320  {
321  object_->emitInterfacesAddedSignal(interfaces);
322  }
323 
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_ */
Definition: Types.h:53
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...
void emitInterfacesAddedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition: StandardInterfaces.h:319
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the proxied D-Bus object.
Definition: IProxy.h:433
Definition: StandardInterfaces.h:158
void emitInterfacesRemovedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition: StandardInterfaces.h:339
Definition: StandardInterfaces.h:40
Definition: Types.h:159
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition: StandardInterfaces.h:329
virtual void addObjectManager()=0
Adds an ObjectManager interface at the path of this D-Bus object.
Definition: StandardInterfaces.h:213
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the proxied D-Bus object.
Definition: IProxy.h:448
Definition: StandardInterfaces.h:105
Managed Object Convenience Adaptor.
Definition: StandardInterfaces.h:288
Object Manager Convenience Adaptor.
Definition: StandardInterfaces.h:255
Definition: IObject.h:59
virtual void emitInterfacesRemovedSignal()=0
Emits InterfacesRemoved signal on this object path.
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the proxied D-Bus object.
Definition: IProxy.h:443
virtual void emitInterfacesAddedSignal()=0
Emits InterfacesAdded signal on this object path.
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the proxied D-Bus object.
Definition: AdaptorInterfaces.h:36
Definition: StandardInterfaces.h:75
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition: StandardInterfaces.h:309
Definition: IProxy.h:65