sdbus-c++  1.0.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 <unistd.h>
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 _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  /********************************************/
108  template <typename... _ValueTypes>
109  class Struct
110  : public std::tuple<_ValueTypes...>
111  {
112  public:
113  using std::tuple<_ValueTypes...>::tuple;
114 
115  // Disable constructor if an older then 7.1.0 version of GCC is used
116 #if !((defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__) && !(__GNUC__ > 7 || (__GNUC__ == 7 && (__GNUC_MINOR__ > 1 || (__GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ > 0)))))
117  Struct() = default;
118 
119  explicit Struct(const std::tuple<_ValueTypes...>& t)
120  : std::tuple<_ValueTypes...>(t)
121  {
122  }
123 #endif
124 
125  template <std::size_t _I>
126  auto& get()
127  {
128  return std::get<_I>(*this);
129  }
130 
131  template <std::size_t _I>
132  const auto& get() const
133  {
134  return std::get<_I>(*this);
135  }
136  };
137 
138  template<typename... _Elements>
139  constexpr Struct<std::decay_t<_Elements>...>
140  make_struct(_Elements&&... args)
141  {
142  typedef Struct<std::decay_t<_Elements>...> result_type;
143  return result_type(std::forward<_Elements>(args)...);
144  }
145 
146  /********************************************/
152  class ObjectPath : public std::string
153  {
154  public:
155  using std::string::string;
156  ObjectPath() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
157  ObjectPath(const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
158  ObjectPath& operator = (const ObjectPath&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
159  ObjectPath(std::string path)
160  : std::string(std::move(path))
161  {}
162  using std::string::operator=;
163  };
164 
165  /********************************************/
171  class Signature : public std::string
172  {
173  public:
174  using std::string::string;
175  Signature() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
176  Signature(const Signature&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
177  Signature& operator = (const Signature&) = default; // Fixes gcc 8.3 error (deleted copy assignment)
178  Signature(std::string path)
179  : std::string(std::move(path))
180  {}
181  using std::string::operator=;
182  };
183 
184  struct adopt_fd_t { explicit adopt_fd_t() = default; };
185  inline constexpr adopt_fd_t adopt_fd{};
186 
187  /********************************************/
198  class UnixFd
199  {
200  public:
201  UnixFd() = default;
202 
203  explicit UnixFd(int fd)
204  : fd_(::dup(fd))
205  {
206  }
207 
208  UnixFd(int fd, adopt_fd_t)
209  : fd_(fd)
210  {
211  }
212 
213  UnixFd(const UnixFd& other)
214  {
215  *this = other;
216  }
217 
218  UnixFd& operator=(const UnixFd& other)
219  {
220  close();
221  fd_ = ::dup(other.fd_);
222  return *this;
223  }
224 
225  UnixFd(UnixFd&& other)
226  {
227  *this = std::move(other);
228  }
229 
230  UnixFd& operator=(UnixFd&& other)
231  {
232  close();
233  fd_ = other.fd_;
234  other.fd_ = -1;
235  return *this;
236  }
237 
238  ~UnixFd()
239  {
240  close();
241  }
242 
243  int get() const
244  {
245  return fd_;
246  }
247 
248  void reset(int fd = -1)
249  {
250  *this = UnixFd{fd};
251  }
252 
253  void reset(int fd, adopt_fd_t)
254  {
255  *this = UnixFd{fd, adopt_fd};
256  }
257 
258  int release()
259  {
260  auto fd = fd_;
261  fd_ = -1;
262  return fd;
263  }
264 
265  bool isValid() const
266  {
267  return fd_ >= 0;
268  }
269 
270  private:
271  void close()
272  {
273  if (fd_ >= 0)
274  ::close(fd_);
275  }
276 
277  int fd_ = -1;
278  };
279 
280 }
281 
282 #endif /* SDBUS_CXX_TYPES_H_ */
Definition: Types.h:184
Definition: TypeTraits.h:63
Definition: Message.h:75
Definition: Types.h:53
Definition: Types.h:152
Definition: Message.h:47
Definition: Types.h:198
Definition: Types.h:171
Definition: AdaptorInterfaces.h:36
Definition: Message.h:242