sdbus-c++  1.1.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() = default;
51 
52  public:
53  void Ping()
54  {
55  proxy_.callMethod("Ping").onInterface(INTERFACE_NAME);
56  }
57 
58  std::string GetMachineId()
59  {
60  std::string machineUUID;
61  proxy_.callMethod("GetMachineId").onInterface(INTERFACE_NAME).storeResultsTo(machineUUID);
62  return machineUUID;
63  }
64 
65  private:
66  sdbus::IProxy& proxy_;
67  };
68 
69  // Proxy for introspection
71  {
72  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
73 
74  protected:
76  : proxy_(proxy)
77  {
78  }
79 
80  ~Introspectable_proxy() = default;
81 
82  public:
83  std::string Introspect()
84  {
85  std::string xml;
86  proxy_.callMethod("Introspect").onInterface(INTERFACE_NAME).storeResultsTo(xml);
87  return xml;
88  }
89 
90  private:
91  sdbus::IProxy& proxy_;
92  };
93 
94  // Proxy for properties
96  {
97  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
98 
99  protected:
101  : proxy_(proxy)
102  {
103  proxy_
104  .uponSignal("PropertiesChanged")
105  .onInterface(INTERFACE_NAME)
106  .call([this]( const std::string& interfaceName
107  , const std::map<std::string, sdbus::Variant>& changedProperties
108  , const std::vector<std::string>& invalidatedProperties )
109  {
110  this->onPropertiesChanged(interfaceName, changedProperties, invalidatedProperties);
111  });
112  }
113 
114  ~Properties_proxy() = default;
115 
116  virtual void onPropertiesChanged( const std::string& interfaceName
117  , const std::map<std::string, sdbus::Variant>& changedProperties
118  , const std::vector<std::string>& invalidatedProperties ) = 0;
119 
120  public:
121  sdbus::Variant Get(const std::string& interfaceName, const std::string& propertyName)
122  {
123  return proxy_.getProperty(propertyName).onInterface(interfaceName);
124  }
125 
126  void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
127  {
128  proxy_.setProperty(propertyName).onInterface(interfaceName).toValue(value);
129  }
130 
131  std::map<std::string, sdbus::Variant> GetAll(const std::string& interfaceName)
132  {
133  std::map<std::string, sdbus::Variant> props;
134  proxy_.callMethod("GetAll").onInterface(INTERFACE_NAME).withArguments(interfaceName).storeResultsTo(props);
135  return props;
136  }
137 
138  private:
139  sdbus::IProxy& proxy_;
140  };
141 
142  // Proxy for object manager
144  {
145  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
146 
147  protected:
149  : proxy_(proxy)
150  {
151  proxy_
152  .uponSignal("InterfacesAdded")
153  .onInterface(INTERFACE_NAME)
154  .call([this]( const sdbus::ObjectPath& objectPath
155  , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties )
156  {
157  this->onInterfacesAdded(objectPath, interfacesAndProperties);
158  });
159 
160  proxy_
161  .uponSignal("InterfacesRemoved")
162  .onInterface(INTERFACE_NAME)
163  .call([this]( const sdbus::ObjectPath& objectPath
164  , const std::vector<std::string>& interfaces )
165  {
166  this->onInterfacesRemoved(objectPath, interfaces);
167  });
168  }
169 
170  ~ObjectManager_proxy() = default;
171 
172  virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
173  , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) = 0;
174  virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
175  , const std::vector<std::string>& interfaces) = 0;
176 
177  public:
178  std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> GetManagedObjects()
179  {
180  std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
181  proxy_.callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
182  return objectsInterfacesAndProperties;
183  }
184 
185  private:
186  sdbus::IProxy& proxy_;
187  };
188 
189  // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
190  // is provided by underlying libsystemd implementation. The exception is Properties_adaptor,
191  // ObjectManager_adaptor and ManagedObject_adaptor, which provide convenience functionality to emit signals.
192 
193  // Adaptor for properties
195  {
196  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
197 
198  protected:
200  : object_(object)
201  {
202  }
203 
204  ~Properties_adaptor() = default;
205 
206  public:
207  void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& properties)
208  {
209  object_.emitPropertiesChangedSignal(interfaceName, properties);
210  }
211 
212  void emitPropertiesChangedSignal(const std::string& interfaceName)
213  {
214  object_.emitPropertiesChangedSignal(interfaceName);
215  }
216 
217  private:
218  sdbus::IObject& object_;
219  };
220 
232  {
233  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
234 
235  protected:
236  explicit ObjectManager_adaptor(sdbus::IObject& object)
237  : object_(object)
238  {
239  object_.addObjectManager();
240  }
241 
242  ~ObjectManager_adaptor() = default;
243 
244  private:
245  sdbus::IObject& object_;
246  };
247 
260  {
261  protected:
262  explicit ManagedObject_adaptor(sdbus::IObject& object) : object_(object)
263  {
264  }
265 
266  ~ManagedObject_adaptor() = default;
267 
268  public:
275  {
276  object_.emitInterfacesAddedSignal();
277  }
278 
284  void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
285  {
286  object_.emitInterfacesAddedSignal(interfaces);
287  }
288 
295  {
296  object_.emitInterfacesRemovedSignal();
297  }
298 
304  void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
305  {
306  object_.emitInterfacesRemovedSignal(interfaces);
307  }
308 
309  private:
310  sdbus::IObject& object_;
311  };
312 
313 }
314 
315 #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:64
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the proxied D-Bus object.
Definition: IProxy.h:410
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the proxied D-Bus object.
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the proxied D-Bus object.
Definition: IProxy.h:405
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the proxied D-Bus object.
Definition: IProxy.h:395
Definition: StandardInterfaces.h:71
Managed Object Convenience Adaptor.
Definition: StandardInterfaces.h:260
void emitInterfacesRemovedSignal()
Emits InterfacesRemoved signal for this object path.
Definition: StandardInterfaces.h:294
void emitInterfacesRemovedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesRemoved signal for this object path.
Definition: StandardInterfaces.h:304
void emitInterfacesAddedSignal()
Emits InterfacesAdded signal for this object path.
Definition: StandardInterfaces.h:274
void emitInterfacesAddedSignal(const std::vector< std::string > &interfaces)
Emits InterfacesAdded signal for this object path.
Definition: StandardInterfaces.h:284
Object Manager Convenience Adaptor.
Definition: StandardInterfaces.h:232
Definition: StandardInterfaces.h:144
Definition: Types.h:153
Definition: StandardInterfaces.h:41
Definition: StandardInterfaces.h:195
Definition: StandardInterfaces.h:96
Definition: Types.h:54