File indexing completed on 2024-05-19 05:47:33
0001 /* This file is part of the KDE project 0002 * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net> 0003 * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org> 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Lesser General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2.1 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Lesser General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #ifndef _KO_GENERIC_REGISTRY_H_ 0022 #define _KO_GENERIC_REGISTRY_H_ 0023 0024 #include <QList> 0025 #include <QString> 0026 #include <QHash> 0027 0028 #include "kis_assert.h" 0029 0030 /** 0031 * Base class for registry objects. 0032 * 0033 * Registered objects are owned by the registry. 0034 * 0035 * Items are mapped by QString as a unique Id. 0036 * 0037 * Example of use: 0038 * @code 0039 * class KoMyClassRegistry : public KoGenericRegistry<MyClass*> { 0040 * public: 0041 * static KoMyClassRegistry * instance(); 0042 * private: 0043 * static KoMyClassRegistry* s_instance; 0044 * }; 0045 * 0046 * KoMyClassRegistry *KoMyClassRegistry::s_instance = 0; 0047 * KoMyClassRegistry * KoMyClassRegistry::instance() 0048 * { 0049 * if(s_instance == 0) 0050 * { 0051 * s_instance = new KoMyClassRegistry; 0052 * } 0053 * return s_instance; 0054 * } 0055 * 0056 * @endcode 0057 */ 0058 template<typename T> 0059 class KoGenericRegistry 0060 { 0061 public: 0062 KoGenericRegistry() { } 0063 virtual ~KoGenericRegistry() 0064 { 0065 m_hash.clear(); 0066 } 0067 0068 public: 0069 /** 0070 * Add an object to the registry. If it is a QObject, make sure it isn't in the 0071 * QObject ownership hierarchy, since the registry itself is responsbile for 0072 * deleting it. 0073 * 0074 * @param item the item to add (NOTE: T must have an QString id() const function) 0075 */ 0076 void add(T item) 0077 { 0078 KIS_SAFE_ASSERT_RECOVER_RETURN(item); 0079 0080 const QString id = item->id(); 0081 KIS_SAFE_ASSERT_RECOVER_NOOP(!m_aliases.contains(id)); 0082 0083 if (m_hash.contains(id)) { 0084 m_doubleEntries << value(id); 0085 remove(id); 0086 } 0087 m_hash.insert(id, item); 0088 } 0089 0090 /** 0091 * add an object to the registry 0092 * @param id the id of the object 0093 * @param item the item to add 0094 */ 0095 void add(const QString &id, T item) 0096 { 0097 KIS_SAFE_ASSERT_RECOVER_RETURN(item); 0098 KIS_SAFE_ASSERT_RECOVER_NOOP(!m_aliases.contains(id)); 0099 0100 if (m_hash.contains(id)) { 0101 m_doubleEntries << value(id); 0102 remove(id); 0103 } 0104 m_hash.insert(id, item); 0105 } 0106 0107 /** 0108 * This function removes an item from the registry 0109 */ 0110 void remove(const QString &id) 0111 { 0112 m_hash.remove(id); 0113 } 0114 0115 void addAlias(const QString &alias, const QString &id) 0116 { 0117 KIS_SAFE_ASSERT_RECOVER_NOOP(!m_hash.contains(alias)); 0118 m_aliases[alias] = id; 0119 } 0120 0121 void removeAlias(const QString &alias) 0122 { 0123 m_aliases.remove(alias); 0124 } 0125 0126 /** 0127 * Retrieve the object from the registry based on the unique 0128 * identifier string. 0129 * 0130 * @param id the id 0131 */ 0132 T get(const QString &id) const 0133 { 0134 return value(id); 0135 } 0136 0137 /** 0138 * @return if there is an object stored in the registry identified 0139 * by the id. 0140 * @param id the unique identifier string 0141 */ 0142 bool contains(const QString &id) const 0143 { 0144 bool result = m_hash.contains(id); 0145 0146 if (!result && m_aliases.contains(id)) { 0147 result = m_hash.contains(m_aliases.value(id)); 0148 } 0149 0150 return result; 0151 } 0152 0153 /** 0154 * Retrieve the object from the registry based on the unique identifier string 0155 * @param id the id 0156 */ 0157 const T value(const QString &id) const 0158 { 0159 T result = m_hash.value(id); 0160 0161 if (!result && m_aliases.contains(id)) { 0162 result = m_hash.value(m_aliases.value(id)); 0163 } 0164 0165 return result; 0166 } 0167 0168 /** 0169 * @return a list of all keys 0170 */ 0171 QList<QString> keys() const 0172 { 0173 return m_hash.keys(); 0174 } 0175 0176 int count() const 0177 { 0178 return m_hash.count(); 0179 } 0180 0181 QList<T> values() const 0182 { 0183 return m_hash.values(); 0184 } 0185 0186 QList<T> doubleEntries() const 0187 { 0188 return m_doubleEntries; 0189 } 0190 0191 private: 0192 0193 QList<T> m_doubleEntries; 0194 0195 private: 0196 0197 QHash<QString, T> m_hash; 0198 QHash<QString, QString> m_aliases; 0199 }; 0200 0201 #endif