sdbus-c++ 2.0.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
27#ifndef SDBUS_CXX_IPROXY_H_
28#define SDBUS_CXX_IPROXY_H_
29
32
33#include <chrono>
34#include <functional>
35#include <future>
36#include <memory>
37#include <string>
38#include <string_view>
39
40// Forward declarations
41namespace sdbus {
42 class MethodCall;
43 class MethodReply;
44 class IConnection;
45 class ObjectPath;
46 class PendingAsyncCall;
47 namespace internal {
48 class Proxy;
49 }
50}
51
52namespace sdbus {
53
54 /********************************************/
68 class IProxy
69 {
70 public: // High-level, convenience API
71 virtual ~IProxy() = default;
72
93 [[nodiscard]] MethodInvoker callMethod(const MethodName& methodName);
94
98 [[nodiscard]] MethodInvoker callMethod(const std::string& methodName);
99
103 [[nodiscard]] MethodInvoker callMethod(const char* methodName);
104
128 [[nodiscard]] AsyncMethodInvoker callMethodAsync(const MethodName& methodName);
129
133 [[nodiscard]] AsyncMethodInvoker callMethodAsync(const std::string& methodName);
134
138 [[nodiscard]] AsyncMethodInvoker callMethodAsync(const char* methodName);
139
164 [[nodiscard]] SignalSubscriber uponSignal(const SignalName& signalName);
165
169 [[nodiscard]] SignalSubscriber uponSignal(const std::string& signalName);
170
174 [[nodiscard]] SignalSubscriber uponSignal(const char* signalName);
175
196 [[nodiscard]] PropertyGetter getProperty(const PropertyName& propertyName);
197
201 [[nodiscard]] PropertyGetter getProperty(std::string_view propertyName);
202
221 [[nodiscard]] AsyncPropertyGetter getPropertyAsync(const PropertyName& propertyName);
222
226 [[nodiscard]] AsyncPropertyGetter getPropertyAsync(std::string_view propertyName);
227
248 [[nodiscard]] PropertySetter setProperty(const PropertyName& propertyName);
249
253 [[nodiscard]] PropertySetter setProperty(std::string_view propertyName);
254
273 [[nodiscard]] AsyncPropertySetter setPropertyAsync(const PropertyName& propertyName);
274
278 [[nodiscard]] AsyncPropertySetter setPropertyAsync(std::string_view propertyName);
279
296
314
320 [[nodiscard]] virtual sdbus::IConnection& getConnection() const = 0;
321
325 [[nodiscard]] virtual const ObjectPath& getObjectPath() const = 0;
326
340 [[nodiscard]] virtual Message getCurrentlyProcessedMessage() const = 0;
341
351 virtual void unregister() = 0;
352
353 public: // Lower-level, message-based API
367 [[nodiscard]] virtual MethodCall createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName) = 0;
368
397 virtual MethodReply callMethod(const MethodCall& message) = 0;
398
428 virtual MethodReply callMethod(const MethodCall& message, uint64_t timeout) = 0;
429
433 template <typename _Rep, typename _Period>
434 MethodReply callMethod(const MethodCall& message, const std::chrono::duration<_Rep, _Period>& timeout);
435
457 virtual PendingAsyncCall callMethodAsync(const MethodCall& message, async_reply_handler asyncReplyCallback) = 0;
458
481 [[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
482 , async_reply_handler asyncReplyCallback
483 , return_slot_t ) = 0;
484
508 , async_reply_handler asyncReplyCallback
509 , uint64_t timeout ) = 0;
510
534 [[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
535 , async_reply_handler asyncReplyCallback
536 , uint64_t timeout
537 , return_slot_t ) = 0;
538
542 template <typename _Rep, typename _Period>
544 , async_reply_handler asyncReplyCallback
545 , const std::chrono::duration<_Rep, _Period>& timeout );
546
550 template <typename _Rep, typename _Period>
551 [[nodiscard]] Slot callMethodAsync( const MethodCall& message
552 , async_reply_handler asyncReplyCallback
553 , const std::chrono::duration<_Rep, _Period>& timeout
554 , return_slot_t );
555
575 virtual std::future<MethodReply> callMethodAsync(const MethodCall& message, with_future_t) = 0;
576
597 virtual std::future<MethodReply> callMethodAsync( const MethodCall& message
598 , uint64_t timeout
599 , with_future_t ) = 0;
600
604 template <typename _Rep, typename _Period>
605 std::future<MethodReply> callMethodAsync( const MethodCall& message
606 , const std::chrono::duration<_Rep, _Period>& timeout
607 , with_future_t );
608
625 virtual void registerSignalHandler( const InterfaceName& interfaceName
626 , const SignalName& signalName
627 , signal_handler signalHandler ) = 0;
628
645 [[nodiscard]] virtual Slot registerSignalHandler( const InterfaceName& interfaceName
646 , const SignalName& signalName
647 , signal_handler signalHandler
648 , return_slot_t ) = 0;
649
650 protected: // Internal API for efficiency reasons used by high-level API helper classes
651 friend MethodInvoker;
652 friend AsyncMethodInvoker;
653 friend SignalSubscriber;
654
655 [[nodiscard]] virtual MethodCall createMethodCall(const char* interfaceName, const char* methodName) = 0;
656 virtual void registerSignalHandler( const char* interfaceName
657 , const char* signalName
658 , signal_handler signalHandler ) = 0;
659 [[nodiscard]] virtual Slot registerSignalHandler( const char* interfaceName
660 , const char* signalName
661 , signal_handler signalHandler
662 , return_slot_t ) = 0;
663 };
664
665 /********************************************/
676 {
677 public:
678 PendingAsyncCall() = default;
679
687 void cancel();
688
697 [[nodiscard]] bool isPending() const;
698
699 private:
700 friend internal::Proxy;
701 PendingAsyncCall(std::weak_ptr<void> callInfo);
702
703 private:
704 std::weak_ptr<void> callInfo_;
705 };
706
707 // Out-of-line member definitions
708
709 template <typename _Rep, typename _Period>
710 inline MethodReply IProxy::callMethod(const MethodCall& message, const std::chrono::duration<_Rep, _Period>& timeout)
711 {
712 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
713 return callMethod(message, microsecs.count());
714 }
715
716 template <typename _Rep, typename _Period>
718 , async_reply_handler asyncReplyCallback
719 , const std::chrono::duration<_Rep, _Period>& timeout )
720 {
721 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
722 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count());
723 }
724
725 template <typename _Rep, typename _Period>
726 inline Slot IProxy::callMethodAsync( const MethodCall& message
727 , async_reply_handler asyncReplyCallback
728 , const std::chrono::duration<_Rep, _Period>& timeout
729 , return_slot_t )
730 {
731 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
732 return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count(), return_slot);
733 }
734
735 template <typename _Rep, typename _Period>
736 inline std::future<MethodReply> IProxy::callMethodAsync( const MethodCall& message
737 , const std::chrono::duration<_Rep, _Period>& timeout
738 , with_future_t )
739 {
740 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
741 return callMethodAsync(message, microsecs.count(), with_future);
742 }
743
745 {
746 return MethodInvoker(*this, methodName);
747 }
748
749 inline MethodInvoker IProxy::callMethod(const std::string& methodName)
750 {
751 return MethodInvoker(*this, methodName.c_str());
752 }
753
754 inline MethodInvoker IProxy::callMethod(const char* methodName)
755 {
756 return MethodInvoker(*this, methodName);
757 }
758
760 {
761 return AsyncMethodInvoker(*this, methodName);
762 }
763
764 inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
765 {
766 return AsyncMethodInvoker(*this, methodName.c_str());
767 }
768
769 inline AsyncMethodInvoker IProxy::callMethodAsync(const char* methodName)
770 {
771 return AsyncMethodInvoker(*this, methodName);
772 }
773
775 {
776 return SignalSubscriber(*this, signalName);
777 }
778
779 inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
780 {
781 return SignalSubscriber(*this, signalName.c_str());
782 }
783
784 inline SignalSubscriber IProxy::uponSignal(const char* signalName)
785 {
786 return SignalSubscriber(*this, signalName);
787 }
788
790 {
791 return PropertyGetter(*this, propertyName);
792 }
793
794 inline PropertyGetter IProxy::getProperty(std::string_view propertyName)
795 {
796 return PropertyGetter(*this, std::move(propertyName));
797 }
798
800 {
801 return AsyncPropertyGetter(*this, propertyName);
802 }
803
804 inline AsyncPropertyGetter IProxy::getPropertyAsync(std::string_view propertyName)
805 {
806 return AsyncPropertyGetter(*this, std::move(propertyName));
807 }
808
810 {
811 return PropertySetter(*this, propertyName);
812 }
813
814 inline PropertySetter IProxy::setProperty(std::string_view propertyName)
815 {
816 return PropertySetter(*this, std::move(propertyName));
817 }
818
820 {
821 return AsyncPropertySetter(*this, propertyName);
822 }
823
824 inline AsyncPropertySetter IProxy::setPropertyAsync(std::string_view propertyName)
825 {
826 return AsyncPropertySetter(*this, std::move(propertyName));
827 }
828
830 {
831 return AllPropertiesGetter(*this);
832 }
833
835 {
836 return AsyncAllPropertiesGetter(*this);
837 }
838
861 [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( sdbus::IConnection& connection
862 , ServiceName destination
863 , ObjectPath objectPath );
864
887 [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<sdbus::IConnection>&& connection
888 , ServiceName destination
889 , ObjectPath objectPath );
890
914 [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( std::unique_ptr<sdbus::IConnection>&& connection
915 , ServiceName destination
916 , ObjectPath objectPath
918
936 [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( ServiceName destination
937 , ObjectPath objectPath );
938
957 [[nodiscard]] std::unique_ptr<sdbus::IProxy> createProxy( ServiceName destination
958 , ObjectPath objectPath
960
961}
962
964
965#endif /* SDBUS_CXX_IPROXY_H_ */
std::unique_ptr< sdbus::IProxy > createProxy(sdbus::IConnection &connection, ServiceName destination, ObjectPath objectPath)
Creates a proxy object for a specific remote D-Bus object.
Definition ConvenienceApiClasses.h:255
Definition ConvenienceApiClasses.h:270
Definition ConvenienceApiClasses.h:124
Definition ConvenienceApiClasses.h:191
Definition ConvenienceApiClasses.h:232
Definition Types.h:197
Definition IConnection.h:61
Definition IProxy.h:69
PropertyGetter getProperty(const PropertyName &propertyName)
Gets value of a property of the D-Bus object.
Definition IProxy.h:789
AsyncPropertyGetter getPropertyAsync(const PropertyName &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:799
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:834
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 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:809
MethodInvoker callMethod(const MethodName &methodName)
Calls method on the D-Bus object.
Definition IProxy.h:744
virtual Message getCurrentlyProcessedMessage() const =0
Provides access to the currently processed D-Bus message.
virtual void unregister()=0
Unregisters proxy's signal handlers and stops receiving replies to pending async calls.
virtual MethodCall createMethodCall(const InterfaceName &interfaceName, const MethodName &methodName)=0
Creates a method call message.
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:759
SignalSubscriber uponSignal(const SignalName &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:774
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:829
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:819
Definition Types.h:220
Definition Types.h:240
Definition Message.h:81
Definition Message.h:261
Definition ConvenienceApiClasses.h:94
Definition Message.h:286
Definition Types.h:177
Definition IProxy.h:676
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:175
Definition ConvenienceApiClasses.h:211
Definition ConvenienceApiClasses.h:154
Definition TypeTraits.h:101
Definition TypeTraits.h:88
Definition TypeTraits.h:104