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>
163 constexpr Struct<std::decay_t<_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
388template <size_t _I, typename... _ValueTypes>
389struct std::tuple_element<_I, sdbus::Struct<_ValueTypes...>>
390 : std::tuple_element<_I, std::tuple<_ValueTypes...>>
391{};
392
393template <typename... _ValueTypes>
394struct std::tuple_size<sdbus::Struct<_ValueTypes...>>
395 : std::tuple_size<std::tuple<_ValueTypes...>>
396{};
397
398#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