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>;
67 class PropertySetCall;
68 class PropertyGetReply;
69 template <
typename... _Results>
class Result;
71 template <
typename _T,
typename _Enable =
void>
struct signature_of;
77 using method_callback = std::function<void(MethodCall msg)>;
78 using async_reply_handler = std::function<void(MethodReply reply, std::optional<Error> error)>;
79 using signal_handler = std::function<void(Signal signal)>;
80 using message_handler = std::function<void(Message msg)>;
81 using property_set_callback = std::function<void(PropertySetCall msg)>;
82 using property_get_callback = std::function<void(PropertyGetReply& reply)>;
85 using Slot = std::unique_ptr<void, std::function<void(
void*)>>;
111 template <
class... _T>
constexpr bool always_false =
false;
114 template <
typename _T, std::
size_t _N1, std::
size_t _N2>
115 constexpr std::array<_T, _N1 + _N2> operator+(std::array<_T, _N1> lhs, std::array<_T, _N2> rhs);
118 template <
typename _T>
119 constexpr auto signature_of_v = signature_of<_T>::value;
121 template <
typename _T,
typename _Enable>
124 static constexpr bool is_valid =
false;
125 static constexpr bool is_trivial_dbus_type =
false;
127 static constexpr void* value = []
131 static_assert(always_false<_T>,
"Unsupported D-Bus type (specialize `signature_of` for your custom types)");
135 template <
typename _T>
139 template <
typename _T>
143 template <
typename _T>
150 static constexpr std::array<char, 0> value{};
151 static constexpr bool is_valid =
true;
152 static constexpr bool is_trivial_dbus_type =
false;
158 static constexpr std::array value{
'b'};
159 static constexpr bool is_valid =
true;
160 static constexpr bool is_trivial_dbus_type =
true;
166 static constexpr std::array value{
'y'};
167 static constexpr bool is_valid =
true;
168 static constexpr bool is_trivial_dbus_type =
true;
174 static constexpr std::array value{
'n'};
175 static constexpr bool is_valid =
true;
176 static constexpr bool is_trivial_dbus_type =
true;
182 static constexpr std::array value{
'q'};
183 static constexpr bool is_valid =
true;
184 static constexpr bool is_trivial_dbus_type =
true;
190 static constexpr std::array value{
'i'};
191 static constexpr bool is_valid =
true;
192 static constexpr bool is_trivial_dbus_type =
true;
198 static constexpr std::array value{
'u'};
199 static constexpr bool is_valid =
true;
200 static constexpr bool is_trivial_dbus_type =
true;
206 static constexpr std::array value{
'x'};
207 static constexpr bool is_valid =
true;
208 static constexpr bool is_trivial_dbus_type =
true;
214 static constexpr std::array value{
't'};
215 static constexpr bool is_valid =
true;
216 static constexpr bool is_trivial_dbus_type =
true;
222 static constexpr std::array value{
'd'};
223 static constexpr bool is_valid =
true;
224 static constexpr bool is_trivial_dbus_type =
true;
230 static constexpr std::array value{
's'};
231 static constexpr bool is_valid =
true;
232 static constexpr bool is_trivial_dbus_type =
false;
247 template <std::
size_t _N>
251 template <std::
size_t _N>
267 template <
typename... _ValueTypes>
270 static constexpr std::array contents = (signature_of_v<_ValueTypes> + ...);
271 static constexpr std::array value = std::array{
'('} + contents + std::array{
')'};
272 static constexpr bool is_valid =
true;
273 static constexpr bool is_trivial_dbus_type =
false;
279 static constexpr std::array value{
'v'};
280 static constexpr bool is_valid =
true;
281 static constexpr bool is_trivial_dbus_type =
false;
284 template <
typename... Elements>
291 static constexpr std::array value{
'o'};
292 static constexpr bool is_valid =
true;
293 static constexpr bool is_trivial_dbus_type =
false;
299 static constexpr std::array value{
'g'};
300 static constexpr bool is_valid =
true;
301 static constexpr bool is_trivial_dbus_type =
false;
307 static constexpr std::array value{
'h'};
308 static constexpr bool is_valid =
true;
309 static constexpr bool is_trivial_dbus_type =
false;
312 template <
typename _T1,
typename _T2>
315 static constexpr std::array value = std::array{
'{'} + signature_of_v<std::tuple<_T1, _T2>> + std::array{
'}'};
316 static constexpr bool is_valid =
true;
317 static constexpr bool is_trivial_dbus_type =
false;
320 template <
typename _Element,
typename _Allocator>
323 static constexpr std::array value = std::array{
'a'} + signature_of_v<_Element>;
324 static constexpr bool is_valid =
true;
325 static constexpr bool is_trivial_dbus_type =
false;
328 template <
typename _Element, std::
size_t _Size>
334 template <
typename _Element, std::
size_t _Extent>
340 template <
typename _Enum>
341 struct signature_of<_Enum, typename std::enable_if_t<std::is_enum_v<_Enum>>>
345 template <
typename _Key,
typename _Value,
typename _Compare,
typename _Allocator>
348 static constexpr std::array contents = signature_of_v<std::tuple<_Key, _Value>>;
349 static constexpr std::array dict_entry = std::array{
'{'} + contents + std::array{
'}'};
350 static constexpr std::array value = std::array{
'a'} + dict_entry;
351 static constexpr bool is_valid =
true;
352 static constexpr bool is_trivial_dbus_type =
false;
355 template <
typename _Key,
typename _Value,
typename _Hash,
typename _KeyEqual,
typename _Allocator>
356 struct signature_of<std::unordered_map<_Key, _Value, _Hash, _KeyEqual, _Allocator>>
361 template <
typename... _Types>
364 static constexpr std::array value = (std::array<char, 0>{} + ... + signature_of_v<_Types>);
365 static constexpr bool is_valid =
false;
366 static constexpr bool is_trivial_dbus_type =
false;
370 template <
typename _T, std::
size_t _N>
371 constexpr auto as_null_terminated(std::array<_T, _N> arr)
373 return arr + std::array<_T, 1>{0};
378 template <
typename _Type>
382 template <
typename _Type>
386 template <
typename _Type>
390 template <
typename _ReturnType,
typename... _Args>
393 typedef _ReturnType result_type;
394 typedef std::tuple<_Args...> arguments_type;
395 typedef std::tuple<std::decay_t<_Args>...> decayed_arguments_type;
397 typedef _ReturnType function_type(_Args...);
399 static constexpr std::size_t arity =
sizeof...(_Args);
416 template <
size_t _Idx>
419 typedef std::tuple_element_t<_Idx, std::tuple<_Args...>> type;
422 template <
size_t _Idx>
423 using arg_t =
typename arg<_Idx>::type;
426 template <
typename _ReturnType,
typename... _Args>
429 static constexpr bool is_async =
false;
430 static constexpr bool has_error_param =
false;
433 template <
typename... _Args>
436 static constexpr bool has_error_param =
true;
439 template <
typename... _Args,
typename... _Results>
442 static constexpr bool is_async =
true;
446 template <
typename... _Args,
typename... _Results>
449 static constexpr bool is_async =
true;
453 template <
typename _ReturnType,
typename... _Args>
457 template <
typename _ClassType,
typename _ReturnType,
typename... _Args>
460 typedef _ClassType& owner_type;
463 template <
typename _ClassType,
typename _ReturnType,
typename... _Args>
466 typedef const _ClassType& owner_type;
469 template <
typename _ClassType,
typename _ReturnType,
typename... _Args>
472 typedef volatile _ClassType& owner_type;
475 template <
typename _ClassType,
typename _ReturnType,
typename... _Args>
478 typedef const volatile _ClassType& owner_type;
481 template <
typename FunctionType>
485 template <
class _Function>
488 template <
class _Function>
491 template <
typename _FunctionType>
494 template <
typename _FunctionType,
size_t _Idx>
497 template <
typename _FunctionType>
500 template <
typename _FunctionType>
503 template <
typename _Function>
509 template <
typename _Function>
510 using tuple_of_function_input_arg_types_t =
typename tuple_of_function_input_arg_types<_Function>::type;
512 template <
typename _Function>
518 template <
typename _Function>
519 using tuple_of_function_output_arg_types_t =
typename tuple_of_function_output_arg_types<_Function>::type;
521 template <
typename _Function>
524 static std::string value_as_string()
526 constexpr auto signature = as_null_terminated(signature_of_v<tuple_of_function_input_arg_types_t<_Function>>);
527 return signature.data();
531 template <
typename _Function>
534 template <
typename _Function>
537 static std::string value_as_string()
539 constexpr auto signature = as_null_terminated(signature_of_v<tuple_of_function_output_arg_types_t<_Function>>);
540 return signature.data();
544 template <
typename _Function>
550 typedef std::tuple<_Args...> type;
563 template <
typename... _Args>
564 using future_return_t =
typename future_return<_Args...>::type;
567 template <
typename,
typename>
568 constexpr bool is_one_of_variants_types =
false;
570 template <
typename... _VariantTypes,
typename _QueriedType>
571 constexpr bool is_one_of_variants_types<std::variant<_VariantTypes...>, _QueriedType>
572 = (std::is_same_v<_QueriedType, _VariantTypes> || ...);
576 template <
class _Function,
class _Tuple,
typename... _Args, std::size_t... _I>
577 constexpr decltype(
auto) apply_impl( _Function&& f
580 , std::index_sequence<_I...> )
582 return std::forward<_Function>(f)(std::move(r), std::get<_I>(std::forward<_Tuple>(t))...);
585 template <
class _Function,
class _Tuple, std::size_t... _I>
586 decltype(
auto) apply_impl( _Function&& f
587 , std::optional<Error> e
589 , std::index_sequence<_I...> )
591 return std::forward<_Function>(f)(std::move(e), std::get<_I>(std::forward<_Tuple>(t))...);
596 template <
class _Function,
class _Tuple, std::size_t... _I>
597 constexpr decltype(
auto) apply_impl( _Function&& f
599 , std::index_sequence<_I...> )
601 if constexpr (!std::is_void_v<function_result_t<_Function>>)
602 return std::forward<_Function>(f)(std::get<_I>(std::forward<_Tuple>(t))...);
604 return std::forward<_Function>(f)(std::get<_I>(std::forward<_Tuple>(t))...), std::tuple<>{};
610 template <
class _Function,
class _Tuple>
611 constexpr decltype(
auto) apply(_Function&& f, _Tuple&& t)
613 return detail::apply_impl( std::forward<_Function>(f)
614 , std::forward<_Tuple>(t)
615 , std::make_index_sequence<std::tuple_size<std::decay_t<_Tuple>>::value>{} );
620 template <
class _Function,
class _Tuple,
typename... _Args>
621 constexpr decltype(
auto) apply(_Function&& f, Result<_Args...>&& r, _Tuple&& t)
623 return detail::apply_impl( std::forward<_Function>(f)
625 , std::forward<_Tuple>(t)
626 , std::make_index_sequence<std::tuple_size<std::decay_t<_Tuple>>::value>{} );
631 template <
class _Function,
class _Tuple>
632 decltype(
auto) apply(_Function&& f, std::optional<Error> e, _Tuple&& t)
634 return detail::apply_impl( std::forward<_Function>(f)
636 , std::forward<_Tuple>(t)
637 , std::make_index_sequence<std::tuple_size<std::decay_t<_Tuple>>::value>{} );
641 template <
typename _T, std::
size_t _N1, std::
size_t _N2>
642 constexpr std::array<_T, _N1 + _N2> operator+(std::array<_T, _N1> lhs, std::array<_T, _N2> rhs)
644 std::array<_T, _N1 + _N2> result{};
645 std::size_t index = 0;
647 for (
auto& el : lhs) {
648 result[index] = std::move(el);
651 for (
auto& el : rhs) {
652 result[index] = std::move(el);
std::pair< _T1, _T2 > DictEntry
Definition Types.h:384
Definition MethodResult.h:51
Definition TypeTraits.h:97
Definition TypeTraits.h:94
Definition TypeTraits.h:107
Definition TypeTraits.h:101
Definition TypeTraits.h:91
Definition TypeTraits.h:418
Definition TypeTraits.h:392
Definition TypeTraits.h:380
Definition TypeTraits.h:549
Definition TypeTraits.h:88
Definition TypeTraits.h:536
Definition TypeTraits.h:123
Definition TypeTraits.h:514
Definition TypeTraits.h:104