sdbus-c++ 2.4.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
206 [[nodiscard]] PropertyChangeSubscriber uponPropertyChange(PropertyName propertyName);
207
211 [[nodiscard]] PropertyChangeSubscriber uponPropertyChange(std::string_view propertyName);
212
233 [[nodiscard]] PropertyGetter getProperty(const PropertyName& propertyName);
234
238 [[nodiscard]] PropertyGetter getProperty(std::string_view propertyName);
239
258 [[nodiscard]] AsyncPropertyGetter getPropertyAsync(const PropertyName& propertyName);
259
263 [[nodiscard]] AsyncPropertyGetter getPropertyAsync(std::string_view propertyName);
264
285 [[nodiscard]] PropertySetter setProperty(const PropertyName& propertyName);
286
290 [[nodiscard]] PropertySetter setProperty(std::string_view propertyName);
291
310 [[nodiscard]] AsyncPropertySetter setPropertyAsync(const PropertyName& propertyName);
311
315 [[nodiscard]] AsyncPropertySetter setPropertyAsync(std::string_view propertyName);
316
333
351
357 [[nodiscard]] virtual sdbus::IConnection& getConnection() const = 0;
358
362 [[nodiscard]] virtual const ObjectPath& getObjectPath() const = 0;
363
377 [[nodiscard]] virtual Message getCurrentlyProcessedMessage() const = 0;
378
388 virtual void unregister() = 0;
389
390 // Lower-level, message-based API
404 [[nodiscard]] virtual MethodCall createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName) const = 0;
405
434 virtual MethodReply callMethod(const MethodCall& message) = 0;
435
465 virtual MethodReply callMethod(const MethodCall& message, uint64_t timeout) = 0;
466
470 template <typename Rep, typename Period>
471 MethodReply callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout);
472
494 virtual PendingAsyncCall callMethodAsync(const MethodCall& message, async_reply_handler asyncReplyCallback) = 0;
495
518 [[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
519 , async_reply_handler asyncReplyCallback
520 , return_slot_t ) = 0;
521
545 , async_reply_handler asyncReplyCallback
546 , uint64_t timeout ) = 0;
547
571 [[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
572 , async_reply_handler asyncReplyCallback
573 , uint64_t timeout
574 , return_slot_t ) = 0;
575
579 template <typename Rep, typename Period>
581 , async_reply_handler asyncReplyCallback
582 , const std::chrono::duration<Rep, Period>& timeout );
583
587 template <typename Rep, typename Period>
588 [[nodiscard]] Slot callMethodAsync( const MethodCall& message
589 , async_reply_handler asyncReplyCallback
590 , const std::chrono::duration<Rep, Period>& timeout
591 , return_slot_t );
592
611 virtual std::future<MethodReply> callMethodAsync(const MethodCall& message, with_future_t) = 0;
612
632 virtual std::future<MethodReply> callMethodAsync( const MethodCall& message
633 , uint64_t timeout
634 , with_future_t ) = 0;
635
639 template <typename Rep, typename Period>
640 std::future<MethodReply> callMethodAsync( const MethodCall& message
641 , const std::chrono::duration<Rep, Period>& timeout
642 , with_future_t );
643
647 template <typename Rep, typename Period>
649 , const std::chrono::duration<Rep, Period>& timeout
651
668 virtual void registerSignalHandler( const InterfaceName& interfaceName
669 , const SignalName& signalName
670 , signal_handler signalHandler ) = 0;
671
688 [[nodiscard]] virtual Slot registerSignalHandler( const InterfaceName& interfaceName
689 , const SignalName& signalName
690 , signal_handler signalHandler
691 , return_slot_t ) = 0;
692
693 protected: // Internal API for efficiency reasons used by high-level API helper classes
694 friend MethodInvoker;
695 friend AsyncMethodInvoker;
696 friend SignalSubscriber;
697 friend PropertyChangeSubscriber;
698
699 [[nodiscard]] virtual MethodCall createMethodCall(const char* interfaceName, const char* methodName) const = 0;
700 virtual void registerSignalHandler( const char* interfaceName
701 , const char* signalName
702 , signal_handler signalHandler ) = 0;
703 [[nodiscard]] virtual Slot registerSignalHandler( const char* interfaceName
704 , const char* signalName
705 , signal_handler signalHandler
706 , return_slot_t ) = 0;
707
708 public: // New virtual functions in sdbus-c++ v2, for ABI compatibility
730
744 , uint64_t timeout
745 , with_awaitable_t ) = 0;
746 };
747
748 /********************************************/
758 class PendingAsyncCall
759 {
760 public:
761 PendingAsyncCall() = default;
762
770 void cancel();
771
780 [[nodiscard]] bool isPending() const;
781
782 private:
783 friend internal::Proxy;
784 explicit PendingAsyncCall(std::weak_ptr<void> callInfo);
785
786 std::weak_ptr<void> callInfo_;
787 };
788
789 // Out-of-line member definitions
790
791 template <typename Rep, typename Period>
792 inline MethodReply IProxy::callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout)
793 {
794 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
795 return callMethod(message, microsecs.count());
796 }
797
798 template <typename Rep, typename Period>
800 , async_reply_handler asyncReplyCallback
801 , const std::chrono::duration<Rep, Period>& timeout )
802 {
803 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
804 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count());
805 }
806
807 template <typename Rep, typename Period>
808 inline Slot IProxy::callMethodAsync( const MethodCall& message
809 , async_reply_handler asyncReplyCallback
810 , const std::chrono::duration<Rep, Period>& timeout
811 , return_slot_t )
812 {
813 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
814 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count(), return_slot);
815 }
816
817 template <typename Rep, typename Period>
818 inline std::future<MethodReply> IProxy::callMethodAsync( const MethodCall& message
819 , const std::chrono::duration<Rep, Period>& timeout
820 , with_future_t )
821 {
822 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
823 return callMethodAsync(message, microsecs.count(), with_future);
824 }
825
826 template <typename Rep, typename Period>
828 , const std::chrono::duration<Rep, Period>& timeout
829 , with_awaitable_t ) {
830 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
831 return callMethodAsync(message, microsecs.count(), with_awaitable);
832 }
833
834 inline MethodInvoker IProxy::callMethod(const MethodName& methodName)
835 {
836 return {*this, methodName};
837 }
838
839 inline MethodInvoker IProxy::callMethod(const std::string& methodName)
840 {
841 return {*this, methodName.c_str()};
842 }
843
844 inline MethodInvoker IProxy::callMethod(const char* methodName)
845 {
846 return {*this, methodName};
847 }
848
849 inline AsyncMethodInvoker IProxy::callMethodAsync(const MethodName& methodName)
850 {
851 return {*this, methodName};
852 }
853
854 inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
855 {
856 return {*this, methodName.c_str()};
857 }
858
859 inline AsyncMethodInvoker IProxy::callMethodAsync(const char* methodName)
860 {
861 return {*this, methodName};
862 }
863
864 inline SignalSubscriber IProxy::uponSignal(const SignalName& signalName)
865 {
866 return {*this, signalName};
867 }
868
869 inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
870 {
871 return {*this, signalName.c_str()};
872 }
873
874 inline SignalSubscriber IProxy::uponSignal(const char* signalName)
875 {
876 return {*this, signalName};
877 }
878
879 inline PropertyChangeSubscriber IProxy::uponPropertyChange(PropertyName propertyName)
880 {
881 return {*this, std::move(propertyName)};
882 }
883
884 inline PropertyChangeSubscriber IProxy::uponPropertyChange(std::string_view propertyName)
885 {
886 return {*this, PropertyName{std::string{propertyName}}};
887 }
888
889 inline PropertyGetter IProxy::getProperty(const PropertyName& propertyName)
890 {
891 return {*this, propertyName};
892 }
893
894 inline PropertyGetter IProxy::getProperty(std::string_view propertyName)
895 {
896 return {*this, std::move(propertyName)};
897 }
898
899 inline AsyncPropertyGetter IProxy::getPropertyAsync(const PropertyName& propertyName)
900 {
901 return {*this, propertyName};
902 }
903
904 inline AsyncPropertyGetter IProxy::getPropertyAsync(std::string_view propertyName)
905 {
906 return {*this, std::move(propertyName)};
907 }
908
909 inline PropertySetter IProxy::setProperty(const PropertyName& propertyName)
910 {
911 return {*this, propertyName};
912 }
913
914 inline PropertySetter IProxy::setProperty(std::string_view propertyName)
915 {
916 return {*this, std::move(propertyName)};
917 }
918
919 inline AsyncPropertySetter IProxy::setPropertyAsync(const PropertyName& propertyName)
920 {
921 return {*this, propertyName};
922 }
923
924 inline AsyncPropertySetter IProxy::setPropertyAsync(std::string_view propertyName)
925 {
926 return {*this, std::move(propertyName)};
927 }
928
930 {
931 return AllPropertiesGetter(*this);
932 }
933
938
961 [[nodiscard]] std::unique_ptr<IProxy> createProxy( IConnection& connection
962 , ServiceName destination
963 , ObjectPath objectPath );
964
987 [[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
988 , ServiceName destination
989 , ObjectPath objectPath );
990
1014 [[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
1015 , ServiceName destination
1016 , ObjectPath objectPath
1018
1024 [[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy( std::unique_ptr<IConnection>&& connection
1025 , ServiceName destination
1026 , ObjectPath objectPath );
1027
1045 [[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
1046 , ObjectPath objectPath );
1047
1066 [[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
1067 , ObjectPath objectPath
1069
1075 [[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy(ServiceName destination, ObjectPath objectPath);
1076
1077} // namespace sdbus
1078
1079#include <sdbus-c++/ConvenienceApiClasses.inl> // NOLINT(misc-header-include-cycle)
1080
1081#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:291
Definition ConvenienceApiClasses.h:305
Definition ConvenienceApiClasses.h:219
Definition ConvenienceApiClasses.h:264
Definition Awaitable.h:110
Definition IConnection.h:62
Definition IProxy.h:70
PropertyGetter getProperty(const PropertyName &propertyName)
Gets value of a property of the D-Bus object.
Definition IProxy.h:889
AsyncPropertyGetter getPropertyAsync(const PropertyName &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:899
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:934
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.
PropertyChangeSubscriber uponPropertyChange(PropertyName propertyName)
Registers handler for changes of a given property of the D-Bus object.
Definition IProxy.h:879
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:909
MethodInvoker callMethod(const MethodName &methodName)
Calls method on the D-Bus object.
Definition IProxy.h:834
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:849
SignalSubscriber uponSignal(const SignalName &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:864
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:929
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:919
Definition Types.h:251
Definition Message.h:82
Definition Message.h:280
Definition Message.h:305
Definition Types.h:208
Definition IProxy.h:759
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:204
Definition ConvenienceApiClasses.h:240
Definition TypeTraits.h:106
Definition TypeTraits.h:90
Definition TypeTraits.h:118
Definition TypeTraits.h:109