27#ifndef SDBUS_CXX_TYPETRAITS_H_
28#define SDBUS_CXX_TYPETRAITS_H_
39# if __has_include(<span>)
47#include <unordered_map>
55 template <
typename... ValueTypes>
class Struct;
59 template<
typename T1,
typename T2>
using DictEntry = std::pair<T1, T2>;
70 template <
typename... Results>
class Result;
72 template <
typename T,
typename Enable =
void>
struct signature_of;
78 using method_callback = std::function<void(
MethodCall msg)>;
79 using async_reply_handler = std::function<void(
MethodReply reply, std::optional<Error> error)>;
80 using signal_handler = std::function<void(
Signal signal)>;
81 using message_handler = std::function<void(
Message msg)>;
84 using track_handler = std::function<void(
Track track)>;
87 using Slot = std::unique_ptr<void, std::function<void(
void*)>>;
90 struct return_slot_t {
explicit return_slot_t() =
default; };
93 struct floating_slot_t {
explicit floating_slot_t() =
default; };
96 struct adopt_message_t {
explicit adopt_message_t() =
default; };
99 struct adopt_fd_t {
explicit adopt_fd_t() =
default; };
102 struct adopt_track_t {
explicit adopt_track_t() =
default; };
106 struct dont_run_event_loop_thread_t {
explicit dont_run_event_loop_thread_t() =
default; };
109 struct with_future_t {
explicit with_future_t() =
default; };
112 struct dont_expect_reply_t {
explicit dont_expect_reply_t() =
default; };
115 struct embed_variant_t {
explicit embed_variant_t() =
default; };
118 struct with_awaitable_t {
explicit with_awaitable_t() =
default; };
122 template <
class... T>
constexpr bool always_false =
false;
125 template <
typename T, std::
size_t N1, std::
size_t N2>
126 constexpr std::array<T, N1 + N2> operator+(std::array<T, N1> lhs, std::array<T, N2> rhs);
129 template <
typename T>
130 constexpr auto signature_of_v = signature_of<T>::value;
132 template <
typename T,
typename Enable>
135 static constexpr bool is_valid =
false;
136 static constexpr bool is_trivial_dbus_type =
false;
138 static constexpr void* value = []
142 static_assert(always_false<T>,
"Unsupported D-Bus type (specialize `signature_of` for your custom types)");
146 template <
typename T>
150 template <
typename T>
154 template <
typename T>
158 template <
typename T>
162 template <
typename T>
169 static constexpr std::array<char, 0> value{};
170 static constexpr bool is_valid =
true;
171 static constexpr bool is_trivial_dbus_type =
false;
177 static constexpr std::array value{
'b'};
178 static constexpr bool is_valid =
true;
179 static constexpr bool is_trivial_dbus_type =
true;
185 static constexpr std::array value{
'y'};
186 static constexpr bool is_valid =
true;
187 static constexpr bool is_trivial_dbus_type =
true;
193 static constexpr std::array value{
'n'};
194 static constexpr bool is_valid =
true;
195 static constexpr bool is_trivial_dbus_type =
true;
201 static constexpr std::array value{
'q'};
202 static constexpr bool is_valid =
true;
203 static constexpr bool is_trivial_dbus_type =
true;
209 static constexpr std::array value{
'i'};
210 static constexpr bool is_valid =
true;
211 static constexpr bool is_trivial_dbus_type =
true;
217 static constexpr std::array value{
'u'};
218 static constexpr bool is_valid =
true;
219 static constexpr bool is_trivial_dbus_type =
true;
225 static constexpr std::array value{
'x'};
226 static constexpr bool is_valid =
true;
227 static constexpr bool is_trivial_dbus_type =
true;
233 static constexpr std::array value{
't'};
234 static constexpr bool is_valid =
true;
235 static constexpr bool is_trivial_dbus_type =
true;
241 static constexpr std::array value{
'd'};
242 static constexpr bool is_valid =
true;
243 static constexpr bool is_trivial_dbus_type =
true;
249 static constexpr std::array value{
's'};
250 static constexpr bool is_valid =
true;
251 static constexpr bool is_trivial_dbus_type =
false;
266 template <std::
size_t N>
270 template <std::
size_t N>
286 template <
typename... ValueTypes>
289 static constexpr std::array contents = (signature_of_v<ValueTypes> + ...);
290 static constexpr std::array value = std::array{
'('} + contents + std::array{
')'};
291 static constexpr char type_value{
'r'};
292 static constexpr bool is_valid =
true;
293 static constexpr bool is_trivial_dbus_type =
false;
299 static constexpr std::array value{
'v'};
300 static constexpr bool is_valid =
true;
301 static constexpr bool is_trivial_dbus_type =
false;
304 template <
typename... Elements>
311 static constexpr std::array value{
'o'};
312 static constexpr bool is_valid =
true;
313 static constexpr bool is_trivial_dbus_type =
false;
319 static constexpr std::array value{
'g'};
320 static constexpr bool is_valid =
true;
321 static constexpr bool is_trivial_dbus_type =
false;
327 static constexpr std::array value{
'h'};
328 static constexpr bool is_valid =
true;
329 static constexpr bool is_trivial_dbus_type =
false;
332 template <
typename T1,
typename T2>
335 static constexpr std::array value = std::array{
'{'} + signature_of_v<std::tuple<T1, T2>> + std::array{
'}'};
336 static constexpr char type_value{
'e'};
337 static constexpr bool is_valid =
true;
338 static constexpr bool is_trivial_dbus_type =
false;
341 template <
typename Element,
typename Allocator>
344 static constexpr std::array value = std::array{
'a'} + signature_of_v<Element>;
345 static constexpr bool is_valid =
true;
346 static constexpr bool is_trivial_dbus_type =
false;
349 template <
typename Element, std::
size_t Size>
355 template <
typename Element, std::
size_t Extent>
361 template <
typename Enum>
362 struct signature_of<Enum, std::enable_if_t<std::is_enum_v<Enum> && !std::is_const_v<Enum> && !std::is_volatile_v<Enum>>>
366 template <
typename Key,
typename Value,
typename Compare,
typename Allocator>
369 static constexpr std::array value = std::array{
'a'} + signature_of_v<DictEntry<Key, Value>>;
370 static constexpr bool is_valid =
true;
371 static constexpr bool is_trivial_dbus_type =
false;
374 template <
typename Key,
typename Value,
typename Hash,
typename KeyEqual,
typename Allocator>
375 struct signature_of<std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>>
380 template <
typename... Types>
383 static constexpr std::array value = (std::array<char, 0>{} + ... + signature_of_v<Types>);
384 static constexpr bool is_valid =
false;
385 static constexpr bool is_trivial_dbus_type =
false;
389 template <
typename T, std::
size_t N>
390 constexpr auto as_null_terminated(std::array<T, N> arr)
392 return arr + std::array<T, 1>{0};
397 template <
typename Type>
401 template <
typename Type>
405 template <
typename Type>
409 template <
typename ReturnType,
typename... Args>
412 using result_type = ReturnType;
413 using arguments_type = std::tuple<Args...>;
414 using decayed_arguments_type = std::tuple<std::decay_t<Args>...>;
416 using function_type = ReturnType (Args...);
418 static constexpr std::size_t arity =
sizeof...(Args);
435 template <
size_t Idx>
438 using type = std::tuple_element_t<Idx, std::tuple<Args...>>;
441 template <
size_t Idx>
442 using arg_t =
typename arg<Idx>::type;
445 template <
typename ReturnType,
typename... Args>
448 static constexpr bool is_async =
false;
449 static constexpr bool has_error_param =
false;
452 template <
typename... Args>
455 static constexpr bool has_error_param =
true;
458 template <
typename... Args>
461 static constexpr bool has_error_param =
true;
464 template <
typename... Args>
467 static constexpr bool has_error_param =
true;
470 template <
typename... Args,
typename... Results>
473 static constexpr bool is_async =
true;
474 using async_result_t =
Result<Results...>;
477 template <
typename... Args,
typename... Results>
480 static constexpr bool is_async =
true;
481 using async_result_t =
Result<Results...>;
484 template <
typename ReturnType,
typename... Args>
488 template <
typename ClassType,
typename ReturnType,
typename... Args>
491 using owner_type = ClassType &;
494 template <
typename ClassType,
typename ReturnType,
typename... Args>
497 using owner_type =
const ClassType &;
500 template <
typename ClassType,
typename ReturnType,
typename... Args>
503 using owner_type =
volatile ClassType &;
506 template <
typename ClassType,
typename ReturnType,
typename... Args>
509 using owner_type =
const volatile ClassType &;
512 template <
typename FunctionType>
516 template <
class Function>
519 template <
class Function>
522 template <
typename T>
526 template <
typename T>
530 template <
typename T>
533 template <
typename FunctionType>
536 template <
typename FunctionType,
size_t Idx>
539 template <
typename FunctionType>
542 template <
typename FunctionType>
545 template <
typename Function>
551 template <
typename Function>
552 using tuple_of_function_input_arg_types_t =
typename tuple_of_function_input_arg_types<Function>::type;
554 template <
typename Function>
560 template <
typename Function>
561 using tuple_of_function_output_arg_types_t =
typename tuple_of_function_output_arg_types<Function>::type;
563 template <
typename Function>
566 static std::string value_as_string()
568 constexpr auto signature = as_null_terminated(signature_of_v<tuple_of_function_input_arg_types_t<Function>>);
569 return signature.data();
573 template <
typename Function>
574 inline const auto signature_of_function_input_arguments_v = signature_of_function_input_arguments<Function>::value_as_string();
576 template <
typename Function>
579 static std::string value_as_string()
581 constexpr auto signature = as_null_terminated(signature_of_v<tuple_of_function_output_arg_types_t<Function>>);
582 return signature.data();
586 template <
typename Function>
587 inline const auto signature_of_function_output_arguments_v = signature_of_function_output_arguments<Function>::value_as_string();
592 using type = std::tuple<Args...>;
605 template <
typename... Args>
606 using future_return_t =
typename future_return<Args...>::type;
610 template <
typename... Args>
611 using awaitable_return_t =
typename future_return<Args...>::type;
614 template <
typename,
typename>
615 constexpr bool is_one_of_variants_types =
false;
617 template <
typename... VariantTypes,
typename QueriedType>
618 constexpr bool is_one_of_variants_types<std::variant<VariantTypes...>, QueriedType>
619 = (std::is_same_v<QueriedType, VariantTypes> || ...);
623 template <
typename Struct>
626 explicit as_dictionary(
const Struct& strct) : m_struct(strct) {}
630 template <
typename Type>
631 const Type& as_dictionary_if_struct(
const Type&
object)
640 template <
typename Struct>
641 constexpr auto strict_dict_as_struct_deserialization_v =
true;
648 template <
typename Struct>
649 constexpr auto nested_struct_as_dict_serialization_v =
false;
653 template <
class Function,
class Tuple,
typename... Args, std::size_t... I>
654 constexpr decltype(
auto) apply_impl( Function&& fun
655 , Result<Args...>&& res
657 , std::index_sequence<I...> )
659 return std::forward<Function>(fun)(std::move(res), std::get<I>(std::forward<Tuple>(tuple))...);
662 template <
class Function,
class Tuple, std::size_t... I>
663 decltype(
auto) apply_impl( Function&& fun
664 , std::optional<Error> err
666 , std::index_sequence<I...> )
668 return std::forward<Function>(fun)(std::move(err), std::get<I>(std::forward<Tuple>(tuple))...);
673 template <
class Function,
class Tuple, std::size_t... I>
674 constexpr decltype(
auto) apply_impl( Function&& fun
676 , std::index_sequence<I...> )
678 if constexpr (!std::is_void_v<function_result_t<Function>>)
679 return std::forward<Function>(fun)(std::get<I>(std::forward<Tuple>(tuple))...);
681 return std::forward<Function>(fun)(std::get<I>(std::forward<Tuple>(tuple))...), std::tuple<>{};
687 template <
class Function,
class Tuple>
688 constexpr decltype(
auto) apply(Function&& fun, Tuple&& tuple)
690 return detail::apply_impl( std::forward<Function>(fun)
691 , std::forward<Tuple>(tuple)
692 , std::make_index_sequence<std::tuple_size_v<std::decay_t<Tuple>>>{} );
697 template <
class Function,
class Tuple,
typename... Args>
698 constexpr decltype(
auto) apply(Function&& fun,
Result<Args...>&& res, Tuple&& tuple)
700 return detail::apply_impl( std::forward<Function>(fun)
702 , std::forward<Tuple>(tuple)
703 , std::make_index_sequence<std::tuple_size_v<std::decay_t<Tuple>>>{} );
708 template <
class Function,
class Tuple>
709 decltype(
auto) apply(Function&& fun, std::optional<Error> err, Tuple&& tuple)
711 return detail::apply_impl( std::forward<Function>(fun)
713 , std::forward<Tuple>(tuple)
714 , std::make_index_sequence<std::tuple_size_v<std::decay_t<Tuple>>>{} );
721 template <
typename T, std::size_t N1, std::size_t N2, std::size_t... I1, std::size_t... I2>
722 constexpr std::array<T, N1 + N2> concat_arrays_impl( [[maybe_unused]] std::array<T, N1> lhs
723 , [[maybe_unused]] std::array<T, N2> rhs
724 , std::index_sequence<I1...>
725 , std::index_sequence<I2...> )
727 return {std::move(std::get<I1>(lhs))..., std::move(std::get<I2>(rhs))...};
732 template <
typename T, std::
size_t N1, std::
size_t N2>
733 constexpr std::array<T, N1 + N2> operator+(std::array<T, N1> lhs, std::array<T, N2> rhs)
735 return detail::concat_arrays_impl( std::move(lhs)
737 , std::make_index_sequence<N1>{}
738 , std::make_index_sequence<N2>{} );
std::pair< T1, T2 > DictEntry
Definition Types.h:415
Definition MethodResult.h:51
Definition TypeTraits.h:99
Definition TypeTraits.h:96
Definition TypeTraits.h:102
Definition TypeTraits.h:112
Definition TypeTraits.h:106
Definition TypeTraits.h:115
Definition TypeTraits.h:93
Definition TypeTraits.h:437
Definition TypeTraits.h:411
Definition TypeTraits.h:399
Definition TypeTraits.h:591
Definition TypeTraits.h:524
Definition TypeTraits.h:90
Definition TypeTraits.h:578
Definition TypeTraits.h:134
Definition TypeTraits.h:556
Definition TypeTraits.h:118
Definition TypeTraits.h:109