qtdbushelper.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2021 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. #ifndef QTDBUSHELPER_H
  4. #define QTDBUSHELPER_H
  5. #include <QtDBus/qdbusmessage.h>
  6. #include <QtDBus/qdbuspendingcall.h>
  7. #include <QtDBus/qdbusreply.h>
  8. QT_BEGIN_NAMESPACE
  9. namespace QtDBusHelper {
  10. // A Python-bindings friendly, non-template QDBusReply
  11. class QDBusReply {
  12. public:
  13. QDBusReply();
  14. // Enable constructing QDBusReply from a QDBusMessage which is returned by
  15. // call().
  16. explicit QDBusReply(const QDBusMessage &reply) :
  17. m_error(reply),
  18. m_data(reply.arguments().value(0, {}))
  19. {
  20. }
  21. // Enable constructing QDBusReply from an original Qt QDBusReply for
  22. // the functions we declare (QDBusConnectionInterface::registeredServiceNames())
  23. template <class T>
  24. explicit QDBusReply(const ::QDBusReply<T> &qr) :
  25. m_error(qr.error()),
  26. m_data(QVariant(qr.value()))
  27. {
  28. }
  29. explicit QDBusReply(const ::QDBusReply<void> &qr) :
  30. m_error(qr.error())
  31. {
  32. }
  33. bool isValid() const { return !m_error.isValid(); }
  34. QVariant value() const
  35. {
  36. return m_data;
  37. }
  38. const QDBusError &error() const { return m_error; }
  39. private:
  40. QDBusError m_error;
  41. QVariant m_data;
  42. };
  43. inline QDBusReply::QDBusReply() = default;
  44. } // namespace QtDBusHelper
  45. QT_END_NAMESPACE
  46. #endif // QTDBUSHELPER_H