sdbus-c++ 2.3.1
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
611 template <typename Rep, typename Period>
613 , const std::chrono::duration<Rep, Period>& timeout
615
632 virtual void registerSignalHandler( const InterfaceName& interfaceName
633 , const SignalName& signalName
634 , signal_handler signalHandler ) = 0;
635
652 [[nodiscard]] virtual Slot registerSignalHandler( const InterfaceName& interfaceName
653 , const SignalName& signalName
654 , signal_handler signalHandler
655 , return_slot_t ) = 0;
656
657 protected: // Internal API for efficiency reasons used by high-level API helper classes
658 friend MethodInvoker;
659 friend AsyncMethodInvoker;
660 friend SignalSubscriber;
661
662 [[nodiscard]] virtual MethodCall createMethodCall(const char* interfaceName, const char* methodName) const = 0;
663 virtual void registerSignalHandler( const char* interfaceName
664 , const char* signalName
665 , signal_handler signalHandler ) = 0;
666 [[nodiscard]] virtual Slot registerSignalHandler( const char* interfaceName
667 , const char* signalName
668 , signal_handler signalHandler
669 , return_slot_t ) = 0;
670
671 public: // New virtual functions in sdbus-c++ v2, for ABI compatibility
693
707 , uint64_t timeout
708 , with_awaitable_t ) = 0;
709 };
710
711 /********************************************/
721 class PendingAsyncCall
722 {
723 public:
724 PendingAsyncCall() = default;
725
733 void cancel();
734
743 [[nodiscard]] bool isPending() const;
744
745 private:
746 friend internal::Proxy;
747 explicit PendingAsyncCall(std::weak_ptr<void> callInfo);
748
749 std::weak_ptr<void> callInfo_;
750 };
751
752 // Out-of-line member definitions
753
754 template <typename Rep, typename Period>
755 inline MethodReply IProxy::callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout)
756 {
757 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
758 return callMethod(message, microsecs.count());
759 }
760
761 template <typename Rep, typename Period>
763 , async_reply_handler asyncReplyCallback
764 , const std::chrono::duration<Rep, Period>& timeout )
765 {
766 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
767 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count());
768 }
769
770 template <typename Rep, typename Period>
771 inline Slot IProxy::callMethodAsync( const MethodCall& message
772 , async_reply_handler asyncReplyCallback
773 , const std::chrono::duration<Rep, Period>& timeout
774 , return_slot_t )
775 {
776 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
777 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count(), return_slot);
778 }
779
780 template <typename Rep, typename Period>
781 inline std::future<MethodReply> IProxy::callMethodAsync( const MethodCall& message
782 , const std::chrono::duration<Rep, Period>& timeout
783 , with_future_t )
784 {
785 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
786 return callMethodAsync(message, microsecs.count(), with_future);
787 }
788
789 template <typename Rep, typename Period>
791 , const std::chrono::duration<Rep, Period>& timeout
792 , with_awaitable_t ) {
793 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
794 return callMethodAsync(message, microsecs.count(), with_awaitable);
795 }
796
797 inline MethodInvoker IProxy::callMethod(const MethodName& methodName)
798 {
799 return {*this, methodName};
800 }
801
802 inline MethodInvoker IProxy::callMethod(const std::string& methodName)
803 {
804 return {*this, methodName.c_str()};
805 }
806
807 inline MethodInvoker IProxy::callMethod(const char* methodName)
808 {
809 return {*this, methodName};
810 }
811
812 inline AsyncMethodInvoker IProxy::callMethodAsync(const MethodName& methodName)
813 {
814 return {*this, methodName};
815 }
816
817 inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
818 {
819 return {*this, methodName.c_str()};
820 }
821
822 inline AsyncMethodInvoker IProxy::callMethodAsync(const char* methodName)
823 {
824 return {*this, methodName};
825 }
826
827 inline SignalSubscriber IProxy::uponSignal(const SignalName& signalName)
828 {
829 return {*this, signalName};
830 }
831
832 inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
833 {
834 return {*this, signalName.c_str()};
835 }
836
837 inline SignalSubscriber IProxy::uponSignal(const char* signalName)
838 {
839 return {*this, signalName};
840 }
841
842 inline PropertyGetter IProxy::getProperty(const PropertyName& propertyName)
843 {
844 return {*this, propertyName};
845 }
846
847 inline PropertyGetter IProxy::getProperty(std::string_view propertyName)
848 {
849 return {*this, std::move(propertyName)};
850 }
851
852 inline AsyncPropertyGetter IProxy::getPropertyAsync(const PropertyName& propertyName)
853 {
854 return {*this, propertyName};
855 }
856
857 inline AsyncPropertyGetter IProxy::getPropertyAsync(std::string_view propertyName)
858 {
859 return {*this, std::move(propertyName)};
860 }
861
862 inline PropertySetter IProxy::setProperty(const PropertyName& propertyName)
863 {
864 return {*this, propertyName};
865 }
866
867 inline PropertySetter IProxy::setProperty(std::string_view propertyName)
868 {
869 return {*this, std::move(propertyName)};
870 }
871
872 inline AsyncPropertySetter IProxy::setPropertyAsync(const PropertyName& propertyName)
873 {
874 return {*this, propertyName};
875 }
876
877 inline AsyncPropertySetter IProxy::setPropertyAsync(std::string_view propertyName)
878 {
879 return {*this, std::move(propertyName)};
880 }
881
883 {
884 return AllPropertiesGetter(*this);
885 }
886
891
914 [[nodiscard]] std::unique_ptr<IProxy> createProxy( IConnection& connection
915 , ServiceName destination
916 , ObjectPath objectPath );
917
940 [[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
941 , ServiceName destination
942 , ObjectPath objectPath );
943
967 [[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
968 , ServiceName destination
969 , ObjectPath objectPath
971
977 [[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy( std::unique_ptr<IConnection>&& connection
978 , ServiceName destination
979 , ObjectPath objectPath );
980
998 [[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
999 , ObjectPath objectPath );
1000
1019 [[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
1020 , ObjectPath objectPath
1022
1028 [[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy(ServiceName destination, ObjectPath objectPath);
1029
1030} // namespace sdbus
1031
1032#include <sdbus-c++/ConvenienceApiClasses.inl> // NOLINT(misc-header-include-cycle)
1033
1034#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:842
AsyncPropertyGetter getPropertyAsync(const PropertyName &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:852
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:887
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:862
MethodInvoker callMethod(const MethodName &methodName)
Calls method on the D-Bus object.
Definition IProxy.h:797
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:812
SignalSubscriber uponSignal(const SignalName &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:827
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:882
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:872
Definition Types.h:251
Definition Message.h:81
Definition Message.h:278
Definition Message.h:303
Definition Types.h:208
Definition IProxy.h:722
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