| 1 | /* |
| 2 | * qca_safeobj.h - Qt Cryptographic Architecture |
| 3 | * Copyright (C) 2008 Justin Karneges <justin@affinix.com> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "qca_safeobj.h" |
| 23 | |
| 24 | namespace QCA { |
| 25 | |
| 26 | // This function performs the following steps: |
| 27 | // obj->disconnect(owner); // to prevent future signals to owner |
| 28 | // obj->setParent(0); // to prevent delete if parent is deleted |
| 29 | // obj->deleteLater(); // now we can forget about the object |
| 30 | inline void releaseAndDeleteLater(QObject *owner, QObject *obj) |
| 31 | { |
| 32 | obj->disconnect(receiver: owner); |
| 33 | obj->setParent(nullptr); |
| 34 | obj->deleteLater(); |
| 35 | } |
| 36 | |
| 37 | SafeSocketNotifier::SafeSocketNotifier(qintptr socket, QSocketNotifier::Type type, QObject *parent) |
| 38 | : QObject(parent) |
| 39 | { |
| 40 | sn = new QSocketNotifier(socket, type, this); |
| 41 | connect(sender: sn, signal: &QSocketNotifier::activated, context: this, slot: &SafeSocketNotifier::activated); |
| 42 | } |
| 43 | |
| 44 | SafeSocketNotifier::~SafeSocketNotifier() |
| 45 | { |
| 46 | sn->setEnabled(false); |
| 47 | releaseAndDeleteLater(owner: this, obj: sn); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |