27#ifndef SDBUS_CPP_CONVENIENCEAPICLASSES_INL_
28#define SDBUS_CPP_CONVENIENCEAPICLASSES_INL_
54 inline VTableAdder::VTableAdder(
IObject&
object, std::vector<VTableItem> vtable)
56 , vtable_(std::move(vtable))
60 inline void VTableAdder::forInterface(InterfaceName interfaceName)
62 object_.addVTable(std::move(interfaceName), std::move(vtable_));
65 inline void VTableAdder::forInterface(std::string interfaceName)
67 forInterface(InterfaceName{std::move(interfaceName)});
70 [[nodiscard]]
inline Slot VTableAdder::forInterface(InterfaceName interfaceName, return_slot_t)
72 return object_.addVTable(std::move(interfaceName), std::move(vtable_), return_slot);
75 [[nodiscard]]
inline Slot VTableAdder::forInterface(std::string interfaceName, return_slot_t)
77 return forInterface(InterfaceName{std::move(interfaceName)}, return_slot);
84 inline SignalEmitter::SignalEmitter(IObject&
object,
const SignalName& signalName)
85 : SignalEmitter(object, signalName.c_str())
89 inline SignalEmitter::SignalEmitter(IObject&
object,
const char* signalName)
91 , signalName_(signalName)
92 , exceptions_(std::uncaught_exceptions())
96 inline SignalEmitter::~SignalEmitter() noexcept(false)
99 if (std::uncaught_exceptions() != exceptions_)
111 object_.emitSignal(signal_);
114 inline SignalEmitter& SignalEmitter::onInterface(
const InterfaceName& interfaceName)
116 return onInterface(interfaceName.c_str());
119 inline SignalEmitter& SignalEmitter::onInterface(
const std::string& interfaceName)
121 return onInterface(interfaceName.c_str());
124 inline SignalEmitter& SignalEmitter::onInterface(
const char* interfaceName)
126 signal_ = object_.createSignal(interfaceName, signalName_);
131 template <
typename... Args>
132 inline void SignalEmitter::withArguments(Args&&... args)
134 assert(signal_.isValid());
136 detail::serialize_pack(signal_, std::forward<Args>(args)...);
143 inline MethodInvoker::MethodInvoker(IProxy& proxy,
const MethodName& methodName)
144 : MethodInvoker(proxy, methodName.c_str())
148 inline MethodInvoker::MethodInvoker(IProxy& proxy,
const char* methodName)
150 , methodName_(methodName)
151 , exceptions_(std::uncaught_exceptions())
155 inline MethodInvoker::~MethodInvoker() noexcept(false)
159 if (methodCalled_ || std::uncaught_exceptions() != exceptions_)
171 proxy_.callMethod(method_, timeout_);
174 inline MethodInvoker& MethodInvoker::onInterface(
const InterfaceName& interfaceName)
176 return onInterface(interfaceName.c_str());
179 inline MethodInvoker& MethodInvoker::onInterface(
const std::string& interfaceName)
181 return onInterface(interfaceName.c_str());
184 inline MethodInvoker& MethodInvoker::onInterface(
const char* interfaceName)
186 method_ = proxy_.createMethodCall(interfaceName, methodName_);
191 inline MethodInvoker& MethodInvoker::withTimeout(uint64_t usec)
198 template <
typename Rep,
typename Period>
199 inline MethodInvoker& MethodInvoker::withTimeout(
const std::chrono::duration<Rep, Period>& timeout)
201 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
202 return withTimeout(microsecs.count());
205 template <
typename... Args>
206 inline MethodInvoker& MethodInvoker::withArguments(Args&&... args)
208 assert(method_.isValid());
210 detail::serialize_pack(method_, std::forward<Args>(args)...);
215 template <
typename... Args>
216 inline void MethodInvoker::storeResultsTo(Args&... args)
218 assert(method_.isValid());
220 auto reply = proxy_.callMethod(method_, timeout_);
221 methodCalled_ =
true;
223 detail::deserialize_pack(reply, args...);
226 inline void MethodInvoker::dontExpectReply()
228 assert(method_.isValid());
230 method_.dontExpectReply();
237 inline AsyncMethodInvoker::AsyncMethodInvoker(IProxy& proxy,
const MethodName& methodName)
238 : AsyncMethodInvoker(proxy, methodName.c_str())
242 inline AsyncMethodInvoker::AsyncMethodInvoker(IProxy& proxy,
const char* methodName)
244 , methodName_(methodName)
248 inline AsyncMethodInvoker& AsyncMethodInvoker::onInterface(
const InterfaceName& interfaceName)
250 return onInterface(interfaceName.c_str());
253 inline AsyncMethodInvoker& AsyncMethodInvoker::onInterface(
const std::string& interfaceName)
255 return onInterface(interfaceName.c_str());
258 inline AsyncMethodInvoker& AsyncMethodInvoker::onInterface(
const char* interfaceName)
260 method_ = proxy_.createMethodCall(interfaceName, methodName_);
265 inline AsyncMethodInvoker& AsyncMethodInvoker::withTimeout(uint64_t usec)
272 template <
typename Rep,
typename Period>
273 inline AsyncMethodInvoker& AsyncMethodInvoker::withTimeout(
const std::chrono::duration<Rep, Period>& timeout)
275 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
276 return withTimeout(microsecs.count());
279 template <
typename... Args>
280 inline AsyncMethodInvoker& AsyncMethodInvoker::withArguments(Args&&... args)
282 assert(method_.isValid());
284 detail::serialize_pack(method_, std::forward<Args>(args)...);
289 template <
typename Function>
290 PendingAsyncCall AsyncMethodInvoker::uponReplyInvoke(Function&& callback)
292 assert(method_.isValid());
294 return proxy_.callMethodAsync(method_, makeAsyncReplyHandler(std::forward<Function>(callback)), timeout_);
297 template <
typename Function>
298 [[nodiscard]] Slot AsyncMethodInvoker::uponReplyInvoke(Function&& callback, return_slot_t)
300 assert(method_.isValid());
302 return proxy_.callMethodAsync( method_
303 , makeAsyncReplyHandler(std::forward<Function>(callback))
308 template <
typename Function>
309 inline async_reply_handler AsyncMethodInvoker::makeAsyncReplyHandler(Function&& callback)
311 return [callback = std::forward<Function>(callback)](MethodReply reply, std::optional<Error> error)
315 tuple_of_function_input_arg_types_t<Function> args;
324 catch (
const Error& err)
328 sdbus::apply(callback, err, args);
334 sdbus::apply(callback, std::move(error), args);
338 template <
typename... Args>
339 std::future<future_return_t<Args...>> AsyncMethodInvoker::getResultAsFuture()
341 auto promise = std::make_shared<std::promise<future_return_t<Args...>>>();
342 auto future = promise->get_future();
344 uponReplyInvoke([promise = std::move(promise)](std::optional<Error> error, Args... args)
347 if constexpr (!std::is_void_v<future_return_t<Args...>>)
348 promise->set_value({std::move(args)...});
350 promise->set_value();
352 promise->set_exception(std::make_exception_ptr(*std::move(error)));
361 template <
typename... Args>
362 Awaitable<awaitable_return_t<Args...>> AsyncMethodInvoker::getResultAsAwaitable()
367 auto data = std::make_shared<AwaitableData<awaitable_return_t<Args...>>>();
369 uponReplyInvoke([data](std::optional<Error> error, Args... args)
372 if constexpr (!std::is_void_v<awaitable_return_t<Args...>>)
373 data->result = {std::move(args)...};
375 data->result = std::monostate{};
377 data->result = std::make_exception_ptr(*std::move(error));
379 auto previous = data->status.exchange(AwaitableState::Completed, std::memory_order_acq_rel);
380 if (previous == AwaitableState::Waiting)
381 data->resumeCoroutine();
384 return Awaitable(data);
391 inline SignalSubscriber::SignalSubscriber(IProxy& proxy,
const SignalName& signalName)
392 : SignalSubscriber(proxy, signalName.c_str())
396 inline SignalSubscriber::SignalSubscriber(IProxy& proxy,
const char* signalName)
398 , signalName_(signalName)
402 inline SignalSubscriber& SignalSubscriber::onInterface(
const InterfaceName& interfaceName)
404 return onInterface(interfaceName.c_str());
407 inline SignalSubscriber& SignalSubscriber::onInterface(
const std::string& interfaceName)
409 return onInterface(interfaceName.c_str());
412 inline SignalSubscriber& SignalSubscriber::onInterface(
const char* interfaceName)
414 interfaceName_ = std::move(interfaceName);
419 template <
typename Function>
420 inline void SignalSubscriber::call(Function&& callback)
422 assert(interfaceName_ !=
nullptr);
424 proxy_.registerSignalHandler( interfaceName_
426 , makeSignalHandler(std::forward<Function>(callback)) );
429 template <
typename Function>
430 [[nodiscard]]
inline Slot SignalSubscriber::call(Function&& callback, return_slot_t)
432 assert(interfaceName_ !=
nullptr);
434 return proxy_.registerSignalHandler( interfaceName_
436 , makeSignalHandler(std::forward<Function>(callback))
440 template <
typename Function>
441 inline signal_handler SignalSubscriber::makeSignalHandler(Function&& callback)
443 return [callback = std::forward<Function>(callback)](Signal signal)
447 tuple_of_function_input_arg_types_t<Function> signalArgs;
453 if constexpr (has_error_param_v<Function>)
458 signal >> signalArgs;
460 catch (
const Error& err)
464 sdbus::apply(callback, err, signalArgs);
469 sdbus::apply(callback, std::nullopt, signalArgs);
474 signal >> signalArgs;
477 sdbus::apply(callback, signalArgs);
486 inline PropertyChangeSubscriber::PropertyChangeSubscriber(IProxy& proxy, PropertyName propertyName)
488 , propertyName_(std::move(propertyName))
492 inline PropertyChangeSubscriber& PropertyChangeSubscriber::onInterface(InterfaceName interfaceName)
494 interfaceName_ = std::move(interfaceName);
499 inline PropertyChangeSubscriber& PropertyChangeSubscriber::onInterface(std::string_view interfaceName)
501 return onInterface(InterfaceName{std::string{interfaceName}});
504 template <
typename Function>
505 inline void PropertyChangeSubscriber::call(Function&& callback)
507 assert(!interfaceName_.empty());
509 proxy_.registerSignalHandler( DBUS_PROPERTIES_INTERFACE_NAME
510 , PROPERTIES_CHANGED_SIGNAL_NAME
511 , makeSignalHandler(std::forward<Function>(callback)) );
514 template <
typename Function>
515 [[nodiscard]]
inline Slot PropertyChangeSubscriber::call(Function&& callback, return_slot_t)
517 assert(!interfaceName_.empty());
519 return proxy_.registerSignalHandler( DBUS_PROPERTIES_INTERFACE_NAME
520 , PROPERTIES_CHANGED_SIGNAL_NAME
521 , makeSignalHandler(std::forward<Function>(callback))
525 template <
typename Function>
526 inline signal_handler PropertyChangeSubscriber::makeSignalHandler(Function&& callback)
528 using callback_args_t = tuple_of_function_input_arg_types_t<Function>;
529 static_assert( std::tuple_size_v<callback_args_t> == 1
530 ,
"Property change callback must take exactly one property value parameter (plus optional error parameter)" );
531 using property_arg_t = std::tuple_element_t<0, callback_args_t>;
532 static_assert( is_optional_v<property_arg_t>
533 ,
"Property value parameter of property change callback must be std::optional<T>" );
534 using property_value_t =
typename property_arg_t::value_type;
536 return [ callback = std::forward<Function>(callback)
537 , interfaceName = std::move(interfaceName_)
538 , propertyName = std::move(propertyName_) ](Signal signal)
540 std::optional<property_value_t> value;
546 if constexpr (has_error_param_v<Function>)
550 affected = readPropertyChange(signal, interfaceName, propertyName, value);
552 catch (
const Error& err)
554 callback(err, std::optional<property_value_t>{});
559 callback(std::nullopt, std::move(value));
563 affected = readPropertyChange(signal, interfaceName, propertyName, value);
566 callback(std::move(value));
571 template <
typename Value>
572 inline bool PropertyChangeSubscriber::readPropertyChange( Signal& signal
573 ,
const InterfaceName& interfaceName
574 ,
const PropertyName& propertyName
575 , std::optional<Value>& value )
577 std::string changedInterfaceName;
578 std::map<std::string, Variant> changedProperties;
579 std::vector<std::string> invalidatedProperties;
580 signal >> changedInterfaceName >> changedProperties >> invalidatedProperties;
582 if (changedInterfaceName != interfaceName)
585 if (
auto it = changedProperties.find(propertyName); it != changedProperties.end())
587 value = it->second.get<Value>();
592 return std::find(invalidatedProperties.begin(), invalidatedProperties.end(), propertyName) != invalidatedProperties.end();
599 inline PropertyGetter::PropertyGetter(IProxy& proxy, std::string_view propertyName)
601 , propertyName_(std::move(propertyName))
605 inline Variant PropertyGetter::onInterface(std::string_view interfaceName)
608 proxy_.callMethod(
"Get")
609 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
610 .withArguments(interfaceName, propertyName_)
611 .storeResultsTo(var);
619 inline AsyncPropertyGetter::AsyncPropertyGetter(IProxy& proxy, std::string_view propertyName)
621 , propertyName_(std::move(propertyName))
625 inline AsyncPropertyGetter& AsyncPropertyGetter::onInterface(std::string_view interfaceName)
627 interfaceName_ = std::move(interfaceName);
632 template <
typename Function>
633 auto AsyncPropertyGetter::makeReplyHandler(Function&& callback)
635 static_assert( has_error_param_v<Function>
636 ,
"Property get callback function must accept std::optional<Error> as its first parameter" );
639 static_assert( function_argument_count_v<Function> == 1
640 ,
"Property get callback function must accept std::optional<Error> and property value" );
641 using value_t = std::decay_t<function_argument_t<Function, 0>>;
643 if constexpr (std::is_same_v<value_t, Variant>)
644 return std::forward<Function>(callback);
646 return [callback = std::forward<Function>(callback)](std::optional<Error> error, Variant value)
650 callback(std::move(error), value_t{});
654 value_t typedValue{};
657 typedValue = value.get<value_t>();
659 catch (
const Error& err)
663 callback(err, value_t{});
667 callback(std::nullopt, std::move(typedValue));
671 template <
typename Function>
672 PendingAsyncCall AsyncPropertyGetter::uponReplyInvoke(Function&& callback)
674 assert(!interfaceName_.empty());
676 return proxy_.callMethodAsync(
"Get")
677 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
678 .withArguments(interfaceName_, propertyName_)
679 .uponReplyInvoke(makeReplyHandler(std::forward<Function>(callback)));
682 template <
typename Function>
683 [[nodiscard]] Slot AsyncPropertyGetter::uponReplyInvoke(Function&& callback, return_slot_t)
685 assert(!interfaceName_.empty());
687 return proxy_.callMethodAsync(
"Get")
688 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
689 .withArguments(interfaceName_, propertyName_)
690 .uponReplyInvoke(makeReplyHandler(std::forward<Function>(callback)), return_slot);
693 inline std::future<Variant> AsyncPropertyGetter::getResultAsFuture()
695 assert(!interfaceName_.empty());
697 return proxy_.callMethodAsync(
"Get")
698 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
699 .withArguments(interfaceName_, propertyName_)
700 .getResultAsFuture<Variant>();
703 inline Awaitable<Variant> AsyncPropertyGetter::getResultAsAwaitable()
705 assert(!interfaceName_.empty());
707 return proxy_.callMethodAsync(
"Get")
708 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
709 .withArguments(interfaceName_, propertyName_)
710 .getResultAsAwaitable<Variant>();
717 inline PropertySetter::PropertySetter(IProxy& proxy, std::string_view propertyName)
719 , propertyName_(std::move(propertyName))
723 inline PropertySetter& PropertySetter::onInterface(std::string_view interfaceName)
725 interfaceName_ = std::move(interfaceName);
730 inline PropertySetter& PropertySetter::withTimeout(uint64_t usec)
737 template <
typename Rep,
typename Period>
738 inline PropertySetter& PropertySetter::withTimeout(
const std::chrono::duration<Rep, Period>& timeout)
740 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
741 return withTimeout(microsecs.count());
744 template <
typename Value>
745 inline void PropertySetter::toValue(
const Value& value)
747 PropertySetter::toValue(Variant{value});
750 template <
typename Value>
751 inline void PropertySetter::toValue(
const Value& value, dont_expect_reply_t)
753 PropertySetter::toValue(Variant{value}, dont_expect_reply);
756 inline void PropertySetter::toValue(
const Variant& value)
758 assert(!interfaceName_.empty());
760 proxy_.callMethod(
"Set")
761 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
762 .withTimeout(timeout_)
763 .withArguments(interfaceName_, propertyName_, value);
766 inline void PropertySetter::toValue(
const Variant& value, dont_expect_reply_t)
768 assert(!interfaceName_.empty());
770 proxy_.callMethod(
"Set")
771 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
772 .withTimeout(timeout_)
773 .withArguments(interfaceName_, propertyName_, value)
781 inline AsyncPropertySetter::AsyncPropertySetter(IProxy& proxy, std::string_view propertyName)
783 , propertyName_(propertyName)
787 inline AsyncPropertySetter& AsyncPropertySetter::onInterface(std::string_view interfaceName)
789 interfaceName_ = std::move(interfaceName);
794 inline AsyncPropertySetter& AsyncPropertySetter::withTimeout(uint64_t usec)
801 template <
typename Rep,
typename Period>
802 inline AsyncPropertySetter& AsyncPropertySetter::withTimeout(
const std::chrono::duration<Rep, Period>& timeout)
804 auto microsecs = std::chrono::duration_cast<std::chrono::microseconds>(timeout);
805 return withTimeout(microsecs.count());
808 template <
typename Value>
809 inline AsyncPropertySetter& AsyncPropertySetter::toValue(Value&& value)
811 return AsyncPropertySetter::toValue(Variant{std::forward<Value>(value)});
814 inline AsyncPropertySetter& AsyncPropertySetter::toValue(Variant value)
816 value_ = std::move(value);
821 template <
typename Function>
822 PendingAsyncCall AsyncPropertySetter::uponReplyInvoke(Function&& callback)
824 static_assert( std::is_invocable_r_v<void, Function, std::optional<Error>>
825 ,
"Property set callback function must accept std::optional<Error> only" );
827 assert(!interfaceName_.empty());
829 return proxy_.callMethodAsync(
"Set")
830 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
831 .withTimeout(timeout_)
832 .withArguments(interfaceName_, propertyName_, std::move(value_))
833 .uponReplyInvoke(std::forward<Function>(callback));
836 template <
typename Function>
837 [[nodiscard]] Slot AsyncPropertySetter::uponReplyInvoke(Function&& callback, return_slot_t)
839 static_assert( std::is_invocable_r_v<void, Function, std::optional<Error>>
840 ,
"Property set callback function must accept std::optional<Error> only" );
842 assert(!interfaceName_.empty());
844 return proxy_.callMethodAsync(
"Set")
845 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
846 .withTimeout(timeout_)
847 .withArguments(interfaceName_, propertyName_, std::move(value_))
848 .uponReplyInvoke(std::forward<Function>(callback), return_slot);
851 inline std::future<void> AsyncPropertySetter::getResultAsFuture()
853 assert(!interfaceName_.empty());
855 return proxy_.callMethodAsync(
"Set")
856 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
857 .withTimeout(timeout_)
858 .withArguments(interfaceName_, propertyName_, std::move(value_))
859 .getResultAsFuture<>();
862 inline Awaitable<void> AsyncPropertySetter::getResultAsAwaitable()
864 assert(!interfaceName_.empty());
866 return proxy_.callMethodAsync(
"Set")
867 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
868 .withTimeout(timeout_)
869 .withArguments(interfaceName_, propertyName_, std::move(value_))
870 .getResultAsAwaitable<>();
877 inline AllPropertiesGetter::AllPropertiesGetter(IProxy& proxy)
882 inline std::map<PropertyName, Variant> AllPropertiesGetter::onInterface(std::string_view interfaceName)
884 std::map<PropertyName, Variant> props;
885 proxy_.callMethod(
"GetAll")
886 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
887 .withArguments(std::move(interfaceName))
888 .storeResultsTo(props);
896 inline AsyncAllPropertiesGetter::AsyncAllPropertiesGetter(IProxy& proxy)
901 inline AsyncAllPropertiesGetter& AsyncAllPropertiesGetter::onInterface(std::string_view interfaceName)
903 interfaceName_ = std::move(interfaceName);
908 template <
typename Function>
909 PendingAsyncCall AsyncAllPropertiesGetter::uponReplyInvoke(Function&& callback)
911 static_assert( std::is_invocable_r_v<void, Function, std::optional<Error>, std::map<PropertyName, Variant>>
912 ,
"All properties get callback function must accept std::optional<Error> and a map of property names to their values" );
914 assert(!interfaceName_.empty());
916 return proxy_.callMethodAsync(
"GetAll")
917 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
918 .withArguments(interfaceName_)
919 .uponReplyInvoke(std::forward<Function>(callback));
922 template <
typename Function>
923 [[nodiscard]] Slot AsyncAllPropertiesGetter::uponReplyInvoke(Function&& callback, return_slot_t)
925 static_assert( std::is_invocable_r_v<void, Function, std::optional<Error>, std::map<PropertyName, Variant>>
926 ,
"All properties get callback function must accept std::optional<Error> and a map of property names to their values" );
928 assert(!interfaceName_.empty());
930 return proxy_.callMethodAsync(
"GetAll")
931 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
932 .withArguments(interfaceName_)
933 .uponReplyInvoke(std::forward<Function>(callback), return_slot);
936 inline std::future<std::map<PropertyName, Variant>> AsyncAllPropertiesGetter::getResultAsFuture()
938 assert(!interfaceName_.empty());
940 return proxy_.callMethodAsync(
"GetAll")
941 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
942 .withArguments(interfaceName_)
943 .getResultAsFuture<std::map<PropertyName, Variant>>();
946 inline Awaitable<std::map<PropertyName, Variant>> AsyncAllPropertiesGetter::getResultAsAwaitable()
948 assert(!interfaceName_.empty());
950 return proxy_.callMethodAsync(
"GetAll")
951 .onInterface(DBUS_PROPERTIES_INTERFACE_NAME)
952 .withArguments(interfaceName_)
953 .getResultAsAwaitable<std::map<PropertyName, Variant>>();