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;
327 uint64_t replySerial();
330 std::pair<char, std::string> peekType();
332 Message &operator<<(uint8_t v);
334 Message &operator<<(int16_t v);
335 Message &operator<<(uint16_t v);
336 Message &operator<<(int32_t v);
337 Message &operator<<(uint32_t v);
338 Message &operator<<(int64_t v);
339 Message &operator<<(uint64_t v);
341 Message &operator<<(
const std::string &s);
342 Message &operator<<(
const char *s);
348 Message &operator<<(
const ContainerEnd &c);
351 template <
typename K,
typename V>
352 Message &operator<<(const std::pair<K, V> &t) {
356 *this << std::get<0>(t);
360 *this << std::get<1>(t);
364 template <
typename... Args>
365 Message &operator<<(
const std::tuple<Args...> &t) {
370 template <
typename... Args>
375 if (*
this <<
Container(Container::Type::Struct,
378 sizeof...(Args)>::marshall(*
this, t.data());
380 *
this << ContainerEnd();
386 template <
typename Key,
typename Value>
387 Message &operator<<(const DictEntry<Key, Value> &t) {
391 if (*
this <<
Container(Container::Type::DictEntry,
402 *
this << ContainerEnd();
408 template <
typename T>
409 Message &operator<<(const std::vector<T> &t) {
410 using value_type = std::vector<T>;
413 if (*
this <<
Container(Container::Type::Array,
418 *
this << ContainerEnd();
423 Message &operator>>(uint8_t &v);
425 Message &operator>>(int16_t &v);
426 Message &operator>>(uint16_t &v);
427 Message &operator>>(int32_t &v);
428 Message &operator>>(uint32_t &v);
429 Message &operator>>(int64_t &v);
430 Message &operator>>(uint64_t &v);
431 Message &operator>>(
double &v);
432 Message &operator>>(std::string &s);
437 Message &operator>>(
const ContainerEnd &c);
440 template <
typename K,
typename V>
441 Message &operator>>(std::pair<K, V> &t) {
445 *
this >> std::get<0>(t);
449 *
this >> std::get<1>(t);
453 template <
typename... Args>
454 Message &operator>>(std::tuple<Args...> &t) {
459 template <
typename... Args>
462 using tuple_type =
typename value_type::tuple_type;
470 *
this >> ContainerEnd();
476 template <
typename Key,
typename Value>
481 if (*
this >>
Container(Container::Type::DictEntry,
492 *
this >> ContainerEnd();
498 template <
typename T>
499 Message &operator>>(std::vector<T> &t) {
500 using value_type = std::vector<T>;
514 *
this >> ContainerEnd();
520 std::unique_ptr<MessagePrivate> d_ptr;
521 FCITX_DECLARE_PRIVATE(
Message);
524 template <
typename K,
typename V>
527 builder <<
"(" << entry.key() <<
", " << entry.value() <<
")";
531 template <
typename... Args>
534 builder << st.data();
540 builder <<
"Signature(" << sig.sig() <<
")";
546 builder <<
"ObjectPath(" << path.path() <<
")";
554 template <std::size_t i,
typename... _Elements>
556 return std::get<i>(t.data());
559 template <std::size_t i,
typename... _Elements>
561 return std::get<i>(t.data());
564 template <
typename T,
typename... _Elements>
566 return std::get<T>(t.data());
569 template <
typename T,
typename... _Elements>
571 return std::get<T>(t.data());
575 #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 descriptor.
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.