sdbus-c++ 2.3.0
High-level C++ D-Bus library based on systemd D-Bus implementation
Loading...
Searching...
No Matches
IProxy.h
Go to the documentation of this file.
1
26
27#ifndef SDBUS_CXX_IPROXY_H_
28#define SDBUS_CXX_IPROXY_H_
29
32#include <sdbus-c++/Awaitable.h>
33
34#include <chrono>
35#include <functional>
36#include <future>
37#include <memory>
38#include <string>
39#include <string_view>
40
41// Forward declarations
42namespace sdbus {
43 class MethodCall;
44 class MethodReply;
45 class IConnection;
46 class ObjectPath;
47 class PendingAsyncCall;
48 namespace internal {
49 class Proxy;
50 } // namespace internal
51} // namespace sdbus
52
53namespace sdbus {
54
55 /********************************************/
69 class IProxy
70 {
71 public: // High-level, convenience API
72 virtual ~IProxy() = default;
73
94 [[nodiscard]] MethodInvoker callMethod(const MethodName& methodName);
95
99 [[nodiscard]] MethodInvoker callMethod(const std::string& methodName);
100
104 [[nodiscard]] MethodInvoker callMethod(const char* methodName);
105
129 [[nodiscard]] AsyncMethodInvoker callMethodAsync(const MethodName& methodName);
130
134 [[nodiscard]] AsyncMethodInvoker callMethodAsync(const std::string& methodName);
135
139 [[nodiscard]] AsyncMethodInvoker callMethodAsync(const char* methodName);
140
165 [[nodiscard]] SignalSubscriber uponSignal(const SignalName& signalName);
166
170 [[nodiscard]] SignalSubscriber uponSignal(const std::string& signalName);
171
175 [[nodiscard]] SignalSubscriber uponSignal(const char* signalName);
176
197 [[nodiscard]] PropertyGetter getProperty(const PropertyName& propertyName);
198
202 [[nodiscard]] PropertyGetter getProperty(std::string_view propertyName);
203
222 [[nodiscard]] AsyncPropertyGetter getPropertyAsync(const PropertyName& propertyName);
223
227 [[nodiscard]] AsyncPropertyGetter getPropertyAsync(std::string_view propertyName);
228
249 [[nodiscard]] PropertySetter setProperty(const PropertyName& propertyName);
250
254 [[nodiscard]] PropertySetter setProperty(std::string_view propertyName);
255
274 [[nodiscard]] AsyncPropertySetter setPropertyAsync(const PropertyName& propertyName);
275
279 [[nodiscard]] AsyncPropertySetter setPropertyAsync(std::string_view propertyName);
280
297
315
321 [[nodiscard]] virtual sdbus::IConnection& getConnection() const = 0;
322
326 [[nodiscard]] virtual const ObjectPath& getObjectPath() const = 0;
327
341 [[nodiscard]] virtual Message getCurrentlyProcessedMessage() const = 0;
342
352 virtual void unregister() = 0;
353
354 // Lower-level, message-based API
368 [[nodiscard]] virtual MethodCall createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName) const = 0;
369
398 virtual MethodReply callMethod(const MethodCall& message) = 0;
399
429 virtual MethodReply callMethod(const MethodCall& message, uint64_t timeout) = 0;
430
434 template <typename Rep, typename Period>
435 MethodReply callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout);
436
458 virtual PendingAsyncCall callMethodAsync(const MethodCall& message, async_reply_handler asyncReplyCallback) = 0;
459
482 [[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
483 , async_reply_handler asyncReplyCallback
484 , return_slot_t ) = 0;
485
509 , async_reply_handler asyncReplyCallback
510 , uint64_t timeout ) = 0;
511
535 [[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
536 , async_reply_handler asyncReplyCallback
537 , uint64_t timeout
538 , return_slot_t ) = 0;
539
543 template <typename Rep, typename Period>
545 , async_reply_handler asyncReplyCallback
546 , const std::chrono::duration<Rep, Period>& timeout );
547
551 template <typename Rep, typename Period>
552 [[nodiscard]] Slot callMethodAsync( const MethodCall& message
553 , async_reply_handler asyncReplyCallback
554 , const std::chrono::duration<Rep, Period>& timeout
555 , return_slot_t );
556
575 virtual std::future<MethodReply> callMethodAsync(const MethodCall& message, with_future_t) = 0;
576
596 virtual std::future<MethodReply> callMethodAsync( const MethodCall& message
597 , uint64_t timeout
598 , with_future_t ) = 0;
599
603 template <typename Rep, typename Period>
604 std::future<MethodReply> callMethodAsync( const MethodCall& message
605 , const std::chrono::duration<Rep, Period>& timeout
606 , with_future_t );
607
629
643 , uint64_t timeout
644 , with_awaitable_t ) = 0;
645
649 template <typename Rep, typename Period>
651 , const std::chrono::duration<Rep, Period>& timeout
653
670 virtual void registerSignalHandler( const InterfaceName& interfaceName
671 , const SignalName& signalName
672 , signal_handler signalHandler ) = 0;
673
690 [[nodiscard]] virtual Slot registerSignalHandler( const InterfaceName& interfaceName
691 , const SignalName& signalName
692 , signal_handler signalHandler
693 , return_slot_t ) = 0;
694
695 protected: // Internal API for efficiency reasons used by high-level API helper classes
696 friend MethodInvoker;
697 friend AsyncMethodInvoker;
698 friend SignalSubscriber;
699
700 [[nodiscard]] virtual MethodCall createMethodCall(const char* interfaceName, const char* methodName) const = 0;
701 virtual void registerSignalHandler( const char* interfaceName
702 , const char* signalName
703 , signal_handler signalHandler ) = 0;
704 [[nodiscard]] virtual Slot registerSignalHandler( const char* interfaceName
705 , const char* signalName
706 , signal_handler signalHandler
707 , return_slot_t ) = 0;
708 };
709
710 /********************************************/
720 class PendingAsyncCall
721 {
722 public:
723 PendingAsyncCall() = default;
724
732 void cancel();
733
742 [[nodiscard]] bool isPending() const;
743
744 private:
745 friend internal::Proxy;
746 explicit PendingAsyncCall(std::weak_ptr<void> callInfo);
747
748 std::weak_ptr<void> callInfo_;
749 };
750
751 // Out-of-line member definitions
752
753 template <typename Rep, typename Period>
754 inline MethodReply IProxy::callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout)
755 {
756 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
757 return callMethod(message, microsecs.count());
758 }
759
760 template <typename Rep, typename Period>
762 , async_reply_handler asyncReplyCallback
763 , const std::chrono::duration<Rep, Period>& timeout )
764 {
765 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
766 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count());
767 }
768
769 template <typename Rep, typename Period>
770 inline Slot IProxy::callMethodAsync( const MethodCall& message
771 , async_reply_handler asyncReplyCallback
772 , const std::chrono::duration<Rep, Period>& timeout
773 , return_slot_t )
774 {
775 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
776 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count(), return_slot);
777 }
778
779 template <typename Rep, typename Period>
780 inline std::future<MethodReply> IProxy::callMethodAsync( const MethodCall& message
781 , const std::chrono::duration<Rep, Period>& timeout
782 , with_future_t )
783 {
784 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
785 return callMethodAsync(message, microsecs.count(), with_future);
786 }
787
788 template <typename Rep, typename Period>
790 , const std::chrono::duration<Rep, Period>& timeout
791 , with_awaitable_t ) {
792 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
793 return callMethodAsync(message, microsecs.count(), with_awaitable);
794 }
795
796 inline MethodInvoker IProxy::callMethod(const MethodName& methodName)
797 {
798 return {*this, methodName};
799 }
800
801 inline MethodInvoker IProxy::callMethod(const std::string& methodName)
802 {
803 return {*this, methodName.c_str()};
804 }
805
806 inline MethodInvoker IProxy::callMethod(const char* methodName)
807 {
808 return {*this, methodName};
809 }
810
811 inline AsyncMethodInvoker IProxy::callMethodAsync(const MethodName& methodName)
812 {
813 return {*this, methodName};
814 }
815
816 inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
817 {
818 return {*this, methodName.c_str()};
819 }
820
821 inline AsyncMethodInvoker IProxy::callMethodAsync(const char* methodName)
822 {
823 return {*this, methodName};
824 }
825
826 inline SignalSubscriber IProxy::uponSignal(const SignalName& signalName)
827 {
828 return {*this, signalName};
829 }
830
831 inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
832 {
833 return {*this, signalName.c_str()};
834 }
835
836 inline SignalSubscriber IProxy::uponSignal(const char* signalName)
837 {
838 return {*this, signalName};
839 }
840
841 inline PropertyGetter IProxy::getProperty(const PropertyName& propertyName)
842 {
843 return {*this, propertyName};
844 }
845
846 inline PropertyGetter IProxy::getProperty(std::string_view propertyName)
847 {
848 return {*this, std::move(propertyName)};
849 }
850
851 inline AsyncPropertyGetter IProxy::getPropertyAsync(const PropertyName& propertyName)
852 {
853 return {*this, propertyName};
854 }
855
856 inline AsyncPropertyGetter IProxy::getPropertyAsync(std::string_view propertyName)
857 {
858 return {*this, std::move(propertyName)};
859 }
860
861 inline PropertySetter IProxy::setProperty(const PropertyName& propertyName)
862 {
863 return {*this, propertyName};
864 }
865
866 inline PropertySetter IProxy::setProperty(std::string_view propertyName)
867 {
868 return {*this, std::move(propertyName)};
869 }
870
871 inline AsyncPropertySetter IProxy::setPropertyAsync(const PropertyName& propertyName)
872 {
873 return {*this, propertyName};
874 }
875
876 inline AsyncPropertySetter IProxy::setPropertyAsync(std::string_view propertyName)
877 {
878 return {*this, std::move(propertyName)};
879 }
880
882 {
883 return AllPropertiesGetter(*this);
884 }
885
890
913 [[nodiscard]] std::unique_ptr<IProxy> createProxy( IConnection& connection
914 , ServiceName destination
915 , ObjectPath objectPath );
916
939 [[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
940 , ServiceName destination
941 , ObjectPath objectPath );
942
966 [[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
967 , ServiceName destination
968 , ObjectPath objectPath
970
976 [[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy( std::unique_ptr<IConnection>&& connection
977 , ServiceName destination
978 , ObjectPath objectPath );
979
997 [[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
998 , ObjectPath objectPath );
999
1018 [[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
1019 , ObjectPath objectPath
1021
1027 [[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy(ServiceName destination, ObjectPath objectPath);
1028
1029} // namespace sdbus
1030
1031#include <sdbus-c++/ConvenienceApiClasses.inl> // NOLINT(misc-header-include-cycle)
1032
1033#endif /* SDBUS_CXX_IPROXY_H_ */
std::unique_ptr< IProxy > createProxy(IConnection &connection, ServiceName destination, ObjectPath objectPath)
Creates a proxy object for a specific remote D-Bus object.
std::unique_ptr< IProxy > createLightWeightProxy(std::unique_ptr< IConnection > &&connection, ServiceName destination, ObjectPath objectPath)
Creates a light-weight proxy object for a specific remote D-Bus object.
Definition ConvenienceApiClasses.h:256
Definition ConvenienceApiClasses.h:270
Definition ConvenienceApiClasses.h:193
Definition ConvenienceApiClasses.h:233
Definition Awaitable.h:110
Definition IConnection.h:61
Definition IProxy.h:70
PropertyGetter getProperty(const PropertyName &propertyName)
Gets value of a property of the D-Bus object.
Definition IProxy.h:841
AsyncPropertyGetter getPropertyAsync(const PropertyName &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:851
virtual std::future< MethodReply > callMethodAsync(const MethodCall &message, with_future_t)=0
Calls method on the D-Bus object asynchronously.
virtual PendingAsyncCall callMethodAsync(const MethodCall &message, async_reply_handler asyncReplyCallback, uint64_t timeout)=0
Calls method on the D-Bus object asynchronously, with custom timeout.
virtual MethodReply callMethod(const MethodCall &message)=0
Calls method on the remote D-Bus object.
AsyncAllPropertiesGetter getAllPropertiesAsync()
Gets values of all properties of the D-Bus object asynchronously.
Definition IProxy.h:886
virtual void registerSignalHandler(const InterfaceName &interfaceName, const SignalName &signalName, signal_handler signalHandler)=0
Registers a handler for the desired signal emitted by the D-Bus object.
virtual Awaitable< MethodReply > callMethodAsync(const MethodCall &message, uint64_t timeout, with_awaitable_t)=0
Calls method on the D-Bus object asynchronously, with custom timeout.
virtual Slot callMethodAsync(const MethodCall &message, async_reply_handler asyncReplyCallback, return_slot_t)=0
Calls method on the D-Bus object asynchronously.
PropertySetter setProperty(const PropertyName &propertyName)
Sets value of a property of the D-Bus object.
Definition IProxy.h:861
MethodInvoker callMethod(const MethodName &methodName)
Calls method on the D-Bus object.
Definition IProxy.h:796
virtual Message getCurrentlyProcessedMessage() const =0
Provides access to the currently processed D-Bus message.
virtual MethodCall createMethodCall(const InterfaceName &interfaceName, const MethodName &methodName) const =0
Creates a method call message.
virtual void unregister()=0
Unregisters proxy's signal handlers and stops receiving replies to pending async calls.
virtual Awaitable< MethodReply > callMethodAsync(const MethodCall &message, with_awaitable_t)=0
Calls method on the D-Bus object asynchronously.
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout)=0
Calls method on the remote D-Bus object.
virtual sdbus::IConnection & getConnection() const =0
Provides D-Bus connection used by the proxy.
virtual std::future< MethodReply > callMethodAsync(const MethodCall &message, uint64_t timeout, with_future_t)=0
Calls method on the D-Bus object asynchronously, with custom timeout.
virtual Slot registerSignalHandler(const InterfaceName &interfaceName, const SignalName &signalName, signal_handler signalHandler, return_slot_t)=0
Registers a handler for the desired signal emitted by the D-Bus object.
virtual Slot callMethodAsync(const MethodCall &message, async_reply_handler asyncReplyCallback, uint64_t timeout, return_slot_t)=0
Calls method on the D-Bus object asynchronously, with custom timeout.
virtual const ObjectPath & getObjectPath() const =0
Returns object path of the underlying DBus object.
AsyncMethodInvoker callMethodAsync(const MethodName &methodName)
Calls method on the D-Bus object asynchronously.
Definition IProxy.h:811
SignalSubscriber uponSignal(const SignalName &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:826
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:881
virtual PendingAsyncCall callMethodAsync(const MethodCall &message, async_reply_handler asyncReplyCallback)=0
Calls method on the D-Bus object asynchronously.
AsyncPropertySetter setPropertyAsync(const PropertyName &propertyName)
Sets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:871
Definition Types.h:251
Definition Message.h:81
Definition Message.h:278
Definition Message.h:303
Definition Types.h:208
Definition IProxy.h:721
void cancel()
Cancels the delivery of the pending asynchronous call result.
bool isPending() const
Answers whether the asynchronous call is still pending.
Definition ConvenienceApiClasses.h:178
Definition ConvenienceApiClasses.h:213
Definition TypeTraits.h:101
Definition TypeTraits.h:88
Definition TypeTraits.h:113
Definition TypeTraits.h:104