sdbus-c++ 1.5.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#include <string>
33#include <type_traits>
34#include <typeinfo>
35#include <memory>
36#include <tuple>
37#include <utility>
38
39namespace sdbus {
40
41 /********************************************/
53 class Variant
54 {
55 public:
56 Variant();
57
58 template <typename _ValueType>
59 Variant(const _ValueType& value)
60 : Variant()
61 {
62 msg_.openVariant(signature_of<_ValueType>::str());
63 msg_ << value;
64 msg_.closeVariant();
65 msg_.seal();
66 }
67
68 template <typename _ValueType>
69 _ValueType get() const
70 {
71 _ValueType val;
72 msg_.rewind(false);
73 msg_.enterVariant(signature_of<_ValueType>::str());
74 msg_ >> val;
75 msg_.exitVariant();
76 return val;
77 }
78
79 // Only allow conversion operator for true D-Bus type representations in C++
80 template <typename _ValueType, typename = std::enable_if_t<signature_of<_ValueType>::is_valid>>
81 operator _ValueType() const
82 {
83 return get<_ValueType>();
84 }
85
86 template <typename _Type>
87 bool containsValueOfType() const
88 {
89 return signature_of<_Type>::str() == peekValueType();
90 }
91
92 bool isEmpty() const;
93
94 void serializeTo(Message& msg) const;
95 void deserializeFrom(Message& msg);
96 std::string peekValueType() const;
97
98 private:
99 mutable PlainMessage msg_{};
100 };
101
102 /********************************************/
112 template <typename... _ValueTypes>
113 class Struct
114 : public std::tuple<_ValueTypes...>
115 {
116 public:
117 using std::tuple<_ValueTypes...>::tuple;
118
119 // Disable constructor if an older then 7.1.0 version of GCC is used
120#if !((defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__) && !(__GNUC__ > 7 || (__GNUC__ == 7 && (__GNUC_MINOR__ > 1 || (__GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ > 0)))))
121 Struct() = default;
122
123 explicit Struct(const std::tuple<_ValueTypes...>& t)
124 : std::tuple<_ValueTypes...>(t)
125 {
126 }
127#endif
128
129 template <std::size_t _I>
130 auto& get()
131 {
132 return std::get<_I>(*this);
133 }
134
135 template <std::size_t _I>
136 const auto& get() const
137 {
138 return std::get<_I>(*this);
139 }
140 };
141
142 template <typename... _Elements>
143 Struct(_Elements...) -> Struct<_Elements...>;
144
145 template<typename... _Elements>
146 constexpr Struct<std::decay_t<_Elements>...>
147 make_struct(_Elements&&... args)
148 {
149 typedef Struct<std::decay_t<_Elements>...> result_type;
150 return result_type(std::forward<_Elements>(args)...);
151 }
152
153 /********************************************/
159 class ObjectPath : public std::string
160 {
161 public:
162 using std::string::string;
163 ObjectPath() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
164 ObjectPath(const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
165 ObjectPath(ObjectPath&&) = default; // Enable move - user-declared copy ctor prevents implicit creation
166 ObjectPath& operator = (const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
167 ObjectPath& operator = (ObjectPath&&) = default; // Enable move - user-declared copy assign prevents implicit creation
168 ObjectPath(std::string path)
169 : std::string(std::move(path))
170 {}
171 using std::string::operator=;
172 };
173
174 /********************************************/
180 class Signature : public std::string
181 {
182 public:
183 using std::string::string;
184 Signature() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
185 Signature(const Signature&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
186 Signature(Signature&&) = default; // Enable move - user-declared copy ctor prevents implicit creation
187 Signature& operator = (const Signature&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
188 Signature& operator = (Signature&&) = default; // Enable move - user-declared copy assign prevents implicit creation
189 Signature(std::string path)
190 : std::string(std::move(path))
191 {}
192 using std::string::operator=;
193 };
194
195 /********************************************/
206 class UnixFd
207 {
208 public:
209 UnixFd() = default;
210
211 explicit UnixFd(int fd)
212 : fd_(checkedDup(fd))
213 {
214 }
215
216 UnixFd(int fd, adopt_fd_t)
217 : fd_(fd)
218 {
219 }
220
221 UnixFd(const UnixFd& other)
222 {
223 *this = other;
224 }
225
226 UnixFd& operator=(const UnixFd& other)
227 {
228 if (this == &other)
229 {
230 return *this;
231 }
232 close();
233 fd_ = checkedDup(other.fd_);
234 return *this;
235 }
236
237 UnixFd(UnixFd&& other)
238 {
239 *this = std::move(other);
240 }
241
242 UnixFd& operator=(UnixFd&& other)
243 {
244 if (this == &other)
245 {
246 return *this;
247 }
248 close();
249 fd_ = std::exchange(other.fd_, -1);
250 return *this;
251 }
252
253 ~UnixFd()
254 {
255 close();
256 }
257
258 int get() const
259 {
260 return fd_;
261 }
262
263 void reset(int fd = -1)
264 {
265 *this = UnixFd{fd};
266 }
267
268 void reset(int fd, adopt_fd_t)
269 {
270 *this = UnixFd{fd, adopt_fd};
271 }
272
273 int release()
274 {
275 return std::exchange(fd_, -1);
276 }
277
278 bool isValid() const
279 {
280 return fd_ >= 0;
281 }
282
283 private:
285 void close();
286
289 static int checkedDup(int fd);
290
291 int fd_ = -1;
292 };
293
294}
295
296template <size_t _I, typename... _ValueTypes>
297struct std::tuple_element<_I, sdbus::Struct<_ValueTypes...>>
298 : std::tuple_element<_I, std::tuple<_ValueTypes...>>
299{};
300
301template <typename... _ValueTypes>
302struct std::tuple_size<sdbus::Struct<_ValueTypes...>>
303 : std::tuple_size<std::tuple<_ValueTypes...>>
304{};
305
306#endif /* SDBUS_CXX_TYPES_H_ */
Definition Message.h:77
Definition Types.h:160
Definition Message.h:301
Definition Types.h:181
Definition Types.h:115
Definition Types.h:207
Definition Types.h:54
Definition TypeTraits.h:87
Definition TypeTraits.h:103