sdbus-c++  1.6.0
High-level C++ D-Bus library based on systemd D-Bus implementation
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 <utility>
38 
39 namespace 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... _Elements>
69  Variant(const std::variant<_Elements...>& value)
70  : Variant()
71  {
72  msg_ << value;
73  msg_.seal();
74  }
75 
76  template <typename _ValueType>
77  _ValueType get() const
78  {
79  _ValueType val;
80  msg_.rewind(false);
81  msg_.enterVariant(signature_of<_ValueType>::str());
82  msg_ >> val;
83  msg_.exitVariant();
84  return val;
85  }
86 
87  // Only allow conversion operator for true D-Bus type representations in C++
88  template <typename _ValueType, typename = std::enable_if_t<signature_of<_ValueType>::is_valid>>
89  operator _ValueType() const
90  {
91  return get<_ValueType>();
92  }
93 
94  template <typename... _Elements>
95  operator std::variant<_Elements...>() const
96  {
97  std::variant<_Elements...> result;
98  msg_.rewind(false);
99  msg_ >> result;
100  return result;
101  }
102 
103  template <typename _Type>
104  bool containsValueOfType() const
105  {
106  return signature_of<_Type>::str() == peekValueType();
107  }
108 
109  bool isEmpty() const;
110 
111  void serializeTo(Message& msg) const;
112  void deserializeFrom(Message& msg);
113  std::string peekValueType() const;
114 
115  private:
116  mutable PlainMessage msg_{};
117  };
118 
119  /********************************************/
129  template <typename... _ValueTypes>
130  class Struct
131  : public std::tuple<_ValueTypes...>
132  {
133  public:
134  using std::tuple<_ValueTypes...>::tuple;
135 
136  // Disable constructor if an older then 7.1.0 version of GCC is used
137 #if !((defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__) && !(__GNUC__ > 7 || (__GNUC__ == 7 && (__GNUC_MINOR__ > 1 || (__GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ > 0)))))
138  Struct() = default;
139 
140  explicit Struct(const std::tuple<_ValueTypes...>& t)
141  : std::tuple<_ValueTypes...>(t)
142  {
143  }
144 #endif
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  using std::string::string;
180  ObjectPath() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
181  ObjectPath(const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
182  ObjectPath(ObjectPath&&) = default; // Enable move - user-declared copy ctor prevents implicit creation
183  ObjectPath& operator = (const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
184  ObjectPath& operator = (ObjectPath&&) = default; // Enable move - user-declared copy assign prevents implicit creation
185  ObjectPath(std::string path)
186  : std::string(std::move(path))
187  {}
188  using std::string::operator=;
189  };
190 
191  /********************************************/
197  class Signature : public std::string
198  {
199  public:
200  using std::string::string;
201  Signature() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
202  Signature(const Signature&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
203  Signature(Signature&&) = default; // Enable move - user-declared copy ctor prevents implicit creation
204  Signature& operator = (const Signature&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
205  Signature& operator = (Signature&&) = default; // Enable move - user-declared copy assign prevents implicit creation
206  Signature(std::string path)
207  : std::string(std::move(path))
208  {}
209  using std::string::operator=;
210  };
211 
212  /********************************************/
223  class UnixFd
224  {
225  public:
226  UnixFd() = default;
227 
228  explicit UnixFd(int fd)
229  : fd_(checkedDup(fd))
230  {
231  }
232 
233  UnixFd(int fd, adopt_fd_t)
234  : fd_(fd)
235  {
236  }
237 
238  UnixFd(const UnixFd& other)
239  {
240  *this = other;
241  }
242 
243  UnixFd& operator=(const UnixFd& other)
244  {
245  if (this == &other)
246  {
247  return *this;
248  }
249  close();
250  fd_ = checkedDup(other.fd_);
251  return *this;
252  }
253 
254  UnixFd(UnixFd&& other)
255  {
256  *this = std::move(other);
257  }
258 
259  UnixFd& operator=(UnixFd&& other)
260  {
261  if (this == &other)
262  {
263  return *this;
264  }
265  close();
266  fd_ = std::exchange(other.fd_, -1);
267  return *this;
268  }
269 
270  ~UnixFd()
271  {
272  close();
273  }
274 
275  int get() const
276  {
277  return fd_;
278  }
279 
280  void reset(int fd = -1)
281  {
282  *this = UnixFd{fd};
283  }
284 
285  void reset(int fd, adopt_fd_t)
286  {
287  *this = UnixFd{fd, adopt_fd};
288  }
289 
290  int release()
291  {
292  return std::exchange(fd_, -1);
293  }
294 
295  bool isValid() const
296  {
297  return fd_ >= 0;
298  }
299 
300  private:
302  void close();
303 
306  static int checkedDup(int fd);
307 
308  int fd_ = -1;
309  };
310 
311 }
312 
313 template <size_t _I, typename... _ValueTypes>
314 struct std::tuple_element<_I, sdbus::Struct<_ValueTypes...>>
315  : std::tuple_element<_I, std::tuple<_ValueTypes...>>
316 {};
317 
318 template <typename... _ValueTypes>
319 struct std::tuple_size<sdbus::Struct<_ValueTypes...>>
320  : std::tuple_size<std::tuple<_ValueTypes...>>
321 {};
322 
323 #endif /* SDBUS_CXX_TYPES_H_ */
Definition: Message.h:79
Definition: Types.h:177
Definition: Message.h:310
Definition: Types.h:198
Definition: Types.h:132
Definition: Types.h:224
Definition: Types.h:54
Definition: TypeTraits.h:88
Definition: TypeTraits.h:107