File indexing completed on 2025-01-05 03:53:48

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-06-15
0007  * Description : Albums manager interface.
0008  *
0009  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_ALBUM_POINTER_H
0017 #define DIGIKAM_ALBUM_POINTER_H
0018 
0019 // Qt includes
0020 
0021 #include <QList>
0022 
0023 // Local includes
0024 
0025 #include "album.h"
0026 #include "albummanager.h"
0027 
0028 namespace Digikam
0029 {
0030 
0031 /**
0032  * You can use AlbumPointer to store a guarded pointer to Album
0033  * or one of the subclasses (use template parameter).
0034  * The pointer will be set to 0 when the album object is deleted.
0035  */
0036 template <class T = Album>
0037 
0038 class AlbumPointer
0039 {
0040 public:
0041 
0042     AlbumPointer()
0043         : album(nullptr)
0044     {
0045     }
0046 
0047     // cppcheck-suppress noExplicitConstructor
0048     AlbumPointer(T* const a)                    // krazy:exclude=explicit
0049         : album(a)
0050     {
0051         AlbumManager::instance()->addGuardedPointer(album, &album);
0052     }
0053 
0054     // cppcheck-suppress noExplicitConstructor
0055     AlbumPointer(const AlbumPointer<T>& p)      // krazy:exclude=explicit
0056         : album(p.album)
0057     {
0058         AlbumManager::instance()->addGuardedPointer(album, &album);
0059     }
0060 
0061     ~AlbumPointer()
0062     {
0063         AlbumManager::instance()->removeGuardedPointer(album, &album);
0064     }
0065 
0066     AlbumPointer<T>& operator=(T* const a)
0067     {
0068         Album* const oldAlbum = album;
0069         album                 = a;
0070         AlbumManager::instance()->changeGuardedPointer(oldAlbum, album, &album);
0071 
0072         return *this;
0073     }
0074 
0075     AlbumPointer<T>& operator=(const AlbumPointer<T>& p)
0076     {
0077         Album* const oldAlbum = album;
0078         album                 = p.album;
0079         AlbumManager::instance()->changeGuardedPointer(oldAlbum, album, &album);
0080 
0081         return *this;
0082     }
0083 
0084     T* operator->() const
0085     {
0086         return static_cast<T*>(const_cast<Album*>(album));
0087     }
0088 
0089     T& operator*() const
0090     {
0091         return *static_cast<T*>(const_cast<Album*>(album));
0092     }
0093 
0094     operator T* () const
0095     {
0096         return static_cast<T*>(const_cast<Album*>(album));
0097     }
0098 
0099     bool operator!() const
0100     {
0101         return !album;
0102     }
0103 
0104 private:
0105 
0106     friend class AlbumManager;
0107     Album* album;
0108 };
0109 
0110 // ------------------------------------------------------------------------------------
0111 
0112 template <class T = Album>
0113 
0114 class AlbumPointerList : public QList<AlbumPointer<T> >
0115 {
0116 public:
0117 
0118     AlbumPointerList()
0119     {
0120     }
0121 
0122     explicit AlbumPointerList(const AlbumPointerList<T>& list)
0123         : QList<AlbumPointer<T> >(list)
0124     {
0125     }
0126 
0127     explicit AlbumPointerList(const QList<T*>& list)
0128     {
0129         operator=(list);
0130     }
0131 
0132     // cppcheck-suppress operatorEqRetRefThis
0133     AlbumPointerList<T>& operator=(const AlbumPointerList<T>& list)
0134     {
0135         return QList<AlbumPointer<T> >::operator=(list);
0136     }
0137 
0138     AlbumPointerList<T>& operator=(const QList<T*>& list)
0139     {
0140         Q_FOREACH (T* const t, list)
0141         {
0142             this->append(AlbumPointer<T>(t));
0143         }
0144 
0145         return *this;
0146     }
0147 };
0148 
0149 } // namespace Digikam
0150 
0151 Q_DECLARE_METATYPE(Digikam::AlbumPointer<>)
0152 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::PAlbum>)
0153 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::TAlbum>)
0154 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::SAlbum>)
0155 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::DAlbum>)
0156 
0157 #endif // DIGIKAM_ALBUM_POINTER_H