Loading...
Searching...
No Matches
IProxy.h
Go to the documentation of this file.
#ifndef SDBUS_CXX_IPROXY_H_
#define SDBUS_CXX_IPROXY_H_
#include <sdbus-c++/ConvenienceApiClasses.h>
#include <sdbus-c++/TypeTraits.h>
#include <sdbus-c++/Awaitable.h>
#include <chrono>
#include <functional>
#include <future>
#include <memory>
#include <string>
#include <string_view>
// Forward declarations
namespace sdbus {
class MethodCall;
class MethodReply;
class IConnection;
class ObjectPath;
class PendingAsyncCall;
namespace internal {
class Proxy;
} // namespace internal
} // namespace sdbus
namespace sdbus {
/********************************************/
class IProxy
{
public: // High-level, convenience API
virtual ~IProxy() = default;
[[nodiscard]] MethodInvoker callMethod(const MethodName& methodName);
[[nodiscard]] MethodInvoker callMethod(const std::string& methodName);
[[nodiscard]] MethodInvoker callMethod(const char* methodName);
[[nodiscard]] AsyncMethodInvoker callMethodAsync(const MethodName& methodName);
[[nodiscard]] AsyncMethodInvoker callMethodAsync(const std::string& methodName);
[[nodiscard]] AsyncMethodInvoker callMethodAsync(const char* methodName);
[[nodiscard]] SignalSubscriber uponSignal(const SignalName& signalName);
[[nodiscard]] SignalSubscriber uponSignal(const std::string& signalName);
[[nodiscard]] SignalSubscriber uponSignal(const char* signalName);
[[nodiscard]] PropertyGetter getProperty(const PropertyName& propertyName);
[[nodiscard]] PropertyGetter getProperty(std::string_view propertyName);
[[nodiscard]] AsyncPropertyGetter getPropertyAsync(const PropertyName& propertyName);
[[nodiscard]] AsyncPropertyGetter getPropertyAsync(std::string_view propertyName);
[[nodiscard]] PropertySetter setProperty(const PropertyName& propertyName);
[[nodiscard]] PropertySetter setProperty(std::string_view propertyName);
[[nodiscard]] AsyncPropertySetter setPropertyAsync(const PropertyName& propertyName);
[[nodiscard]] AsyncPropertySetter setPropertyAsync(std::string_view propertyName);
[[nodiscard]] AllPropertiesGetter getAllProperties();
[[nodiscard]] AsyncAllPropertiesGetter getAllPropertiesAsync();
[[nodiscard]] virtual sdbus::IConnection& getConnection() const = 0;
[[nodiscard]] virtual const ObjectPath& getObjectPath() const = 0;
[[nodiscard]] virtual Message getCurrentlyProcessedMessage() const = 0;
virtual void unregister() = 0;
// Lower-level, message-based API
[[nodiscard]] virtual MethodCall createMethodCall(const InterfaceName& interfaceName, const MethodName& methodName) const = 0;
virtual MethodReply callMethod(const MethodCall& message) = 0;
virtual MethodReply callMethod(const MethodCall& message, uint64_t timeout) = 0;
template <typename Rep, typename Period>
MethodReply callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout);
virtual PendingAsyncCall callMethodAsync(const MethodCall& message, async_reply_handler asyncReplyCallback) = 0;
[[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, return_slot_t ) = 0;
virtual PendingAsyncCall callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, uint64_t timeout ) = 0;
[[nodiscard]] virtual Slot callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, uint64_t timeout
, return_slot_t ) = 0;
template <typename Rep, typename Period>
PendingAsyncCall callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, const std::chrono::duration<Rep, Period>& timeout );
template <typename Rep, typename Period>
[[nodiscard]] Slot callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, const std::chrono::duration<Rep, Period>& timeout
, return_slot_t );
virtual std::future<MethodReply> callMethodAsync(const MethodCall& message, with_future_t) = 0;
virtual std::future<MethodReply> callMethodAsync( const MethodCall& message
, uint64_t timeout
, with_future_t ) = 0;
template <typename Rep, typename Period>
std::future<MethodReply> callMethodAsync( const MethodCall& message
, const std::chrono::duration<Rep, Period>& timeout
, with_future_t );
template <typename Rep, typename Period>
Awaitable<MethodReply> callMethodAsync( const MethodCall& message
, const std::chrono::duration<Rep, Period>& timeout
, with_awaitable_t );
virtual void registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler ) = 0;
[[nodiscard]] virtual Slot registerSignalHandler( const InterfaceName& interfaceName
, const SignalName& signalName
, signal_handler signalHandler
, return_slot_t ) = 0;
protected: // Internal API for efficiency reasons used by high-level API helper classes
friend MethodInvoker;
friend AsyncMethodInvoker;
friend SignalSubscriber;
[[nodiscard]] virtual MethodCall createMethodCall(const char* interfaceName, const char* methodName) const = 0;
virtual void registerSignalHandler( const char* interfaceName
, const char* signalName
, signal_handler signalHandler ) = 0;
[[nodiscard]] virtual Slot registerSignalHandler( const char* interfaceName
, const char* signalName
, signal_handler signalHandler
, return_slot_t ) = 0;
public: // New virtual functions in sdbus-c++ v2, for ABI compatibility
virtual Awaitable<MethodReply> callMethodAsync(const MethodCall& message, with_awaitable_t) = 0;
virtual Awaitable<MethodReply> callMethodAsync( const MethodCall& message
, uint64_t timeout
, with_awaitable_t ) = 0;
};
/********************************************/
class PendingAsyncCall
{
public:
PendingAsyncCall() = default;
void cancel();
[[nodiscard]] bool isPending() const;
private:
friend internal::Proxy;
explicit PendingAsyncCall(std::weak_ptr<void> callInfo);
std::weak_ptr<void> callInfo_;
};
// Out-of-line member definitions
template <typename Rep, typename Period>
inline MethodReply IProxy::callMethod(const MethodCall& message, const std::chrono::duration<Rep, Period>& timeout)
{
auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
return callMethod(message, microsecs.count());
}
template <typename Rep, typename Period>
inline PendingAsyncCall IProxy::callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, const std::chrono::duration<Rep, Period>& timeout )
{
auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count());
}
template <typename Rep, typename Period>
inline Slot IProxy::callMethodAsync( const MethodCall& message
, async_reply_handler asyncReplyCallback
, const std::chrono::duration<Rep, Period>& timeout
, return_slot_t )
{
auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
return callMethodAsync(message, std::move(asyncReplyCallback), microsecs.count(), return_slot);
}
template <typename Rep, typename Period>
inline std::future<MethodReply> IProxy::callMethodAsync( const MethodCall& message
, const std::chrono::duration<Rep, Period>& timeout
, with_future_t )
{
auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
return callMethodAsync(message, microsecs.count(), with_future);
}
template <typename Rep, typename Period>
inline Awaitable<MethodReply> IProxy::callMethodAsync( const MethodCall& message
, const std::chrono::duration<Rep, Period>& timeout
, with_awaitable_t ) {
auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
return callMethodAsync(message, microsecs.count(), with_awaitable);
}
inline MethodInvoker IProxy::callMethod(const MethodName& methodName)
{
return {*this, methodName};
}
inline MethodInvoker IProxy::callMethod(const std::string& methodName)
{
return {*this, methodName.c_str()};
}
inline MethodInvoker IProxy::callMethod(const char* methodName)
{
return {*this, methodName};
}
inline AsyncMethodInvoker IProxy::callMethodAsync(const MethodName& methodName)
{
return {*this, methodName};
}
inline AsyncMethodInvoker IProxy::callMethodAsync(const std::string& methodName)
{
return {*this, methodName.c_str()};
}
inline AsyncMethodInvoker IProxy::callMethodAsync(const char* methodName)
{
return {*this, methodName};
}
inline SignalSubscriber IProxy::uponSignal(const SignalName& signalName)
{
return {*this, signalName};
}
inline SignalSubscriber IProxy::uponSignal(const std::string& signalName)
{
return {*this, signalName.c_str()};
}
inline SignalSubscriber IProxy::uponSignal(const char* signalName)
{
return {*this, signalName};
}
inline PropertyGetter IProxy::getProperty(const PropertyName& propertyName)
{
return {*this, propertyName};
}
inline PropertyGetter IProxy::getProperty(std::string_view propertyName)
{
return {*this, std::move(propertyName)};
}
inline AsyncPropertyGetter IProxy::getPropertyAsync(const PropertyName& propertyName)
{
return {*this, propertyName};
}
inline AsyncPropertyGetter IProxy::getPropertyAsync(std::string_view propertyName)
{
return {*this, std::move(propertyName)};
}
inline PropertySetter IProxy::setProperty(const PropertyName& propertyName)
{
return {*this, propertyName};
}
inline PropertySetter IProxy::setProperty(std::string_view propertyName)
{
return {*this, std::move(propertyName)};
}
inline AsyncPropertySetter IProxy::setPropertyAsync(const PropertyName& propertyName)
{
return {*this, propertyName};
}
inline AsyncPropertySetter IProxy::setPropertyAsync(std::string_view propertyName)
{
return {*this, std::move(propertyName)};
}
inline AllPropertiesGetter IProxy::getAllProperties()
{
return AllPropertiesGetter(*this);
}
inline AsyncAllPropertiesGetter IProxy::getAllPropertiesAsync()
{
return AsyncAllPropertiesGetter(*this);
}
[[nodiscard]] std::unique_ptr<IProxy> createProxy( IConnection& connection
, ServiceName destination
, ObjectPath objectPath );
[[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
, ServiceName destination
, ObjectPath objectPath );
[[nodiscard]] std::unique_ptr<IProxy> createProxy( std::unique_ptr<IConnection>&& connection
, ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t );
[[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy( std::unique_ptr<IConnection>&& connection
, ServiceName destination
, ObjectPath objectPath );
[[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
, ObjectPath objectPath );
[[nodiscard]] std::unique_ptr<IProxy> createProxy( ServiceName destination
, ObjectPath objectPath
, dont_run_event_loop_thread_t );
[[nodiscard]] std::unique_ptr<IProxy> createLightWeightProxy(ServiceName destination, ObjectPath objectPath);
} // namespace sdbus
#include <sdbus-c++/ConvenienceApiClasses.inl> // NOLINT(misc-header-include-cycle)
#endif /* SDBUS_CXX_IPROXY_H_ */
Awaitable.h
ConvenienceApiClasses.h
ConvenienceApiClasses.inl
sdbus::createProxy
std::unique_ptr< IProxy > createProxy(IConnection &connection, ServiceName destination, ObjectPath objectPath)
Creates a proxy object for a specific remote D-Bus object.
sdbus::createLightWeightProxy
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.
TypeTraits.h
sdbus::AllPropertiesGetter
Definition ConvenienceApiClasses.h:256
sdbus::AsyncAllPropertiesGetter
Definition ConvenienceApiClasses.h:270
sdbus::AsyncPropertyGetter
Definition ConvenienceApiClasses.h:193
sdbus::AsyncPropertySetter
Definition ConvenienceApiClasses.h:233
sdbus::Awaitable
Definition Awaitable.h:110
sdbus::IConnection
Definition IConnection.h:61
sdbus::IProxy
Definition IProxy.h:70
sdbus::IProxy::getProperty
PropertyGetter getProperty(const PropertyName &propertyName)
Gets value of a property of the D-Bus object.
Definition IProxy.h:842
sdbus::IProxy::getPropertyAsync
AsyncPropertyGetter getPropertyAsync(const PropertyName &propertyName)
Gets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:852
sdbus::IProxy::callMethodAsync
virtual std::future< MethodReply > callMethodAsync(const MethodCall &message, with_future_t)=0
Calls method on the D-Bus object asynchronously.
sdbus::IProxy::callMethodAsync
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.
sdbus::IProxy::callMethod
virtual MethodReply callMethod(const MethodCall &message)=0
Calls method on the remote D-Bus object.
sdbus::IProxy::getAllPropertiesAsync
AsyncAllPropertiesGetter getAllPropertiesAsync()
Gets values of all properties of the D-Bus object asynchronously.
Definition IProxy.h:887
sdbus::IProxy::registerSignalHandler
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.
sdbus::IProxy::callMethodAsync
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.
sdbus::IProxy::callMethodAsync
virtual Slot callMethodAsync(const MethodCall &message, async_reply_handler asyncReplyCallback, return_slot_t)=0
Calls method on the D-Bus object asynchronously.
sdbus::IProxy::setProperty
PropertySetter setProperty(const PropertyName &propertyName)
Sets value of a property of the D-Bus object.
Definition IProxy.h:862
sdbus::IProxy::callMethod
MethodInvoker callMethod(const MethodName &methodName)
Calls method on the D-Bus object.
Definition IProxy.h:797
sdbus::IProxy::getCurrentlyProcessedMessage
virtual Message getCurrentlyProcessedMessage() const =0
Provides access to the currently processed D-Bus message.
sdbus::IProxy::createMethodCall
virtual MethodCall createMethodCall(const InterfaceName &interfaceName, const MethodName &methodName) const =0
Creates a method call message.
sdbus::IProxy::unregister
virtual void unregister()=0
Unregisters proxy's signal handlers and stops receiving replies to pending async calls.
sdbus::IProxy::callMethodAsync
virtual Awaitable< MethodReply > callMethodAsync(const MethodCall &message, with_awaitable_t)=0
Calls method on the D-Bus object asynchronously.
sdbus::IProxy::callMethod
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout)=0
Calls method on the remote D-Bus object.
sdbus::IProxy::getConnection
virtual sdbus::IConnection & getConnection() const =0
Provides D-Bus connection used by the proxy.
sdbus::IProxy::callMethodAsync
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.
sdbus::IProxy::registerSignalHandler
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.
sdbus::IProxy::callMethodAsync
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.
sdbus::IProxy::getObjectPath
virtual const ObjectPath & getObjectPath() const =0
Returns object path of the underlying DBus object.
sdbus::IProxy::callMethodAsync
AsyncMethodInvoker callMethodAsync(const MethodName &methodName)
Calls method on the D-Bus object asynchronously.
Definition IProxy.h:812
sdbus::IProxy::uponSignal
SignalSubscriber uponSignal(const SignalName &signalName)
Registers signal handler for a given signal of the D-Bus object.
Definition IProxy.h:827
sdbus::IProxy::getAllProperties
AllPropertiesGetter getAllProperties()
Gets values of all properties of the D-Bus object.
Definition IProxy.h:882
sdbus::IProxy::callMethodAsync
virtual PendingAsyncCall callMethodAsync(const MethodCall &message, async_reply_handler asyncReplyCallback)=0
Calls method on the D-Bus object asynchronously.
sdbus::IProxy::setPropertyAsync
AsyncPropertySetter setPropertyAsync(const PropertyName &propertyName)
Sets value of a property of the D-Bus object asynchronously.
Definition IProxy.h:872
sdbus::InterfaceName
Definition Types.h:251
sdbus::Message
Definition Message.h:81
sdbus::MethodCall
Definition Message.h:278
sdbus::MethodReply
Definition Message.h:303
sdbus::ObjectPath
Definition Types.h:208
sdbus::PendingAsyncCall
Definition IProxy.h:722
sdbus::PendingAsyncCall::cancel
void cancel()
Cancels the delivery of the pending asynchronous call result.
sdbus::PendingAsyncCall::isPending
bool isPending() const
Answers whether the asynchronous call is still pending.
sdbus::PropertyGetter
Definition ConvenienceApiClasses.h:178
sdbus::PropertySetter
Definition ConvenienceApiClasses.h:213
sdbus::dont_run_event_loop_thread_t
Definition TypeTraits.h:101
sdbus::return_slot_t
Definition TypeTraits.h:88
sdbus::with_awaitable_t
Definition TypeTraits.h:113
sdbus::with_future_t
Definition TypeTraits.h:104