sdbus-c++  0.8.3
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(std::string path)
159  : std::string(std::move(path))
160  {}
161  using std::string::operator=;
162  };
163 
164  /********************************************/
170  class Signature : public std::string
171  {
172  public:
173  using std::string::string;
174  Signature() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
175  Signature(const Signature&) = default; // Fixes gcc 8.3 error (deleted copy constructor)
176  Signature(std::string path)
177  : std::string(std::move(path))
178  {}
179  using std::string::operator=;
180  };
181 
182  struct adopt_fd_t { explicit adopt_fd_t() = default; };
183  inline constexpr adopt_fd_t adopt_fd{};
184 
185  /********************************************/
196  class UnixFd
197  {
198  public:
199  UnixFd() = default;
200 
201  explicit UnixFd(int fd)
202  : fd_(::dup(fd))
203  {
204  }
205 
206  UnixFd(int fd, adopt_fd_t)
207  : fd_(fd)
208  {
209  }
210 
211  UnixFd(const UnixFd& other)
212  {
213  *this = other;
214  }
215 
216  UnixFd& operator=(const UnixFd& other)
217  {
218  close();
219  fd_ = ::dup(other.fd_);
220  return *this;
221  }
222 
223  UnixFd(UnixFd&& other)
224  {
225  *this = std::move(other);
226  }
227 
228  UnixFd& operator=(UnixFd&& other)
229  {
230  close();
231  fd_ = other.fd_;
232  other.fd_ = -1;
233  return *this;
234  }
235 
236  ~UnixFd()
237  {
238  close();
239  }
240 
241  int get() const
242  {
243  return fd_;
244  }
245 
246  void reset(int fd = -1)
247  {
248  *this = UnixFd{fd};
249  }
250 
251  void reset(int fd, adopt_fd_t)
252  {
253  *this = UnixFd{fd, adopt_fd};
254  }
255 
256  int release()
257  {
258  auto fd = fd_;
259  fd_ = -1;
260  return fd;
261  }
262 
263  bool isValid() const
264  {
265  return fd_ >= 0;
266  }
267 
268  private:
269  void close()
270  {
271  if (fd_ >= 0)
272  ::close(fd_);
273  }
274 
275  int fd_ = -1;
276  };
277 
278 }
279 
280 #endif /* SDBUS_CXX_TYPES_H_ */
Definition: Message.h:75
Definition: Types.h:153
Definition: Message.h:231
Definition: Types.h:171
Definition: Types.h:111
Definition: Types.h:197
Definition: Types.h:54
Definition: Types.h:182
Definition: TypeTraits.h:64