27#ifndef SDBUS_CXX_TYPES_H_
28#define SDBUS_CXX_TYPES_H_
61 template <
typename _ValueType>
64 msg_.openVariant<_ValueType>();
78 template <
typename _Struct>
81 msg_.openVariant<std::map<std::string, Variant>>();
87 template <
typename... _Elements>
88 Variant(
const std::variant<_Elements...>& value)
95 template <
typename _ValueType>
96 _ValueType get()
const
100 msg_.enterVariant<_ValueType>();
108 template <typename _ValueType, typename = std::enable_if_t<signature_of<_ValueType>::is_valid>>
109 explicit operator _ValueType()
const
111 return get<_ValueType>();
114 template <
typename... _Elements>
115 operator std::variant<_Elements...>()
const
117 std::variant<_Elements...> result;
123 template <
typename _Type>
124 bool containsValueOfType()
const
126 constexpr auto signature = as_null_terminated(signature_of_v<_Type>);
127 return std::strcmp(signature.data(), peekValueType()) == 0;
130 bool isEmpty()
const;
132 void serializeTo(
Message& msg)
const;
133 void deserializeFrom(
Message& msg);
134 const char* peekValueType()
const;
150 template <
typename... _ValueTypes>
152 :
public std::tuple<_ValueTypes...>
155 using std::tuple<_ValueTypes...>::tuple;
159 explicit Struct(
const std::tuple<_ValueTypes...>& t)
160 : std::tuple<_ValueTypes...>(t)
164 template <std::
size_t _I>
167 return std::get<_I>(*
this);
170 template <std::
size_t _I>
171 const auto& get()
const
173 return std::get<_I>(*
this);
177 template <
typename... _Elements>
178 Struct(_Elements...) -> Struct<_Elements...>;
180 template<
typename... _Elements>
181 constexpr Struct<std::decay_t<_Elements>...>
182 make_struct(_Elements&&... args)
184 typedef Struct<std::decay_t<_Elements>...> result_type;
185 return result_type(std::forward<_Elements>(args)...);
199 : std::string(std::move(value))
205 using std::string::operator=;
218 explicit BusName(std::string value)
219 : std::string(std::move(value))
221 explicit BusName(
const char* value)
225 using std::string::operator=;
242 : std::string(std::move(value))
248 using std::string::operator=;
262 : std::string(std::move(value))
268 using std::string::operator=;
286 : std::string(std::move(value))
292 using std::string::operator=;
312 : fd_(checkedDup(fd))
333 fd_ = checkedDup(other.fd_);
339 *
this = std::move(other);
349 fd_ = std::exchange(other.fd_, -1);
358 [[nodiscard]]
int get()
const
363 void reset(
int fd = -1)
370 *
this =
UnixFd{fd, adopt_fd};
375 return std::exchange(fd_, -1);
378 [[nodiscard]]
bool isValid()
const
389 static int checkedDup(
int fd);
401 template<
typename _T1,
typename _T2>
407template <
size_t _I,
typename... _ValueTypes>
408struct std::tuple_element<_I, sdbus::Struct<_ValueTypes...>>
409 : std::tuple_element<_I, std::tuple<_ValueTypes...>>
411template <
typename... _ValueTypes>
412struct std::tuple_size<sdbus::Struct<_ValueTypes...>>
413 : std::tuple_size<std::tuple<_ValueTypes...>>
453#define SDBUSCPP_REGISTER_STRUCT(STRUCT, ...) \
455 static_assert(SDBUSCPP_PP_NARG(__VA_ARGS__) <= 16, \
456 "Not more than 16 struct members are supported, please open an issue if you need more"); \
459 struct signature_of<STRUCT> \
460 : signature_of<sdbus::Struct<SDBUSCPP_STRUCT_MEMBER_TYPES(STRUCT, __VA_ARGS__)>> \
463 inline auto as_dictionary_if_struct(const STRUCT& object) \
465 return as_dictionary<STRUCT>(object); \
468 inline sdbus::Message& operator<<(sdbus::Message& msg, const STRUCT& items) \
470 return msg << sdbus::Struct{std::forward_as_tuple(SDBUSCPP_STRUCT_MEMBERS(items, __VA_ARGS__))}; \
473 inline Message& operator<<(Message& msg, const as_dictionary<STRUCT>& s) \
475 if constexpr (!nested_struct_as_dict_serialization_v<STRUCT>) \
476 return msg.serializeDictionary<std::string, Variant>({SDBUSCPP_STRUCT_MEMBERS_AS_DICT_ENTRIES(s.m_struct, __VA_ARGS__)}); \
478 return msg.serializeDictionary<std::string, Variant>({SDBUSCPP_STRUCT_MEMBERS_AS_NESTED_DICT_ENTRIES(s.m_struct, __VA_ARGS__)}); \
481 inline Message& operator>>(Message& msg, STRUCT& s) \
484 if (msg.peekType().first == signature_of<STRUCT>::type_value) \
486 Struct sdbusStruct{std::forward_as_tuple(SDBUSCPP_STRUCT_MEMBERS(s, __VA_ARGS__))}; \
487 return msg >> sdbusStruct; \
492 return msg.deserializeDictionary<std::string, Variant>([&s](const auto& dictEntry) \
494 const std::string& key = dictEntry.first; \
495 const Variant& value = dictEntry.second; \
497 using namespace std::string_literals; \
499 SDBUSCPP_FIND_AND_DESERIALIZE_STRUCT_MEMBERS(s, __VA_ARGS__) \
500 SDBUS_THROW_ERROR_IF( strict_dict_as_struct_deserialization_v<STRUCT> \
501 , ("Failed to deserialize struct from a dictionary: could not find field '"s += key) += "' in struct 'my::Struct'" \
520#define SDBUSCPP_ENABLE_RELAXED_DICT2STRUCT_DESERIALIZATION(STRUCT) \
522 constexpr auto sdbus::strict_dict_as_struct_deserialization_v<STRUCT> = false; \
537#define SDBUSCPP_ENABLE_NESTED_STRUCT2DICT_SERIALIZATION(STRUCT) \
539 constexpr auto sdbus::nested_struct_as_dict_serialization_v<STRUCT> = true \
547#define SDBUSCPP_STRUCT_MEMBERS(STRUCT, ...) \
548 SDBUSCPP_PP_CAT(SDBUSCPP_FOR_EACH_, SDBUSCPP_PP_NARG(__VA_ARGS__))(SDBUSCPP_STRUCT_MEMBER, SDBUSCPP_PP_COMMA, STRUCT, __VA_ARGS__) \
550#define SDBUSCPP_STRUCT_MEMBER(STRUCT, MEMBER) STRUCT.MEMBER
552#define SDBUSCPP_STRUCT_MEMBER_TYPES(STRUCT, ...) \
553 SDBUSCPP_PP_CAT(SDBUSCPP_FOR_EACH_, SDBUSCPP_PP_NARG(__VA_ARGS__))(SDBUSCPP_STRUCT_MEMBER_TYPE, SDBUSCPP_PP_COMMA, STRUCT, __VA_ARGS__) \
555#define SDBUSCPP_STRUCT_MEMBER_TYPE(STRUCT, MEMBER) decltype(STRUCT::MEMBER)
557#define SDBUSCPP_STRUCT_MEMBERS_AS_DICT_ENTRIES(STRUCT, ...) \
558 SDBUSCPP_PP_CAT(SDBUSCPP_FOR_EACH_, SDBUSCPP_PP_NARG(__VA_ARGS__))(SDBUSCPP_STRUCT_MEMBER_AS_DICT_ENTRY, SDBUSCPP_PP_COMMA, STRUCT, __VA_ARGS__) \
560#define SDBUSCPP_STRUCT_MEMBER_AS_DICT_ENTRY(STRUCT, MEMBER) {#MEMBER, Variant{STRUCT.MEMBER}}
562#define SDBUSCPP_STRUCT_MEMBERS_AS_NESTED_DICT_ENTRIES(STRUCT, ...) \
563 SDBUSCPP_PP_CAT(SDBUSCPP_FOR_EACH_, SDBUSCPP_PP_NARG(__VA_ARGS__))(SDBUSCPP_STRUCT_MEMBER_AS_NESTED_DICT_ENTRY, SDBUSCPP_PP_COMMA, STRUCT, __VA_ARGS__) \
565#define SDBUSCPP_STRUCT_MEMBER_AS_NESTED_DICT_ENTRY(STRUCT, MEMBER) {#MEMBER, Variant{as_dictionary_if_struct(STRUCT.MEMBER)}}
567#define SDBUSCPP_FIND_AND_DESERIALIZE_STRUCT_MEMBERS(STRUCT, ...) \
568 SDBUSCPP_PP_CAT(SDBUSCPP_FOR_EACH_, SDBUSCPP_PP_NARG(__VA_ARGS__))(SDBUSCPP_FIND_AND_DESERIALIZE_STRUCT_MEMBER, SDBUSCPP_PP_SPACE, STRUCT, __VA_ARGS__) \
570#define SDBUSCPP_FIND_AND_DESERIALIZE_STRUCT_MEMBER(STRUCT, MEMBER) if (key == #MEMBER) STRUCT.MEMBER = value.get<decltype(STRUCT.MEMBER)>(); else
572#define SDBUSCPP_FOR_EACH_1(M, D, S, M1) M(S, M1)
573#define SDBUSCPP_FOR_EACH_2(M, D, S, M1, M2) M(S, M1) D M(S, M2)
574#define SDBUSCPP_FOR_EACH_3(M, D, S, M1, M2, M3) M(S, M1) D M(S, M2) D M(S, M3)
575#define SDBUSCPP_FOR_EACH_4(M, D, S, M1, M2, M3, M4) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4)
576#define SDBUSCPP_FOR_EACH_5(M, D, S, M1, M2, M3, M4, M5) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5)
577#define SDBUSCPP_FOR_EACH_6(M, D, S, M1, M2, M3, M4, M5, M6) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6)
578#define SDBUSCPP_FOR_EACH_7(M, D, S, M1, M2, M3, M4, M5, M6, M7) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7)
579#define SDBUSCPP_FOR_EACH_8(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8)
580#define SDBUSCPP_FOR_EACH_9(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9)
581#define SDBUSCPP_FOR_EACH_10(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10)
582#define SDBUSCPP_FOR_EACH_11(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10) D M(S, M11)
583#define SDBUSCPP_FOR_EACH_12(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10) D M(S, M11) D M(S, M12)
584#define SDBUSCPP_FOR_EACH_13(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10) D M(S, M11) D M(S, M12) D M(S, M13)
585#define SDBUSCPP_FOR_EACH_14(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10) D M(S, M11) D M(S, M12) D M(S, M13) D M(S, M14)
586#define SDBUSCPP_FOR_EACH_15(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10) D M(S, M11) D M(S, M12) D M(S, M13) D M(S, M14) D M(S, M15)
587#define SDBUSCPP_FOR_EACH_16(M, D, S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15, M16) M(S, M1) D M(S, M2) D M(S, M3) D M(S, M4) D M(S, M5) D M(S, M6) D M(S, M7) D M(S, M8) D M(S, M9) D M(S, M10) D M(S, M11) D M(S, M12) D M(S, M13) D M(S, M14) D M(S, M15) D M(S, M16)
589#define SDBUSCPP_PP_CAT(X, Y) SDBUSCPP_PP_CAT_IMPL(X, Y)
590#define SDBUSCPP_PP_CAT_IMPL(X, Y) X##Y
591#define SDBUSCPP_PP_NARG(...) SDBUSCPP_PP_NARG_IMPL(__VA_ARGS__, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
592#define SDBUSCPP_PP_NARG_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _N, ...) _N
594#define SDBUSCPP_PP_COMMA ,
595#define SDBUSCPP_PP_SPACE
std::pair< _T1, _T2 > DictEntry
Definition Types.h:402
Definition TypeTraits.h:55
Definition TypeTraits.h:97
Definition TypeTraits.h:585
Definition TypeTraits.h:110