sdbus-c++: include/sdbus-c++/Types.h Source File

sdbus-c++ 1.4.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>
31#include <sdbus-c++/TypeTraits.h>
32#include <string>
33#include <type_traits>
34#include <typeinfo>
35#include <memory>
36#include <tuple>
37#include <unistd.h>
38
39namespace sdbus {
40
41 /********************************************/
53 class Variant
54 {
55 public:
56 Variant();
57
58 template <typename _ValueType>
59 /*explicit*/ Variant(const _ValueType& value) // TODO: Mark explicit in new major version so we don't break client code within v1
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_(::dup(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 close();
229 fd_ = ::dup(other.fd_);
230 return *this;
231 }
232
233 UnixFd(UnixFd&& other)
234 {
235 *this = std::move(other);
236 }
237
238 UnixFd& operator=(UnixFd&& other)
239 {
240 close();
241 fd_ = other.fd_;
242 other.fd_ = -1;
243 return *this;
244 }
245
246 ~UnixFd()
247 {
248 close();
249 }
250
251 int get() const
252 {
253 return fd_;
254 }
255
256 void reset(int fd = -1)
257 {
258 *this = UnixFd{fd};
259 }
260
261 void reset(int fd, adopt_fd_t)
262 {
263 *this = UnixFd{fd, adopt_fd};
264 }
265
266 int release()
267 {
268 auto fd = fd_;
269 fd_ = -1;
270 return fd;
271 }
272
273 bool isValid() const
274 {
275 return fd_ >= 0;
276 }
277
278 private:
279 void close()
280 {
281 if (fd_ >= 0)
282 ::close(fd_);
283 }
284
285 int fd_ = -1;
286 };
287
288}
289
290template <size_t _I, typename... _ValueTypes>
291 struct std::tuple_element<_I, sdbus::Struct<_ValueTypes...>>
292 : std::tuple_element<_I, std::tuple<_ValueTypes...>>
293{};
294
295template <typename... _ValueTypes>
296 struct std::tuple_size<sdbus::Struct<_ValueTypes...>>
297 : std::tuple_size<std::tuple<_ValueTypes...>>
298{};
299
300#endif /* SDBUS_CXX_TYPES_H_ */
Message.h
TypeTraits.h
sdbus::Message
Definition Message.h:76
sdbus::ObjectPath
Definition Types.h:160
sdbus::PlainMessage
Definition Message.h:300
sdbus::Signature
Definition Types.h:181
sdbus::Struct
Definition Types.h:115
sdbus::UnixFd
Definition Types.h:207
sdbus::Variant
Definition Types.h:54
sdbus::adopt_fd_t
Definition TypeTraits.h:87
sdbus::signature_of
Definition TypeTraits.h:103