1/* This file is part of the KDE project
2 * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
3 * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef _KO_GENERIC_REGISTRY_H_
22#define _KO_GENERIC_REGISTRY_H_
23
24#include <kdemacros.h>
25#include <kdebug.h>
26#include <QList>
27#include <QString>
28#include <QHash>
29
30/**
31 * Base class for registry objects.
32 *
33 * Registered objects are owned by the registry.
34 *
35 * Items are mapped by QString as a unique Id.
36 *
37 * Exemple of use:
38 * @code
39 * class KoMyClassRegistry : public KoGenericRegistry<MyClass*> {
40 * public:
41 * static KoMyClassRegistry * instance();
42 * private:
43 * static KoMyClassRegistry* s_instance;
44 * };
45 *
46 * KoMyClassRegistry *KoMyClassRegistry::s_instance = 0;
47 * KoMyClassRegistry * KoMyClassRegistry::instance()
48 * {
49 * if(s_instance == 0)
50 * {
51 * s_instance = new KoMyClassRegistry;
52 * }
53 * return s_instance;
54 * }
55 *
56 * @endcode
57 */
58template<typename T>
59class KoGenericRegistry
60{
61public:
62 KoGenericRegistry() { }
63 virtual ~KoGenericRegistry() { m_hash.clear(); }
64
65public:
66 /**
67 * Add an object to the registry. If it is a QObject, make sure it isn't in the
68 * QObject ownership hierarchy, since the registry itself is responsbile for
69 * deleting it.
70 *
71 * @param item the item to add (NOTE: T must have an QString id() const function)
72 */
73 void add(T item) {
74 Q_ASSERT( item );
75 QString id = item->id();
76 if(m_hash.contains(id)) {
77 m_doubleEntries << value(id);
78 remove(id);
79 }
80 m_hash.insert(id, item);
81 }
82
83 /**
84 * add an object to the registry
85 * @param id the id of the object
86 * @param item the item to add
87 */
88 void add(const QString &id, T item) {
89 Q_ASSERT( item );
90 if(m_hash.contains(id)) {
91 m_doubleEntries << value(id);
92 remove(id);
93 }
94 m_hash.insert(id, item);
95 }
96
97 /**
98 * This function removes an item from the registry
99 */
100 void remove(const QString &id) {
101 m_hash.remove(id);
102 }
103
104 /**
105 * Retrieve the object from the registry based on the unique
106 * identifier string.
107 *
108 * @param id the id
109 */
110 T get(const QString& id) const {
111 return value(id);
112 }
113
114 /**
115 * @return if there is an object stored in the registry identified
116 * by the id.
117 * @param id the unique identifier string
118 */
119 bool contains(const QString &id) const {
120 return m_hash.contains(id);
121 }
122
123 /**
124 * Retrieve the object from the registry based on the unique identifier string
125 * @param id the id
126 */
127 const T value(const QString &id) const {
128 return m_hash.value(id);
129 }
130
131 /**
132 * @return a list of all keys
133 */
134 QList<QString> keys() const {
135 return m_hash.keys();
136 }
137
138 int count() const {
139 return m_hash.count();
140 }
141
142 QList<T> values() const {
143 return m_hash.values();
144 }
145
146 QList<T> doubleEntries() const {
147 return m_doubleEntries;
148 }
149
150private:
151
152 QList<T> m_doubleEntries;
153
154private:
155
156 QHash<QString, T> m_hash;
157};
158
159#endif
160