sdbus-c++  1.6.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  template <typename _Function>
142  PendingAsyncCall GetAsync(const std::string& interfaceName, const std::string& propertyName, _Function&& callback)
143  {
144  return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
145  }
146 
147  std::future<sdbus::Variant> GetAsync(const std::string& interfaceName, const std::string& propertyName, with_future_t)
148  {
149  return proxy_->getPropertyAsync(propertyName).onInterface(interfaceName).getResultAsFuture();
150  }
151 
152  void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
153  {
154  proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value);
155  }
156 
157  void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, dont_expect_reply_t)
158  {
159  proxy_->setProperty(propertyName).onInterface(interfaceName).toValue(value, dont_expect_reply);
160  }
161 
162  template <typename _Function>
163  PendingAsyncCall SetAsync(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, _Function&& callback)
164  {
165  return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).uponReplyInvoke(std::forward<_Function>(callback));
166  }
167 
168  std::future<void> SetAsync(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value, with_future_t)
169  {
170  return proxy_->setPropertyAsync(propertyName).onInterface(interfaceName).toValue(value).getResultAsFuture();
171  }
172 
173  std::map<std::string, sdbus::Variant> GetAll(const std::string& interfaceName)
174  {
175  return proxy_->getAllProperties().onInterface(interfaceName);
176  }
177 
178  template <typename _Function>
179  PendingAsyncCall GetAllAsync(const std::string& interfaceName, _Function&& callback)
180  {
181  return proxy_->getAllPropertiesAsync().onInterface(interfaceName).uponReplyInvoke(std::forward<_Function>(callback));
182  }
183 
184  std::future<std::map<std::string, sdbus::Variant>> GetAllAsync(const std::string& interfaceName, with_future_t)
185  {
186  return proxy_->getAllPropertiesAsync().onInterface(interfaceName).getResultAsFuture();
187  }
188 
189  private:
190  sdbus::IProxy* proxy_;
191  };
192 
193  // Proxy for object manager
195  {
196  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
197 
198  protected:
200  : proxy_(&proxy)
201  {
202  proxy_
203  ->uponSignal("InterfacesAdded")
204  .onInterface(INTERFACE_NAME)
205  .call([this]( const sdbus::ObjectPath& objectPath
206  , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties )
207  {
208  this->onInterfacesAdded(objectPath, interfacesAndProperties);
209  });
210 
211  proxy_->uponSignal("InterfacesRemoved")
212  .onInterface(INTERFACE_NAME)
213  .call([this]( const sdbus::ObjectPath& objectPath
214  , const std::vector<std::string>& interfaces )
215  {
216  this->onInterfacesRemoved(objectPath, interfaces);
217  });
218  }
219 
220  ObjectManager_proxy(const ObjectManager_proxy&) = delete;
221  ObjectManager_proxy& operator=(const ObjectManager_proxy&) = delete;
223  ObjectManager_proxy& operator=(ObjectManager_proxy&&) = default;
224 
225  ~ObjectManager_proxy() = default;
226 
227  virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
228  , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) = 0;
229  virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
230  , const std::vector<std::string>& interfaces) = 0;
231 
232  public:
233  std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> GetManagedObjects()
234  {
235  std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
236  proxy_->callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
237  return objectsInterfacesAndProperties;
238  }
239 
240  private:
241  sdbus::IProxy* proxy_;
242  };
243 
244  // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
245  // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
246  // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
247 
248  // Adaptor for properties
250  {
251  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
252 
253  protected:
255  : object_(&object)
256  {
257  }
258 
259  Properties_adaptor(const Properties_adaptor&) = delete;
260  Properties_adaptor& operator=(const Properties_adaptor&) = delete;
262  Properties_adaptor& operator=(Properties_adaptor&&) = default;
263 
264  ~Properties_adaptor() = default;
265 
266  public:
267  void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& properties)
268  {
269  object_->emitPropertiesChangedSignal(interfaceName, properties);
270  }
271 
272  void emitPropertiesChangedSignal(const std::string& interfaceName)
273  {
274  object_->emitPropertiesChangedSignal(interfaceName);
275  }
276 
277  private:
278  sdbus::IObject* object_;
279  };
280 
292  {
293  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
294 
295  protected:
296  explicit ObjectManager_adaptor(sdbus::IObject& object)
297  : object_(&object)
298  {
299  object_->addObjectManager();
300  }
301 
303  ObjectManager_adaptor& operator=(const ObjectManager_adaptor&) = delete;
305  ObjectManager_adaptor& operator=(ObjectManager_adaptor&&) = default;
306 
307  ~ObjectManager_adaptor() = default;
308 
309  private:
310  sdbus::IObject* object_;
311  };
312 
325  {
326  protected:
327  explicit ManagedObject_adaptor(sdbus::IObject& object)
328  : object_(&object)
329  {
330  }
331 
333  ManagedObject_adaptor& operator=(const ManagedObject_adaptor&) = delete;
335  ManagedObject_adaptor& operator=(ManagedObject_adaptor&&) = default;
336 
337  ~ManagedObject_adaptor() = default;
338 
339  public:
346  {
347  object_->emitInterfacesAddedSignal();
348  }
349 
355  void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
356  {
357  object_->emitInterfacesAddedSignal(interfaces);
358  }
359 
366  {
367  object_->emitInterfacesRemovedSignal();
368  }
369 
375  void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
376  {
377  object_->emitInterfacesRemovedSignal(interfaces);
378  }
379 
380  private:
381  sdbus::IObject* object_;
382  };
383 
384 }
385 
386 #endif /* SDBUS_CXX_STANDARDINTERFACES_H_ */
Definition: IObject.h:60
virtual void emitInterfacesRemovedSignal()=0
Emits InterfacesRemoved signal on 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.
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.
Definition: IProxy.h:66
AsyncAllPropertiesGetter getAllPropertiesAsync()
Gets values of all properties of the D-Bus object asynchronously.
Definition: IProxy.h:546
AsyncPropertyGetter getPropertyAsync(const std::string &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition: IProxy.h:526
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the D-Bus object.
Definition: IProxy.h:531
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the D-Bus object.
AsyncPropertySetter setPropertyAsync(const std::string &propertyName)
Sets value of a property of the D-Bus object asynchronously.
Definition: IProxy.h:536
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the D-Bus object.
Definition: IProxy.h:521
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition: IProxy.h:541
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition: IProxy.h:511
Definition: StandardInterfaces.h:76
Managed Object Convenience Adaptor.
Definition: StandardInterfaces.h:325
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition: StandardInterfaces.h:365
void emitInterfacesRemovedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition: StandardInterfaces.h:375
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition: StandardInterfaces.h:345
void emitInterfacesAddedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition: StandardInterfaces.h:355
Object Manager Convenience Adaptor.
Definition: StandardInterfaces.h:292
Definition: StandardInterfaces.h:195
Definition: Types.h:177
Definition: StandardInterfaces.h:41
Definition: IProxy.h:445
Definition: StandardInterfaces.h:250
Definition: StandardInterfaces.h:106
Definition: Types.h:54
Definition: TypeTraits.h:98
Definition: TypeTraits.h:95