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