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 9 #include "base/algorithm.h" 10 11 #include <QtCore/QObject> 12 13 namespace base { 14 15 class qt_connection final { 16 public: _data(data)17 qt_connection(QMetaObject::Connection data = {}) : _data(data) { 18 } qt_connection(qt_connection && other)19 qt_connection(qt_connection &&other) : _data(base::take(other._data)) { 20 } 21 qt_connection &operator=(qt_connection &&other) { 22 reset(base::take(other._data)); 23 return *this; 24 } ~qt_connection()25 ~qt_connection() { 26 disconnect(); 27 } 28 release()29 void release() { 30 _data = QMetaObject::Connection(); 31 } 32 void reset(QMetaObject::Connection data = {}) { 33 disconnect(); 34 _data = data; 35 } 36 37 private: disconnect()38 void disconnect() { 39 if (_data) { 40 QObject::disconnect(base::take(_data)); 41 } 42 } 43 44 QMetaObject::Connection _data; 45 46 }; 47 48 } // namespace base 49