sdbus-c++  1.3.0
High-level C++ D-Bus library based on systemd D-Bus implementation
IProxy.h
Go to the documentation of this file.
1 
27 #ifndef SDBUS_CXX_IPROXY_H_
28 #define SDBUS_CXX_IPROXY_H_
29 
31 #include <sdbus-c++/TypeTraits.h>
32 #include <string>
33 #include <memory>
34 #include <functional>
35 #include <chrono>
36 #include <future>
37 
38 // Forward declarations
39 namespace sdbus {
40  class MethodCall;
41  class MethodReply;
42  class IConnection;
43  class PendingAsyncCall;
44  namespace internal {
45  class Proxy;
46  }
47 }
48 
49 namespace sdbus {
50 
51  /********************************************/
65  class IProxy
66  {
67  public:
68  virtual ~IProxy() = default;
69 
83  virtual MethodCall createMethodCall(const std::string& interfaceName, const std::string& methodName) = 0;
84 
103  virtual MethodReply callMethod(const MethodCall& message, uint64_t timeout = 0) = 0;
104 
108  template <typename _Rep, typename _Period>
109  MethodReply callMethod(const MethodCall& message, const std::chrono::duration<_Rep, _Period>& timeout);
110 
127  virtual PendingAsyncCall callMethod(const MethodCall& message, async_reply_handler asyncReplyCallback, uint64_t timeout = 0) = 0;
128 
132  template <typename _Rep, typename _Period>
133  PendingAsyncCall callMethod(const MethodCall& message, async_reply_handler asyncReplyCallback, const std::chrono::duration<_Rep, _Period>& timeout);
134 
144  virtual void registerSignalHandler( const std::string& interfaceName
145  , const std::string& signalName
146  , signal_handler signalHandler ) = 0;
147 
156  virtual void unregisterSignalHandler( const std::string& interfaceName
157  , const std::string& signalName ) = 0;
158 
167  virtual void finishRegistration() = 0;
168 
178  virtual void unregister() = 0;
179 
199  [[nodiscard]] MethodInvoker callMethod(const std::string& methodName);
200 
223  [[nodiscard]] AsyncMethodInvoker callMethodAsync(const std::string& methodName);
224 
243  [[nodiscard]] SignalSubscriber uponSignal(const std::string& signalName);
244 
260  [[nodiscard]] SignalUnsubscriber muteSignal(const std::string& signalName);
261 
279  [[nodiscard]] PropertyGetter getProperty(const std::string& propertyName);
280 
298  [[nodiscard]] PropertySetter setProperty(const std::string& propertyName);
299 
305  virtual sdbus::IConnection& getConnection() const = 0;
306 
310  virtual const std::string& getObjectPath() const = 0;
311 
326  virtual const Message* getCurrentlyProcessedMessage() const = 0;
327 
344  virtual std::future<MethodReply> callMethod(const MethodCall& message, with_future_t) = 0;
345  virtual std::future<MethodReply> callMethod(const MethodCall& message, uint64_t timeout, with_future_t) = 0;
346 
350  template <typename _Rep, typename _Period>
351  std::future<MethodReply> callMethod( const MethodCall& message
352  , const std::chrono::duration<_Rep, _Period>& timeout
353  , with_future_t );
354  };
355 
356  /********************************************/
367  {
368  public:
369  PendingAsyncCall() = default;
370 
378  void cancel();
379 
388  bool isPending() const;
389 
390  private:
391  friend internal::Proxy;
392  PendingAsyncCall(std::weak_ptr<void> callData);
393 
394  private:
395  std::weak_ptr<void> callData_;
396  };
397 
398  // Out-of-line member definitions
399 
400  template <typename _Rep, typename _Period>
401  inline MethodReply IProxy::callMethod(const MethodCall& message, const std::chrono::duration<_Rep, _Period>& timeout)
402  {
403  auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
404  return callMethod(message, microsecs.count());
405  }
406 
407  template <typename _Rep, typename _Period>
408  inline PendingAsyncCall IProxy::callMethod(const MethodCall& message, async_reply_handler asyncReplyCallback, const std::chrono::duration<_Rep, _Period>& timeout)
409  {
410  auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
411  return callMethod(message, std::move(asyncReplyCallback), microsecs.count());
412  }
413 
414  template <typename _Rep, typename _Period>
415  inline std::future<MethodReply> IProxy::callMethod( const MethodCall& message
416  , const std::chrono::duration<_Rep, _Period>& timeout
417  , with_future_t )
418  {
419  auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
420  return callMethod(message, microsecs.count(), with_future);
421  }
422 
423  inline MethodInvoker IProxy::callMethod(const std::string& methodName)
424  {
425  return MethodInvoker(*this, methodName);
426  }
427 
428  inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
429  {
430  return AsyncMethodInvoker(*this, methodName);
431  }
432 
433  inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
434  {
435  return SignalSubscriber(*this, signalName);
436  }
437 
438  inline SignalUnsubscriber IProxy::muteSignal(const std::string& signalName)
439  {
440  return SignalUnsubscriber(*this, signalName);
441  }
442 
443  inline PropertyGetter IProxy::getProperty(const std::string& propertyName)
444  {
445  return PropertyGetter(*this, propertyName);
446  }
447 
448  inline PropertySetter IProxy::setProperty(const std::string& propertyName)
449  {
450  return PropertySetter(*this, propertyName);
451  }
452 
472  [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( sdbus::IConnection& connection
473  , std::string destination
474  , std::string objectPath );
475 
495  [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<sdbus::IConnection>&& connection
496  , std::string destination
497  , std::string objectPath );
498 
518  [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<sdbus::IConnection>&& connection
519  , std::string destination
520  , std::string objectPath
522 
540  [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
541  , std::string objectPath );
542 
561  [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::string destination
562  , std::string objectPath
564 
565 }
566 
568 
569 #endif /* SDBUS_CXX_IPROXY_H_ */
Definition: Message.h:259
virtual MethodCall createMethodCall(const std::string &interfaceName, const std::string &methodName)=0
Creates a method call message.
Definition: ConvenienceApiClasses.h:189
virtual void unregisterSignalHandler(const std::string &interfaceName, const std::string &signalName)=0
Unregisters the handler of the desired signal.
Definition: Message.h:75
virtual sdbus::IConnection & getConnection() const =0
Provides D-Bus connection used by the proxy.
virtual void registerSignalHandler(const std::string &interfaceName, const std::string &signalName, signal_handler signalHandler)=0
Registers a handler for the desired signal emitted by the proxied D-Bus object.
bool isPending() const
Answers whether the asynchronous call is still pending.
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the proxied D-Bus object.
Definition: IProxy.h:433
virtual const Message * getCurrentlyProcessedMessage() const =0
Provides currently processed D-Bus message.
AsyncMethodInvoker callMethodAsync(const std::string &methodName)
Calls method on the proxied D-Bus object asynchronously.
Definition: IProxy.h:428
Definition: ConvenienceApiClasses.h:164
virtual void finishRegistration()=0
Finishes the registration of signal handlers.
Definition: TypeTraits.h:94
std::unique_ptr< sdbus::IProxy > createProxy(sdbus::IConnection &connection, std::string destination, std::string objectPath)
Creates a proxy object for a specific remote D-Bus object.
Definition: ConvenienceApiClasses.h:224
void cancel()
Cancels the delivery of the pending asynchronous call result.
SignalUnsubscriber muteSignal(const std::string &signalName)
Unregisters signal handler of a given signal of the proxied D-Bus object.
Definition: IProxy.h:438
Definition: ConvenienceApiClasses.h:235
Definition: TypeTraits.h:91
virtual void unregister()=0
Unregisters proxy&#39;s signal handlers and stops receving replies to pending async calls.
Definition: ConvenienceApiClasses.h:246
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the proxied D-Bus object.
Definition: IProxy.h:448
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the proxied D-Bus object.
Definition: IProxy.h:443
Definition: IProxy.h:366
virtual const std::string & getObjectPath() const =0
Returns object path of the underlying DBus object.
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the proxied D-Bus object.
Definition: AdaptorInterfaces.h:36
Definition: IConnection.h:49
Definition: ConvenienceApiClasses.h:211
Definition: IProxy.h:65
Definition: Message.h:231