qt_connection.h 948 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/algorithm.h"
  9. #include <QtCore/QObject>
  10. namespace base {
  11. class qt_connection final {
  12. public:
  13. qt_connection(QMetaObject::Connection data = {}) : _data(data) {
  14. }
  15. qt_connection(qt_connection &&other) : _data(base::take(other._data)) {
  16. }
  17. qt_connection &operator=(qt_connection &&other) {
  18. reset(base::take(other._data));
  19. return *this;
  20. }
  21. ~qt_connection() {
  22. disconnect();
  23. }
  24. void release() {
  25. _data = QMetaObject::Connection();
  26. }
  27. void reset(QMetaObject::Connection data = {}) {
  28. disconnect();
  29. _data = data;
  30. }
  31. private:
  32. void disconnect() {
  33. if (_data) {
  34. QObject::disconnect(base::take(_data));
  35. }
  36. }
  37. QMetaObject::Connection _data;
  38. };
  39. } // namespace base