1 /* ============================================================ 2 * 3 * This file is a part of digiKam project 4 * https://www.digikam.org 5 * 6 * Date : 2004-06-15 7 * Description : Albums manager interface. 8 * 9 * Copyright (C) 2006-2021 by Gilles Caulier <caulier dot gilles at gmail dot com> 10 * Copyright (C) 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> 11 * 12 * This program is free software; you can redistribute it 13 * and/or modify it under the terms of the GNU General 14 * Public License as published by the Free Software Foundation; 15 * either version 2, or (at your option) 16 * any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * ============================================================ */ 24 25 #ifndef DIGIKAM_ALBUM_POINTER_H 26 #define DIGIKAM_ALBUM_POINTER_H 27 28 // Qt includes 29 30 #include <QList> 31 32 // Local includes 33 34 #include "album.h" 35 #include "albummanager.h" 36 37 namespace Digikam 38 { 39 40 /** 41 * You can use AlbumPointer to store a guarded pointer to Album 42 * or one of the subclasses (use template parameter). 43 * The pointer will be set to 0 when the album object is deleted. 44 */ 45 template <class T = Album> 46 47 class AlbumPointer 48 { 49 public: 50 AlbumPointer()51 AlbumPointer() 52 : album(nullptr) 53 { 54 } 55 56 // cppcheck-suppress noExplicitConstructor AlbumPointer(T * const a)57 AlbumPointer(T* const a) // krazy:exclude=explicit 58 : album(a) 59 { 60 AlbumManager::instance()->addGuardedPointer(album, &album); 61 } 62 63 // cppcheck-suppress noExplicitConstructor AlbumPointer(const AlbumPointer<T> & p)64 AlbumPointer(const AlbumPointer<T>& p) // krazy:exclude=explicit 65 : album(p.album) 66 { 67 AlbumManager::instance()->addGuardedPointer(album, &album); 68 } 69 ~AlbumPointer()70 ~AlbumPointer() 71 { 72 AlbumManager::instance()->removeGuardedPointer(album, &album); 73 } 74 75 AlbumPointer<T>& operator=(T* const a) 76 { 77 Album* const oldAlbum = album; 78 album = a; 79 AlbumManager::instance()->changeGuardedPointer(oldAlbum, album, &album); 80 81 return *this; 82 } 83 84 AlbumPointer<T>& operator=(const AlbumPointer<T>& p) 85 { 86 Album* const oldAlbum = album; 87 album = p.album; 88 AlbumManager::instance()->changeGuardedPointer(oldAlbum, album, &album); 89 90 return *this; 91 } 92 93 T* operator->() const 94 { 95 return static_cast<T*>(const_cast<Album*>(album)); 96 } 97 98 T& operator*() const 99 { 100 return *static_cast<T*>(const_cast<Album*>(album)); 101 } 102 103 operator T* () const 104 { 105 return static_cast<T*>(const_cast<Album*>(album)); 106 } 107 108 bool operator!() const 109 { 110 return !album; 111 } 112 113 private: 114 115 friend class AlbumManager; 116 Album* album; 117 }; 118 119 // ------------------------------------------------------------------------------------ 120 121 template <class T = Album> 122 123 class AlbumPointerList : public QList<AlbumPointer<T> > 124 { 125 public: 126 AlbumPointerList()127 AlbumPointerList() 128 { 129 } 130 AlbumPointerList(const AlbumPointerList<T> & list)131 explicit AlbumPointerList(const AlbumPointerList<T>& list) 132 : QList<AlbumPointer<T> >(list) 133 { 134 } 135 AlbumPointerList(const QList<T * > & list)136 explicit AlbumPointerList(const QList<T*>& list) 137 { 138 operator=(list); 139 } 140 141 // cppcheck-suppress operatorEqRetRefThis 142 AlbumPointerList<T>& operator=(const AlbumPointerList<T>& list) 143 { 144 return QList<AlbumPointer<T> >::operator=(list); 145 } 146 147 AlbumPointerList<T>& operator=(const QList<T*>& list) 148 { foreach(T * const t,list)149 foreach (T* const t, list) 150 { 151 this->append(AlbumPointer<T>(t)); 152 } 153 154 return *this; 155 } 156 }; 157 158 } // namespace Digikam 159 160 Q_DECLARE_METATYPE(Digikam::AlbumPointer<>) 161 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::PAlbum>) 162 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::TAlbum>) 163 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::SAlbum>) 164 Q_DECLARE_METATYPE(Digikam::AlbumPointer<Digikam::DAlbum>) 165 Q_DECLARE_METATYPE(QList<Digikam::TAlbum*>) 166 167 #endif // DIGIKAM_ALBUM_POINTER_H 168