7 #ifndef _FCITX_UTILS_DBUS_OBJECTVTABLE_H_ 8 #define _FCITX_UTILS_DBUS_OBJECTVTABLE_H_ 17 #include <type_traits> 19 #include <fcitx-utils/fcitxutils_export.h> 21 #include <fcitx-utils/macros.h> 31 class ObjectVTableBase;
34 class ObjectVTablePrivate;
36 using ObjectMethod = std::function<bool(Message)>;
37 using ObjectMethodClosure = std::function<bool(Message, const ObjectMethod &)>;
38 using PropertyGetMethod = std::function<void(Message &)>;
39 using PropertySetMethod = std::function<bool(Message &)>;
55 : name_(name), error_(error) {}
57 const char *what()
const noexcept
override {
return error_.c_str(); }
59 const char *name()
const {
return name_.c_str(); }
76 const char *what()
const noexcept
override {
return "MethodCallNoReply"; }
83 const char *what()
const noexcept
override;
86 class ObjectVTableMethodPrivate;
98 const std::string &signature,
const std::string &ret,
99 ObjectMethod handler);
103 const std::string &name()
const;
104 const std::string &signature()
const;
105 const std::string &ret()
const;
106 const ObjectMethod &handler()
const;
117 void setClosureFunction(ObjectMethodClosure closure);
120 std::unique_ptr<ObjectVTableMethodPrivate> d_ptr;
124 template <
typename T>
129 template <
typename U>
137 using type = std::tuple<>;
139 template <
typename U>
157 #define FCITX_OBJECT_VTABLE_METHOD(FUNCTION, FUNCTION_NAME, SIGNATURE, RET) \ 158 ::fcitx::dbus::ObjectVTableMethod FUNCTION##Method { \ 159 this, FUNCTION_NAME, SIGNATURE, RET, \ 160 ::fcitx::dbus::makeObjectVTablePropertyObjectMethodAdaptor< \ 161 FCITX_STRING_TO_DBUS_TYPE(RET), \ 162 FCITX_STRING_TO_DBUS_TUPLE(SIGNATURE)>( \ 163 this, [this](auto &&...args) { \ 164 return this->FUNCTION( \ 165 std::forward<decltype(args)>(args)...); \ 182 #define FCITX_OBJECT_VTABLE_SIGNAL(SIGNAL, SIGNAL_NAME, SIGNATURE) \ 183 ::fcitx::dbus::ObjectVTableSignal SIGNAL##Signal{this, SIGNAL_NAME, \ 185 using SIGNAL##ArgType = FCITX_STRING_TO_DBUS_TUPLE(SIGNATURE); \ 186 template <typename... Args> \ 187 void SIGNAL(Args &&...args) { \ 188 auto msg = SIGNAL##Signal.createSignal(); \ 189 SIGNAL##ArgType tupleArg{std::forward<Args>(args)...}; \ 193 template <typename... Args> \ 194 void SIGNAL##To(const std::string &dest, Args &&...args) { \ 195 auto msg = SIGNAL##Signal.createSignal(); \ 196 msg.setDestination(dest); \ 197 SIGNAL##ArgType tupleArg{std::forward<Args>(args)...}; \ 212 #define FCITX_OBJECT_VTABLE_PROPERTY(PROPERTY, NAME, SIGNATURE, GETMETHOD, \ 214 ::fcitx::dbus::ObjectVTableProperty PROPERTY##Property{ \ 215 this, NAME, SIGNATURE, \ 216 ::fcitx::dbus::makeObjectVTablePropertyGetMethodAdaptor< \ 217 FCITX_STRING_TO_DBUS_TUPLE(SIGNATURE)>(this, GETMETHOD), \ 218 ::fcitx::dbus::PropertyOptions{__VA_ARGS__}}; 231 #define FCITX_OBJECT_VTABLE_WRITABLE_PROPERTY(PROPERTY, NAME, SIGNATURE, \ 232 GETMETHOD, SETMETHOD, ...) \ 233 ::fcitx::dbus::ObjectVTableWritableProperty PROPERTY##Property{ \ 237 ::fcitx::dbus::makeObjectVTablePropertyGetMethodAdaptor< \ 238 FCITX_STRING_TO_DBUS_TUPLE(SIGNATURE)>(this, GETMETHOD), \ 239 ::fcitx::dbus::makeObjectVTablePropertySetMethodAdaptor< \ 240 FCITX_STRING_TO_DBUS_TUPLE(SIGNATURE)>(this, SETMETHOD), \ 241 ::fcitx::dbus::PropertyOptions{__VA_ARGS__}}; 243 class ObjectVTableSignalPrivate;
255 std::string signature);
259 const std::string &name()
const;
260 const std::string &signature()
const;
263 std::unique_ptr<ObjectVTableSignalPrivate> d_ptr;
267 enum class PropertyOption : uint32_t { Hidden = (1 << 0) };
271 class ObjectVTablePropertyPrivate;
283 std::string signature, PropertyGetMethod getMethod,
287 const std::string &name()
const;
288 const std::string &signature()
const;
289 bool writable()
const;
290 const PropertyGetMethod &getMethod()
const;
296 std::unique_ptr<ObjectVTablePropertyPrivate> d_ptr;
311 std::string signature,
312 PropertyGetMethod getMethod,
313 PropertySetMethod setMethod,
316 const PropertySetMethod &setMethod()
const;
325 friend class MessageSetter;
348 bool isRegistered()
const;
350 const std::string &path()
const;
352 const std::string &interface()
const;
361 Message *currentMessage()
const;
371 void setCurrentMessage(
Message *message);
377 virtual std::mutex &privateDataMutexForType() = 0;
379 static std::shared_ptr<ObjectVTablePrivate> newSharedPrivateData();
382 void setSlot(
Slot *slot);
384 std::unique_ptr<ObjectVTableBasePrivate> d_ptr;
400 template <
typename T>
403 std::mutex &privateDataMutexForType()
override {
404 return privateDataMutex();
407 static std::mutex &privateDataMutex() {
408 static std::mutex mutex;
412 static std::shared_ptr<ObjectVTablePrivate> d(newSharedPrivateData());
417 template <
typename Ret,
typename Args,
typename Callback>
422 : base_(base), callback_(std::move(callback)) {}
424 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
428 base_->setCurrentMessage(&msg);
429 auto watcher = base_->watch();
433 using ReturnType = decltype(callWithTuple(callback_, args));
434 static_assert(std::is_same<Ret, ReturnType>::value,
435 "Return type does not match.");
438 [
this, &args]() {
return callWithTuple(callback_, args); });
443 auto reply = msg.
createError(error.name(), error.what());
448 if (watcher.isValid()) {
449 watcher.get()->setCurrentMessage(
nullptr);
459 template <
typename Ret,
typename Args,
typename Callback>
461 Callback &&callback) {
463 base, std::forward<Callback>(callback));
466 template <
typename Ret,
typename Callback>
471 : base_(base), callback_(std::move(callback)) {}
473 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
476 void operator()(
Message &msg) {
477 Ret
property = callback_();
486 template <
typename Ret,
typename Callback>
488 Callback &&callback) {
490 base, std::forward<Callback>(callback));
493 template <
typename Ret,
typename Callback>
498 : base_(base), callback_(std::move(callback)) {}
500 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
503 bool operator()(
Message &msg) {
504 base_->setCurrentMessage(&msg);
505 auto watcher = base_->watch();
508 callWithTuple(callback_, args);
511 if (watcher.isValid()) {
512 watcher.get()->setCurrentMessage(
nullptr);
522 template <
typename Ret,
typename Callback>
524 Callback &&callback) {
526 base, std::forward<Callback>(callback));
531 #endif // _FCITX_UTILS_DBUS_OBJECTVTABLE_H_ bool send()
Send this message.
Basic DBus type of a DBus message.
Base class of any DBus object.
Register a DBus read-only property to current DBus VTable.
Message createError(const char *name, const char *message) const
Create a error reply to this message.
Virtual base class represent some internal registration of the bus.
Utitliy classes for statically tracking the life of a object.
A class that represents a connection to the Bus.
Message createReply() const
Create a reply to this message.
Register a DBus property to current DBus VTable.
An exception if you want message to return a DBus error.
Helper class to be used with TrackableObjectReference.
Register a DBus method to current DBus VTable.
Class provides bit flag support for Enum.
Helper template class to make easier to use type safe enum flags.
An exception to not reply a the D-Bus method call.
Register a DBus signal to current DBus VTable.