File indexing completed on 2024-11-24 04:42:24

0001 /*
0002  *  autoqpointer.h  -  QPointer which on destruction deletes object
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2009, 2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include <QPointer>
0012 
0013 
0014 /**
0015  *  Stores a pointer to a QObject, and ensures that when either it or the QObject is deleted,
0016  *  the 'right thing' happens. It is like a QScopedPointer holding a QPointer. It ensures:
0017  *  - When the QObject pointed to is deleted, the stored pointer is set to null;
0018  *  - When the AutoQPointer is deleted, its QObject is also deleted.
0019  *
0020  *  @author David Jarvie <djarvie@kde.org>
0021  */
0022 template <class T>
0023 class AutoQPointer : public QPointer<T>
0024 {
0025 public:
0026     AutoQPointer() = default;
0027     AutoQPointer(T* p) : QPointer<T>(p) {}                    //cppcheck-suppress noExplicitConstructor; Allow implicit conversion
0028     AutoQPointer(const QPointer<T>& p) : QPointer<T>(p) {}    //cppcheck-suppress noExplicitConstructor; Allow implicit conversion
0029     ~AutoQPointer()  { delete this->data(); }
0030     AutoQPointer<T>& operator=(T* p) { QPointer<T>::operator=(p); return *this; }
0031 
0032     AutoQPointer(const AutoQPointer<T>&) = delete;
0033     AutoQPointer<T>& operator=(const AutoQPointer<T>&) = delete;
0034 };
0035 
0036 // vim: et sw=4: