sdbus-c++ 2.0.0
High-level C++ D-Bus library based on systemd D-Bus implementation
Loading...
Searching...
No Matches
TypeTraits.h
Go to the documentation of this file.
1
27#ifndef SDBUS_CXX_TYPETRAITS_H_
28#define SDBUS_CXX_TYPETRAITS_H_
29
30#include <sdbus-c++/Error.h>
31
32#include <array>
33#include <cstdint>
34#include <functional>
35#include <map>
36#include <memory>
37#include <optional>
38#ifdef __has_include
39# if __has_include(<span>)
40# include <span>
41# endif
42#endif
43#include <string>
44#include <string_view>
45#include <tuple>
46#include <type_traits>
47#include <unordered_map>
48#include <utility>
49#include <variant>
50#include <vector>
51
52// Forward declarations
53namespace sdbus {
54 class Variant;
55 template <typename... _ValueTypes> class Struct;
56 class ObjectPath;
57 class Signature;
58 class UnixFd;
59 template<typename _T1, typename _T2> using DictEntry = std::pair<_T1, _T2>;
60 class BusName;
61 class InterfaceName;
62 class MemberName;
63 class MethodCall;
64 class MethodReply;
65 class Signal;
66 class Message;
67 class PropertySetCall;
68 class PropertyGetReply;
69 template <typename... _Results> class Result;
70 class Error;
71 template <typename _T, typename _Enable = void> struct signature_of;
72}
73
74namespace sdbus {
75
76 // Callbacks from sdbus-c++
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)>;
83
84 // Type-erased RAII-style handle to callbacks/subscriptions registered to sdbus-c++
85 using Slot = std::unique_ptr<void, std::function<void(void*)>>;
86
87 // Tag specifying that an owning handle (so-called slot) of the logical resource shall be provided to the client
88 struct return_slot_t { explicit return_slot_t() = default; };
89 inline constexpr return_slot_t return_slot{};
90 // Tag specifying that the library shall own the slot resulting from the call of the function (so-called floating slot)
91 struct floating_slot_t { explicit floating_slot_t() = default; };
92 inline constexpr floating_slot_t floating_slot{};
93 // Tag denoting the assumption that the caller has already obtained message ownership
94 struct adopt_message_t { explicit adopt_message_t() = default; };
95 inline constexpr adopt_message_t adopt_message{};
96 // Tag denoting the assumption that the caller has already obtained fd ownership
97 struct adopt_fd_t { explicit adopt_fd_t() = default; };
98 inline constexpr adopt_fd_t adopt_fd{};
99 // Tag specifying that the proxy shall not run an event loop thread on its D-Bus connection.
100 // Such proxies are typically created to carry out a simple synchronous D-Bus call(s) and then are destroyed.
102 inline constexpr dont_run_event_loop_thread_t dont_run_event_loop_thread{};
103 // Tag denoting an asynchronous call that returns std::future as a handle
104 struct with_future_t { explicit with_future_t() = default; };
105 inline constexpr with_future_t with_future{};
106 // Tag denoting a call where the reply shouldn't be waited for
107 struct dont_expect_reply_t { explicit dont_expect_reply_t() = default; };
108 inline constexpr dont_expect_reply_t dont_expect_reply{};
109
110 // Helper for static assert
111 template <class... _T> constexpr bool always_false = false;
112
113 // Helper operator+ for concatenation of `std::array`s
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);
116
117 // Template specializations for getting D-Bus signatures from C++ types
118 template <typename _T>
119 constexpr auto signature_of_v = signature_of<_T>::value;
120
121 template <typename _T, typename _Enable>
123 {
124 static constexpr bool is_valid = false;
125 static constexpr bool is_trivial_dbus_type = false;
126
127 static constexpr void* value = []
128 {
129 // See using-sdbus-c++.md, section "Extending sdbus-c++ type system",
130 // on how to teach sdbus-c++ about your custom types
131 static_assert(always_false<_T>, "Unsupported D-Bus type (specialize `signature_of` for your custom types)");
132 };
133 };
134
135 template <typename _T>
136 struct signature_of<const _T> : signature_of<_T>
137 {};
138
139 template <typename _T>
140 struct signature_of<volatile _T> : signature_of<_T>
141 {};
142
143 template <typename _T>
145 {};
146
147 template <>
148 struct signature_of<void>
149 {
150 static constexpr std::array<char, 0> value{};
151 static constexpr bool is_valid = true;
152 static constexpr bool is_trivial_dbus_type = false;
153 };
154
155 template <>
156 struct signature_of<bool>
157 {
158 static constexpr std::array value{'b'};
159 static constexpr bool is_valid = true;
160 static constexpr bool is_trivial_dbus_type = true;
161 };
162
163 template <>
164 struct signature_of<uint8_t>
165 {
166 static constexpr std::array value{'y'};
167 static constexpr bool is_valid = true;
168 static constexpr bool is_trivial_dbus_type = true;
169 };
170
171 template <>
172 struct signature_of<int16_t>
173 {
174 static constexpr std::array value{'n'};
175 static constexpr bool is_valid = true;
176 static constexpr bool is_trivial_dbus_type = true;
177 };
178
179 template <>
180 struct signature_of<uint16_t>
181 {
182 static constexpr std::array value{'q'};
183 static constexpr bool is_valid = true;
184 static constexpr bool is_trivial_dbus_type = true;
185 };
186
187 template <>
188 struct signature_of<int32_t>
189 {
190 static constexpr std::array value{'i'};
191 static constexpr bool is_valid = true;
192 static constexpr bool is_trivial_dbus_type = true;
193 };
194
195 template <>
196 struct signature_of<uint32_t>
197 {
198 static constexpr std::array value{'u'};
199 static constexpr bool is_valid = true;
200 static constexpr bool is_trivial_dbus_type = true;
201 };
202
203 template <>
204 struct signature_of<int64_t>
205 {
206 static constexpr std::array value{'x'};
207 static constexpr bool is_valid = true;
208 static constexpr bool is_trivial_dbus_type = true;
209 };
210
211 template <>
212 struct signature_of<uint64_t>
213 {
214 static constexpr std::array value{'t'};
215 static constexpr bool is_valid = true;
216 static constexpr bool is_trivial_dbus_type = true;
217 };
218
219 template <>
220 struct signature_of<double>
221 {
222 static constexpr std::array value{'d'};
223 static constexpr bool is_valid = true;
224 static constexpr bool is_trivial_dbus_type = true;
225 };
226
227 template <>
228 struct signature_of<std::string>
229 {
230 static constexpr std::array value{'s'};
231 static constexpr bool is_valid = true;
232 static constexpr bool is_trivial_dbus_type = false;
233 };
234
235 template <>
236 struct signature_of<std::string_view> : signature_of<std::string>
237 {};
238
239 template <>
242
243 template <>
245 {};
246
247 template <std::size_t _N>
248 struct signature_of<char[_N]> : signature_of<std::string>
249 {};
250
251 template <std::size_t _N>
252 struct signature_of<const char[_N]> : signature_of<std::string>
253 {};
254
255 template <>
256 struct signature_of<BusName> : signature_of<std::string>
257 {};
258
259 template <>
261 {};
262
263 template <>
264 struct signature_of<MemberName> : signature_of<std::string>
265 {};
266
267 template <typename... _ValueTypes>
268 struct signature_of<Struct<_ValueTypes...>>
269 {
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;
274 };
275
276 template <>
278 {
279 static constexpr std::array value{'v'};
280 static constexpr bool is_valid = true;
281 static constexpr bool is_trivial_dbus_type = false;
282 };
283
284 template <typename... Elements>
285 struct signature_of<std::variant<Elements...>> : signature_of<Variant>
286 {};
287
288 template <>
290 {
291 static constexpr std::array value{'o'};
292 static constexpr bool is_valid = true;
293 static constexpr bool is_trivial_dbus_type = false;
294 };
295
296 template <>
298 {
299 static constexpr std::array value{'g'};
300 static constexpr bool is_valid = true;
301 static constexpr bool is_trivial_dbus_type = false;
302 };
303
304 template <>
306 {
307 static constexpr std::array value{'h'};
308 static constexpr bool is_valid = true;
309 static constexpr bool is_trivial_dbus_type = false;
310 };
311
312 template <typename _T1, typename _T2>
313 struct signature_of<DictEntry<_T1, _T2>>
314 {
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;
318 };
319
320 template <typename _Element, typename _Allocator>
321 struct signature_of<std::vector<_Element, _Allocator>>
322 {
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;
326 };
327
328 template <typename _Element, std::size_t _Size>
329 struct signature_of<std::array<_Element, _Size>> : signature_of<std::vector<_Element>>
330 {
331 };
332
333#ifdef __cpp_lib_span
334 template <typename _Element, std::size_t _Extent>
335 struct signature_of<std::span<_Element, _Extent>> : signature_of<std::vector<_Element>>
336 {
337 };
338#endif
339
340 template <typename _Enum>
341 struct signature_of<_Enum, typename std::enable_if_t<std::is_enum_v<_Enum>>>
342 : public signature_of<std::underlying_type_t<_Enum>>
343 {};
344
345 template <typename _Key, typename _Value, typename _Compare, typename _Allocator>
346 struct signature_of<std::map<_Key, _Value, _Compare, _Allocator>>
347 {
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;
353 };
354
355 template <typename _Key, typename _Value, typename _Hash, typename _KeyEqual, typename _Allocator>
356 struct signature_of<std::unordered_map<_Key, _Value, _Hash, _KeyEqual, _Allocator>>
357 : signature_of<std::map<_Key, _Value>>
358 {
359 };
360
361 template <typename... _Types>
362 struct signature_of<std::tuple<_Types...>> // A simple concatenation of signatures of _Types
363 {
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;
367 };
368
369 // To simplify conversions of arrays to C strings
370 template <typename _T, std::size_t _N>
371 constexpr auto as_null_terminated(std::array<_T, _N> arr)
372 {
373 return arr + std::array<_T, 1>{0};
374 }
375
376 // Function traits implementation inspired by (c) kennytm,
377 // https://github.com/kennytm/utils/blob/master/traits.hpp
378 template <typename _Type>
379 struct function_traits : function_traits<decltype(&_Type::operator())>
380 {};
381
382 template <typename _Type>
383 struct function_traits<const _Type> : function_traits<_Type>
384 {};
385
386 template <typename _Type>
388 {};
389
390 template <typename _ReturnType, typename... _Args>
392 {
393 typedef _ReturnType result_type;
394 typedef std::tuple<_Args...> arguments_type;
395 typedef std::tuple<std::decay_t<_Args>...> decayed_arguments_type;
396
397 typedef _ReturnType function_type(_Args...);
398
399 static constexpr std::size_t arity = sizeof...(_Args);
400
401// template <size_t _Idx, typename _Enabled = void>
402// struct arg;
403//
404// template <size_t _Idx>
405// struct arg<_Idx, std::enable_if_t<(_Idx < arity)>>
406// {
407// typedef std::tuple_element_t<_Idx, arguments_type> type;
408// };
409//
410// template <size_t _Idx>
411// struct arg<_Idx, std::enable_if_t<!(_Idx < arity)>>
412// {
413// typedef void type;
414// };
415
416 template <size_t _Idx>
417 struct arg
418 {
419 typedef std::tuple_element_t<_Idx, std::tuple<_Args...>> type;
420 };
421
422 template <size_t _Idx>
423 using arg_t = typename arg<_Idx>::type;
424 };
425
426 template <typename _ReturnType, typename... _Args>
427 struct function_traits<_ReturnType(_Args...)> : function_traits_base<_ReturnType, _Args...>
428 {
429 static constexpr bool is_async = false;
430 static constexpr bool has_error_param = false;
431 };
432
433 template <typename... _Args>
434 struct function_traits<void(std::optional<Error>, _Args...)> : function_traits_base<void, _Args...>
435 {
436 static constexpr bool has_error_param = true;
437 };
438
439 template <typename... _Args, typename... _Results>
440 struct function_traits<void(Result<_Results...>, _Args...)> : function_traits_base<std::tuple<_Results...>, _Args...>
441 {
442 static constexpr bool is_async = true;
443 using async_result_t = Result<_Results...>;
444 };
445
446 template <typename... _Args, typename... _Results>
447 struct function_traits<void(Result<_Results...>&&, _Args...)> : function_traits_base<std::tuple<_Results...>, _Args...>
448 {
449 static constexpr bool is_async = true;
450 using async_result_t = Result<_Results...>;
451 };
452
453 template <typename _ReturnType, typename... _Args>
454 struct function_traits<_ReturnType(*)(_Args...)> : function_traits<_ReturnType(_Args...)>
455 {};
456
457 template <typename _ClassType, typename _ReturnType, typename... _Args>
458 struct function_traits<_ReturnType(_ClassType::*)(_Args...)> : function_traits<_ReturnType(_Args...)>
459 {
460 typedef _ClassType& owner_type;
461 };
462
463 template <typename _ClassType, typename _ReturnType, typename... _Args>
464 struct function_traits<_ReturnType(_ClassType::*)(_Args...) const> : function_traits<_ReturnType(_Args...)>
465 {
466 typedef const _ClassType& owner_type;
467 };
468
469 template <typename _ClassType, typename _ReturnType, typename... _Args>
470 struct function_traits<_ReturnType(_ClassType::*)(_Args...) volatile> : function_traits<_ReturnType(_Args...)>
471 {
472 typedef volatile _ClassType& owner_type;
473 };
474
475 template <typename _ClassType, typename _ReturnType, typename... _Args>
476 struct function_traits<_ReturnType(_ClassType::*)(_Args...) const volatile> : function_traits<_ReturnType(_Args...)>
477 {
478 typedef const volatile _ClassType& owner_type;
479 };
480
481 template <typename FunctionType>
482 struct function_traits<std::function<FunctionType>> : function_traits<FunctionType>
483 {};
484
485 template <class _Function>
486 constexpr auto is_async_method_v = function_traits<_Function>::is_async;
487
488 template <class _Function>
489 constexpr auto has_error_param_v = function_traits<_Function>::has_error_param;
490
491 template <typename _FunctionType>
492 using function_arguments_t = typename function_traits<_FunctionType>::arguments_type;
493
494 template <typename _FunctionType, size_t _Idx>
495 using function_argument_t = typename function_traits<_FunctionType>::template arg_t<_Idx>;
496
497 template <typename _FunctionType>
498 constexpr auto function_argument_count_v = function_traits<_FunctionType>::arity;
499
500 template <typename _FunctionType>
501 using function_result_t = typename function_traits<_FunctionType>::result_type;
502
503 template <typename _Function>
508
509 template <typename _Function>
510 using tuple_of_function_input_arg_types_t = typename tuple_of_function_input_arg_types<_Function>::type;
511
512 template <typename _Function>
517
518 template <typename _Function>
519 using tuple_of_function_output_arg_types_t = typename tuple_of_function_output_arg_types<_Function>::type;
520
521 template <typename _Function>
522 struct signature_of_function_input_arguments : signature_of<tuple_of_function_input_arg_types_t<_Function>>
523 {
524 static std::string value_as_string()
525 {
526 constexpr auto signature = as_null_terminated(signature_of_v<tuple_of_function_input_arg_types_t<_Function>>);
527 return signature.data();
528 }
529 };
530
531 template <typename _Function>
532 inline auto signature_of_function_input_arguments_v = signature_of_function_input_arguments<_Function>::value_as_string();
533
534 template <typename _Function>
535 struct signature_of_function_output_arguments : signature_of<tuple_of_function_output_arg_types_t<_Function>>
536 {
537 static std::string value_as_string()
538 {
539 constexpr auto signature = as_null_terminated(signature_of_v<tuple_of_function_output_arg_types_t<_Function>>);
540 return signature.data();
541 }
542 };
543
544 template <typename _Function>
545 inline auto signature_of_function_output_arguments_v = signature_of_function_output_arguments<_Function>::value_as_string();
546
547 // std::future stuff for return values of async calls
548 template <typename... _Args> struct future_return
549 {
550 typedef std::tuple<_Args...> type;
551 };
552
553 template <> struct future_return<>
554 {
555 typedef void type;
556 };
557
558 template <typename _Type> struct future_return<_Type>
559 {
560 typedef _Type type;
561 };
562
563 template <typename... _Args>
564 using future_return_t = typename future_return<_Args...>::type;
565
566 // Credit: Piotr Skotnicki (https://stackoverflow.com/a/57639506)
567 template <typename, typename>
568 constexpr bool is_one_of_variants_types = false;
569
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> || ...);
573
574 namespace detail
575 {
576 template <class _Function, class _Tuple, typename... _Args, std::size_t... _I>
577 constexpr decltype(auto) apply_impl( _Function&& f
578 , Result<_Args...>&& r
579 , _Tuple&& t
580 , std::index_sequence<_I...> )
581 {
582 return std::forward<_Function>(f)(std::move(r), std::get<_I>(std::forward<_Tuple>(t))...);
583 }
584
585 template <class _Function, class _Tuple, std::size_t... _I>
586 decltype(auto) apply_impl( _Function&& f
587 , std::optional<Error> e
588 , _Tuple&& t
589 , std::index_sequence<_I...> )
590 {
591 return std::forward<_Function>(f)(std::move(e), std::get<_I>(std::forward<_Tuple>(t))...);
592 }
593
594 // For non-void returning functions, apply_impl simply returns function return value (a tuple of values).
595 // For void-returning functions, apply_impl returns an empty tuple.
596 template <class _Function, class _Tuple, std::size_t... _I>
597 constexpr decltype(auto) apply_impl( _Function&& f
598 , _Tuple&& t
599 , std::index_sequence<_I...> )
600 {
601 if constexpr (!std::is_void_v<function_result_t<_Function>>)
602 return std::forward<_Function>(f)(std::get<_I>(std::forward<_Tuple>(t))...);
603 else
604 return std::forward<_Function>(f)(std::get<_I>(std::forward<_Tuple>(t))...), std::tuple<>{};
605 }
606 }
607
608 // Convert tuple `t' of values into a list of arguments
609 // and invoke function `f' with those arguments.
610 template <class _Function, class _Tuple>
611 constexpr decltype(auto) apply(_Function&& f, _Tuple&& t)
612 {
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>{} );
616 }
617
618 // Convert tuple `t' of values into a list of arguments
619 // and invoke function `f' with those arguments.
620 template <class _Function, class _Tuple, typename... _Args>
621 constexpr decltype(auto) apply(_Function&& f, Result<_Args...>&& r, _Tuple&& t)
622 {
623 return detail::apply_impl( std::forward<_Function>(f)
624 , std::move(r)
625 , std::forward<_Tuple>(t)
626 , std::make_index_sequence<std::tuple_size<std::decay_t<_Tuple>>::value>{} );
627 }
628
629 // Convert tuple `t' of values into a list of arguments
630 // and invoke function `f' with those arguments.
631 template <class _Function, class _Tuple>
632 decltype(auto) apply(_Function&& f, std::optional<Error> e, _Tuple&& t)
633 {
634 return detail::apply_impl( std::forward<_Function>(f)
635 , std::move(e)
636 , std::forward<_Tuple>(t)
637 , std::make_index_sequence<std::tuple_size<std::decay_t<_Tuple>>::value>{} );
638 }
639
640 // Convenient concatenation of arrays
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)
643 {
644 std::array<_T, _N1 + _N2> result{};
645 std::size_t index = 0;
646
647 for (auto& el : lhs) {
648 result[index] = std::move(el);
649 ++index;
650 }
651 for (auto& el : rhs) {
652 result[index] = std::move(el);
653 ++index;
654 }
655
656 return result;
657 }
658
659}
660
661#endif /* SDBUS_CXX_TYPETRAITS_H_ */
std::pair< _T1, _T2 > DictEntry
Definition Types.h:384
Definition Types.h:197
Definition Error.h:44
Definition Types.h:220
Definition Types.h:240
Definition Message.h:81
Definition Message.h:261
Definition Message.h:286
Definition Types.h:177
Definition Message.h:317
Definition Message.h:308
Definition TypeTraits.h:69
Definition Message.h:296
Definition Types.h:264
Definition TypeTraits.h:55
Definition Types.h:289
Definition Types.h:56
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:123
Definition TypeTraits.h:505
Definition TypeTraits.h:104