chalk

kis_generic_registry.h
1/*
2 * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#ifndef _KIS_GENERIC_REGISTRY_H_
20#define _KIS_GENERIC_REGISTRY_H_
21
22#include <map>
23
24#include <tqstring.h>
25#include <kdebug.h>
26
27#include <kis_id.h>
28
38template<typename _T>
39class KisGenericRegistry {
40protected:
41 typedef std::map<KisID, _T> storageMap;
42public:
43 KisGenericRegistry() { };
44 virtual ~KisGenericRegistry() { };
45public:
46
51 void add(_T item)
52 {
53 m_storage.insert( typename storageMap::value_type( item->id(), item) );
54 }
55
60 void add(KisID id, _T item)
61 {
62 m_storage.insert(typename storageMap::value_type(id, item));
63 }
64
68 _T remove(const KisID& name)
69 {
70 _T p = 0;
71 typename storageMap::iterator it = m_storage.find(name);
72 if (it != m_storage.end()) {
73 m_storage.erase(it);
74 p = it->second;
75 }
76 return p;
77 }
78
83 _T remove(const TQString& id)
84 {
85 return remove(KisID(id,""));
86 }
87
92 _T get(const KisID& name) const
93 {
94 _T p = 0;
95 typename storageMap::const_iterator it = m_storage.find(name);
96 if (it != m_storage.end()) {
97 p = it->second;
98 }
99 return p;
100 }
101
106 _T get(const TQString& id) const
107 {
108 return get(KisID(id, ""));
109 }
110
115 bool exists(const KisID& id) const
116 {
117 typename storageMap::const_iterator it = m_storage.find(id);
118 return (it != m_storage.end());
119 }
120
121 bool exists(const TQString& id) const
122 {
123 return exists(KisID(id, ""));
124 }
131 bool search(const TQString& t, KisID& result) const
132 {
133 for(typename storageMap::const_iterator it = m_storage.begin();
134 it != m_storage.end(); ++it)
135 {
136 if(it->first.name() == t)
137 {
138 result = it->first;
139 return true;
140 }
141 }
142 return false;
143 }
144
147 KisIDList listKeys() const
148 {
149 KisIDList list;
150 typename storageMap::const_iterator it = m_storage.begin();
151 typename storageMap::const_iterator endit = m_storage.end();
152 while( it != endit )
153 {
154 list.append(it->first);
155 ++it;
156 }
157 return list;
158 }
159
160protected:
162 KisGenericRegistry operator=(const KisGenericRegistry&) { };
163 storageMap m_storage;
164};
165
166#endif
Base class for registry objects in Chalk.
void add(KisID id, _T item)
add an object to the registry
_T remove(const KisID &name)
This function remove an item from the registry.
KisIDList listKeys() const
This function return a list of all the keys.
bool search(const TQString &t, KisID &result) const
This function allow to search a KisID from the name.
bool exists(const KisID &id) const
_T get(const KisID &name) const
This function allow to get an object from its KisID.
_T get(const TQString &id) const
Get a single entry based on the identifying part of KisID, not the the descriptive part.
void add(_T item)
add an object to the registry
_T remove(const TQString &id)
This function remove an item from the registry.
Chalk has a large number of extensible resources.
Definition kis_id.h:57