File indexing completed on 2024-05-05 17:04:28
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 /** 0029 * Base class for registry objects. 0030 * 0031 * Registered objects are owned by the registry. 0032 * 0033 * Items are mapped by QString as a unique Id. 0034 * 0035 * Exemple of use: 0036 * @code 0037 * class KoMyClassRegistry : public KoGenericRegistry<MyClass*> { 0038 * public: 0039 * static KoMyClassRegistry * instance(); 0040 * private: 0041 * static KoMyClassRegistry* s_instance; 0042 * }; 0043 * 0044 * KoMyClassRegistry *KoMyClassRegistry::s_instance = 0; 0045 * KoMyClassRegistry * KoMyClassRegistry::instance() 0046 * { 0047 * if(s_instance == 0) 0048 * { 0049 * s_instance = new KoMyClassRegistry; 0050 * } 0051 * return s_instance; 0052 * } 0053 * 0054 * @endcode 0055 */ 0056 template<typename T> 0057 class KoGenericRegistry 0058 { 0059 public: 0060 KoGenericRegistry() { } 0061 virtual ~KoGenericRegistry() { m_hash.clear(); } 0062 0063 public: 0064 /** 0065 * Add an object to the registry. If it is a QObject, make sure it isn't in the 0066 * QObject ownership hierarchy, since the registry itself is responsible for 0067 * deleting it. 0068 * 0069 * @param item the item to add (NOTE: T must have an QString id() const function) 0070 */ 0071 void add(T item) { 0072 Q_ASSERT( item ); 0073 QString id = item->id(); 0074 if(m_hash.contains(id)) { 0075 m_doubleEntries << value(id); 0076 remove(id); 0077 } 0078 m_hash.insert(id, item); 0079 } 0080 0081 /** 0082 * add an object to the registry 0083 * @param id the id of the object 0084 * @param item the item to add 0085 */ 0086 void add(const QString &id, T item) { 0087 Q_ASSERT( item ); 0088 if(m_hash.contains(id)) { 0089 m_doubleEntries << value(id); 0090 remove(id); 0091 } 0092 m_hash.insert(id, item); 0093 } 0094 0095 /** 0096 * This function removes an item from the registry 0097 */ 0098 void remove(const QString &id) { 0099 m_hash.remove(id); 0100 } 0101 0102 /** 0103 * Retrieve the object from the registry based on the unique 0104 * identifier string. 0105 * 0106 * @param id the id 0107 */ 0108 T get(const QString& id) const { 0109 return value(id); 0110 } 0111 0112 /** 0113 * @return if there is an object stored in the registry identified 0114 * by the id. 0115 * @param id the unique identifier string 0116 */ 0117 bool contains(const QString &id) const { 0118 return m_hash.contains(id); 0119 } 0120 0121 /** 0122 * Retrieve the object from the registry based on the unique identifier string 0123 * @param id the id 0124 */ 0125 const T value(const QString &id) const { 0126 return m_hash.value(id); 0127 } 0128 0129 /** 0130 * @return a list of all keys 0131 */ 0132 QList<QString> keys() const { 0133 return m_hash.keys(); 0134 } 0135 0136 int count() const { 0137 return m_hash.count(); 0138 } 0139 0140 QList<T> values() const { 0141 return m_hash.values(); 0142 } 0143 0144 QList<T> doubleEntries() const { 0145 return m_doubleEntries; 0146 } 0147 0148 private: 0149 0150 QList<T> m_doubleEntries; 0151 0152 private: 0153 0154 QHash<QString, T> m_hash; 0155 }; 0156 0157 #endif