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