sdbus-c++ 2.0.0
High-level C++ D-Bus library based on systemd D-Bus implementation
Loading...
Searching...
No Matches
Types.h
Go to the documentation of this file.
1
27#ifndef SDBUS_CXX_TYPES_H_
28#define SDBUS_CXX_TYPES_H_
29
30#include <sdbus-c++/Message.h>
32
33#include <cstring>
34#include <memory>
35#include <string>
36#include <tuple>
37#include <type_traits>
38#include <typeinfo>
39#include <utility>
40
41namespace sdbus {
42
43 /********************************************/
55 class Variant
56 {
57 public:
58 Variant();
59
60 template <typename _ValueType>
61 explicit Variant(const _ValueType& value) : Variant()
62 {
63 msg_.openVariant<_ValueType>();
64 msg_ << value;
65 msg_.closeVariant();
66 msg_.seal();
67 }
68
69 template <typename... _Elements>
70 Variant(const std::variant<_Elements...>& value)
71 : Variant()
72 {
73 msg_ << value;
74 msg_.seal();
75 }
76
77 template <typename _ValueType>
78 _ValueType get() const
79 {
80 _ValueType val;
81 msg_.rewind(false);
82
83 msg_.enterVariant<_ValueType>();
84 msg_ >> val;
85 msg_.exitVariant();
86 return val;
87 }
88
89 // Only allow conversion operator for true D-Bus type representations in C++
90 template <typename _ValueType, typename = std::enable_if_t<signature_of<_ValueType>::is_valid>>
91 explicit operator _ValueType() const
92 {
93 return get<_ValueType>();
94 }
95
96 template <typename... _Elements>
97 operator std::variant<_Elements...>() const
98 {
99 std::variant<_Elements...> result;
100 msg_.rewind(false);
101 msg_ >> result;
102 return result;
103 }
104
105 template <typename _Type>
106 bool containsValueOfType() const
107 {
108 constexpr auto signature = as_null_terminated(signature_of_v<_Type>);
109 return std::strcmp(signature.data(), peekValueType()) == 0;
110 }
111
112 bool isEmpty() const;
113
114 void serializeTo(Message& msg) const;
115 void deserializeFrom(Message& msg);
116 const char* peekValueType() const;
117
118 private:
119 mutable PlainMessage msg_{};
120 };
121
122 /********************************************/
132 template <typename... _ValueTypes>
133 class Struct
134 : public std::tuple<_ValueTypes...>
135 {
136 public:
137 using std::tuple<_ValueTypes...>::tuple;
138
139 Struct() = default;
140
141 explicit Struct(const std::tuple<_ValueTypes...>& t)
142 : std::tuple<_ValueTypes...>(t)
143 {
144 }
145
146 template <std::size_t _I>
147 auto& get()
148 {
149 return std::get<_I>(*this);
150 }
151
152 template <std::size_t _I>
153 const auto& get() const
154 {
155 return std::get<_I>(*this);
156 }
157 };
158
159 template <typename... _Elements>
160 Struct(_Elements...) -> Struct<_Elements...>;
161
162 template<typename... _Elements>
164 make_struct(_Elements&&... args)
165 {
166 typedef Struct<std::decay_t<_Elements>...> result_type;
167 return result_type(std::forward<_Elements>(args)...);
168 }
169
170 /********************************************/
176 class ObjectPath : public std::string
177 {
178 public:
179 ObjectPath() = default;
180 explicit ObjectPath(std::string value)
181 : std::string(std::move(value))
182 {}
183 explicit ObjectPath(const char* value)
184 : std::string(value)
185 {}
186
187 using std::string::operator=;
188 };
189
190 /********************************************/
196 class BusName : public std::string
197 {
198 public:
199 BusName() = default;
200 explicit BusName(std::string value)
201 : std::string(std::move(value))
202 {}
203 explicit BusName(const char* value)
204 : std::string(value)
205 {}
206
207 using std::string::operator=;
208 };
209
210 using ServiceName = BusName;
211 using ConnectionName = BusName;
212
213 /********************************************/
219 class InterfaceName : public std::string
220 {
221 public:
222 InterfaceName() = default;
223 explicit InterfaceName(std::string value)
224 : std::string(std::move(value))
225 {}
226 explicit InterfaceName(const char* value)
227 : std::string(value)
228 {}
229
230 using std::string::operator=;
231 };
232
233 /********************************************/
239 class MemberName : public std::string
240 {
241 public:
242 MemberName() = default;
243 explicit MemberName(std::string value)
244 : std::string(std::move(value))
245 {}
246 explicit MemberName(const char* value)
247 : std::string(value)
248 {}
249
250 using std::string::operator=;
251 };
252
253 using MethodName = MemberName;
254 using SignalName = MemberName;
255 using PropertyName = MemberName;
256
257 /********************************************/
263 class Signature : public std::string
264 {
265 public:
266 Signature() = default;
267 explicit Signature(std::string value)
268 : std::string(std::move(value))
269 {}
270 explicit Signature(const char* value)
271 : std::string(value)
272 {}
273
274 using std::string::operator=;
275 };
276
277 /********************************************/
288 class UnixFd
289 {
290 public:
291 UnixFd() = default;
292
293 explicit UnixFd(int fd)
294 : fd_(checkedDup(fd))
295 {
296 }
297
298 UnixFd(int fd, adopt_fd_t)
299 : fd_(fd)
300 {
301 }
302
303 UnixFd(const UnixFd& other)
304 {
305 *this = other;
306 }
307
308 UnixFd& operator=(const UnixFd& other)
309 {
310 if (this == &other)
311 {
312 return *this;
313 }
314 close();
315 fd_ = checkedDup(other.fd_);
316 return *this;
317 }
318
319 UnixFd(UnixFd&& other)
320 {
321 *this = std::move(other);
322 }
323
324 UnixFd& operator=(UnixFd&& other)
325 {
326 if (this == &other)
327 {
328 return *this;
329 }
330 close();
331 fd_ = std::exchange(other.fd_, -1);
332 return *this;
333 }
334
335 ~UnixFd()
336 {
337 close();
338 }
339
340 [[nodiscard]] int get() const
341 {
342 return fd_;
343 }
344
345 void reset(int fd = -1)
346 {
347 *this = UnixFd{fd};
348 }
349
350 void reset(int fd, adopt_fd_t)
351 {
352 *this = UnixFd{fd, adopt_fd};
353 }
354
355 int release()
356 {
357 return std::exchange(fd_, -1);
358 }
359
360 [[nodiscard]] bool isValid() const
361 {
362 return fd_ >= 0;
363 }
364
365 private:
367 void close();
368
371 static int checkedDup(int fd);
372
373 int fd_ = -1;
374 };
375
376 /********************************************/
383 template<typename _T1, typename _T2>
384 using DictEntry = std::pair<_T1, _T2>;
385
386}
387
388// Making sdbus::Struct implement the tuple-protocol, i.e. be a tuple-like type
389template <size_t _I, typename... _ValueTypes>
390struct std::tuple_element<_I, sdbus::Struct<_ValueTypes...>>
391 : std::tuple_element<_I, std::tuple<_ValueTypes...>>
392{};
393template <typename... _ValueTypes>
394struct std::tuple_size<sdbus::Struct<_ValueTypes...>>
395 : std::tuple_size<std::tuple<_ValueTypes...>>
396{};
397
398/********************************************/
436#define SDBUSCPP_REGISTER_STRUCT(STRUCT, ...) \
437 namespace sdbus { \
438 static_assert(SDBUSCPP_PP_NARG(__VA_ARGS__) <= 16, \
439 "Not more than 16 struct members are supported, please open an issue if you need more"); \
440 inline sdbus::Message& operator<<(sdbus::Message& msg, const STRUCT& items) \
441 { \
442 return msg << sdbus::Struct{std::forward_as_tuple(SDBUSCPP_STRUCT_MEMBERS(items, __VA_ARGS__))}; \
443 } \
444 inline sdbus::Message& operator>>(sdbus::Message& msg, STRUCT& items) \
445 { \
446 sdbus::Struct s{std::forward_as_tuple(SDBUSCPP_STRUCT_MEMBERS(items, __VA_ARGS__))}; \
447 return msg >> s; \
448 } \
449 template <> \
450 struct signature_of<STRUCT> \
451 : signature_of<sdbus::Struct<SDBUSCPP_STRUCT_MEMBER_TYPES(STRUCT, __VA_ARGS__)>> \
452 {}; \
453 } \
454
455
461#define SDBUSCPP_STRUCT_MEMBERS(STRUCT, ...) \
462 SDBUSCPP_PP_CAT(SDBUSCPP_STRUCT_MEMBERS_, SDBUSCPP_PP_NARG(__VA_ARGS__))(STRUCT, __VA_ARGS__) \
463
464#define SDBUSCPP_STRUCT_MEMBERS_1(S, M1) S.M1
465#define SDBUSCPP_STRUCT_MEMBERS_2(S, M1, M2) S.M1, S.M2
466#define SDBUSCPP_STRUCT_MEMBERS_3(S, M1, M2, M3) S.M1, S.M2, S.M3
467#define SDBUSCPP_STRUCT_MEMBERS_4(S, M1, M2, M3, M4) S.M1, S.M2, S.M3, S.M4
468#define SDBUSCPP_STRUCT_MEMBERS_5(S, M1, M2, M3, M4, M5) S.M1, S.M2, S.M3, S.M4, S.M5
469#define SDBUSCPP_STRUCT_MEMBERS_6(S, M1, M2, M3, M4, M5, M6) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6
470#define SDBUSCPP_STRUCT_MEMBERS_7(S, M1, M2, M3, M4, M5, M6, M7) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7
471#define SDBUSCPP_STRUCT_MEMBERS_8(S, M1, M2, M3, M4, M5, M6, M7, M8) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8
472#define SDBUSCPP_STRUCT_MEMBERS_9(S, M1, M2, M3, M4, M5, M6, M7, M8, M9) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9
473#define SDBUSCPP_STRUCT_MEMBERS_10(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10
474#define SDBUSCPP_STRUCT_MEMBERS_11(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10, S.M11
475#define SDBUSCPP_STRUCT_MEMBERS_12(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10, S.M11, S.M12
476#define SDBUSCPP_STRUCT_MEMBERS_13(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10, S.M11, S.M12, S.M13
477#define SDBUSCPP_STRUCT_MEMBERS_14(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10, S.M11, S.M12, S.M13, S.M14
478#define SDBUSCPP_STRUCT_MEMBERS_15(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10, S.M11, S.M12, S.M13, S.M14, S.M15
479#define SDBUSCPP_STRUCT_MEMBERS_16(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15, M16) S.M1, S.M2, S.M3, S.M4, S.M5, S.M6, S.M7, S.M8, S.M9, S.M10, S.M11, S.M12, S.M13, S.M14, S.M15, S.M16
480
481#define SDBUSCPP_STRUCT_MEMBER_TYPES(STRUCT, ...) \
482 SDBUSCPP_PP_CAT(SDBUSCPP_STRUCT_MEMBER_TYPES_, SDBUSCPP_PP_NARG(__VA_ARGS__))(STRUCT, __VA_ARGS__) \
483
484#define SDBUSCPP_STRUCT_MEMBER_TYPES_1(S, M1) decltype(S::M1)
485#define SDBUSCPP_STRUCT_MEMBER_TYPES_2(S, M1, M2) decltype(S::M1), decltype(S::M2)
486#define SDBUSCPP_STRUCT_MEMBER_TYPES_3(S, M1, M2, M3) decltype(S::M1), decltype(S::M2), decltype(S::M3)
487#define SDBUSCPP_STRUCT_MEMBER_TYPES_4(S, M1, M2, M3, M4) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4)
488#define SDBUSCPP_STRUCT_MEMBER_TYPES_5(S, M1, M2, M3, M4, M5) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5)
489#define SDBUSCPP_STRUCT_MEMBER_TYPES_6(S, M1, M2, M3, M4, M5, M6) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6)
490#define SDBUSCPP_STRUCT_MEMBER_TYPES_7(S, M1, M2, M3, M4, M5, M6, M7) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7)
491#define SDBUSCPP_STRUCT_MEMBER_TYPES_8(S, M1, M2, M3, M4, M5, M6, M7, M8) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8)
492#define SDBUSCPP_STRUCT_MEMBER_TYPES_9(S, M1, M2, M3, M4, M5, M6, M7, M8, M9) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9)
493#define SDBUSCPP_STRUCT_MEMBER_TYPES_10(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10)
494#define SDBUSCPP_STRUCT_MEMBER_TYPES_11(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10), decltype(S::M11)
495#define SDBUSCPP_STRUCT_MEMBER_TYPES_12(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10), decltype(S::M11), decltype(S::M12)
496#define SDBUSCPP_STRUCT_MEMBER_TYPES_13(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10), decltype(S::M11), decltype(S::M12), decltype(S::M13)
497#define SDBUSCPP_STRUCT_MEMBER_TYPES_14(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10), decltype(S::M11), decltype(S::M12), decltype(S::M13), decltype(S::M14)
498#define SDBUSCPP_STRUCT_MEMBER_TYPES_15(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10), decltype(S::M11), decltype(S::M12), decltype(S::M13), decltype(S::M14), decltype(S::M15)
499#define SDBUSCPP_STRUCT_MEMBER_TYPES_16(S, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15, M16) decltype(S::M1), decltype(S::M2), decltype(S::M3), decltype(S::M4), decltype(S::M5), decltype(S::M6), decltype(S::M7), decltype(S::M8), decltype(S::M9), decltype(S::M10), decltype(S::M11), decltype(S::M12), decltype(S::M13), decltype(S::M14), decltype(S::M15), decltype(S::M16)
500
501#define SDBUSCPP_PP_CAT(X, Y) SDBUSCPP_PP_CAT_IMPL(X, Y)
502#define SDBUSCPP_PP_CAT_IMPL(X, Y) X##Y
503#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)
504#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
505
506#endif /* SDBUS_CXX_TYPES_H_ */
std::pair< _T1, _T2 > DictEntry
Definition Types.h:384
Definition Types.h:197
Definition Types.h:220
Definition Types.h:240
Definition Message.h:81
Definition Types.h:177
Definition Message.h:327
Definition Types.h:264
Definition Types.h:135
Definition Types.h:289
Definition Types.h:56
Definition TypeTraits.h:97