7 #ifndef _FCITX_UTILS_DBUS_MESSAGE_H_ 8 #define _FCITX_UTILS_DBUS_MESSAGE_H_ 17 #include <type_traits> 20 #include <fcitx-utils/dbus/message_details.h> 21 #include <fcitx-utils/fcitxutils_export.h> 23 #include <fcitx-utils/macros.h> 25 #include <fcitx-utils/tuplehelpers.h> 43 template <
typename... Args>
45 using tuple_type = std::tuple<Args...>;
49 template <
typename Element,
typename... Elements>
51 requires(
sizeof...(Elements) != 0 ||
52 !std::is_same_v<
typename std::decay_t<Element>,
DBusStruct>)
53 : data_(std::forward<Element>(ele),
54 std::forward<Elements>(elements)...) {}
58 std::is_nothrow_move_constructible<tuple_type>::value) =
default;
61 std::is_nothrow_move_assignable<tuple_type>::value) =
default;
63 explicit DBusStruct(
const tuple_type &other) : data_(std::forward(other)) {}
65 : data_(std::forward<tuple_type>(other)) {}
67 constexpr tuple_type &data() {
return data_; }
68 constexpr
const tuple_type &data()
const {
return data_; }
77 virtual std::shared_ptr<void> copy(
const void *)
const = 0;
78 virtual void serialize(
dbus::Message &msg,
const void *data)
const = 0;
80 virtual void deserialize(
dbus::Message &msg,
void *data)
const = 0;
81 virtual std::string signature()
const = 0;
84 template <
typename Value>
86 std::shared_ptr<void> copy(
const void *src)
const override {
88 auto *s =
static_cast<const Value *
>(src);
89 return std::make_shared<Value>(*s);
91 return std::make_shared<Value>();
93 void serialize(
dbus::Message &msg,
const void *data)
const override {
94 auto *s =
static_cast<const Value *
>(data);
97 void deserialize(
dbus::Message &msg,
void *data)
const override {
98 auto *s =
static_cast<Value *
>(data);
102 auto *s =
static_cast<const Value *
>(data);
105 std::string signature()
const override {
115 template <
typename Key,
typename Value>
124 DictEntry(
const Key &key,
const Value &value) : key_(key), value_(value) {}
126 constexpr
Key &key() {
return key_; }
127 constexpr
const Key &key()
const {
return key_; }
128 constexpr Value &value() {
return value_; }
129 constexpr
const Value &value()
const {
return value_; }
137 using MessageCallback = std::function<bool(Message &)>;
140 enum class MessageType {
153 ObjectPath(
const std::string &path = {}) : path_(path) {}
155 const std::string &path()
const {
return path_; }
166 Signature(
const std::string &sig = {}) : sig_(sig) {}
168 const std::string &sig()
const {
return sig_; }
180 : type_(t), content_(content) {}
182 Type type()
const {
return type_; }
183 const Signature &content()
const {
return content_; }
193 class MessagePrivate;
195 template <
typename Tuple, std::
size_t N>
197 static void marshall(
Message &msg,
const Tuple &t) {
199 msg << std::get<N - 1>(t);
201 static void unmarshall(
Message &msg, Tuple &t) {
203 msg >> std::get<N - 1>(t);
207 template <
typename Tuple>
209 static void marshall(
Message &msg,
const Tuple &t) {
210 msg << std::get<0>(t);
212 static void unmarshall(
Message &msg, Tuple &t) { msg >> std::get<0>(t); }
215 template <
typename Tuple>
217 static void marshall(
Message & ,
const Tuple & ) {}
218 static void unmarshall(
Message & , Tuple & ) {}
230 FCITX_DECLARE_VIRTUAL_DTOR_MOVE(
Message);
234 Message createError(
const char *name,
const char *message)
const;
237 MessageType type()
const;
240 bool isError()
const {
return type() == MessageType::Error; }
243 std::string destination()
const;
252 void setDestination(
const std::string &dest);
255 std::string sender()
const;
258 std::string member()
const;
261 std::string interface()
const;
264 std::string signature()
const;
273 std::string errorName()
const;
282 std::string errorMessage()
const;
285 std::string path()
const;
294 void *nativeHandle()
const;
305 std::unique_ptr<Slot> callAsync(uint64_t usec, MessageCallback callback);
311 operator bool()
const;
326 std::pair<char, std::string> peekType();
328 Message &operator<<(uint8_t v);
330 Message &operator<<(int16_t v);
331 Message &operator<<(uint16_t v);
332 Message &operator<<(int32_t v);
333 Message &operator<<(uint32_t v);
334 Message &operator<<(int64_t v);
335 Message &operator<<(uint64_t v);
337 Message &operator<<(
const std::string &s);
338 Message &operator<<(
const char *s);
344 Message &operator<<(
const ContainerEnd &c);
347 template <
typename K,
typename V>
348 Message &operator<<(const std::pair<K, V> &t) {
352 *this << std::get<0>(t);
356 *this << std::get<1>(t);
360 template <
typename... Args>
361 Message &operator<<(
const std::tuple<Args...> &t) {
366 template <
typename... Args>
371 if (*
this <<
Container(Container::Type::Struct,
374 sizeof...(Args)>::marshall(*
this, t.data());
376 *
this << ContainerEnd();
382 template <
typename Key,
typename Value>
383 Message &operator<<(const DictEntry<Key, Value> &t) {
387 if (*
this <<
Container(Container::Type::DictEntry,
398 *
this << ContainerEnd();
404 template <
typename T>
405 Message &operator<<(const std::vector<T> &t) {
406 using value_type = std::vector<T>;
409 if (*
this <<
Container(Container::Type::Array,
414 *
this << ContainerEnd();
419 Message &operator>>(uint8_t &v);
421 Message &operator>>(int16_t &v);
422 Message &operator>>(uint16_t &v);
423 Message &operator>>(int32_t &v);
424 Message &operator>>(uint32_t &v);
425 Message &operator>>(int64_t &v);
426 Message &operator>>(uint64_t &v);
427 Message &operator>>(
double &v);
428 Message &operator>>(std::string &s);
433 Message &operator>>(
const ContainerEnd &c);
436 template <
typename K,
typename V>
437 Message &operator>>(std::pair<K, V> &t) {
441 *
this >> std::get<0>(t);
445 *
this >> std::get<1>(t);
449 template <
typename... Args>
450 Message &operator>>(std::tuple<Args...> &t) {
455 template <
typename... Args>
458 using tuple_type =
typename value_type::tuple_type;
466 *
this >> ContainerEnd();
472 template <
typename Key,
typename Value>
477 if (*
this >>
Container(Container::Type::DictEntry,
488 *
this >> ContainerEnd();
494 template <
typename T>
495 Message &operator>>(std::vector<T> &t) {
496 using value_type = std::vector<T>;
510 *
this >> ContainerEnd();
516 std::unique_ptr<MessagePrivate> d_ptr;
517 FCITX_DECLARE_PRIVATE(
Message);
520 template <
typename K,
typename V>
523 builder <<
"(" << entry.key() <<
", " << entry.value() <<
")";
527 template <
typename... Args>
530 builder << st.data();
536 builder <<
"Signature(" << sig.sig() <<
")";
542 builder <<
"ObjectPath(" << path.path() <<
")";
550 template <std::size_t i,
typename... _Elements>
552 return std::get<i>(t.data());
555 template <std::size_t i,
typename... _Elements>
557 return std::get<i>(t.data());
560 template <
typename T,
typename... _Elements>
562 return std::get<T>(t.data());
565 template <
typename T,
typename... _Elements>
567 return std::get<T>(t.data());
571 #endif // _FCITX_UTILS_DBUS_MESSAGE_H_ Helper type for serialization, should not be used directly.
Class wrap around the unix fd.
Basic DBus type of a DBus message.
String like type object signature 'g'.
Virtual base class represent some internal registration of the bus.
A class that represents a connection to the Bus.
Helper type for serialization, should not be used directly.
Utility class to handle unix file decriptor.
Class to represent a signal. May be used like a functor.
A type to represent DBus dict entry.
Variant type to be used to box or unbox the dbus variant type.
String like type object path 'o'.
bool isError() const
Check if the message is error.
A type to represent DBus struct.
Base class that can be used for UI composition or graph.