00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _KO_GENERIC_REGISTRY_H_
00022 #define _KO_GENERIC_REGISTRY_H_
00023
00024 #include "KoID.h"
00025 #include <QList>
00026 #include <map>
00027
00036 template<typename T>
00037 class KoGenericRegistry {
00038 protected:
00039 typedef std::map<KoID, T> storageMap;
00040 public:
00041 KoGenericRegistry() { }
00042 virtual ~KoGenericRegistry() { }
00043
00044 public:
00049 void add(T item)
00050 {
00051 m_storage.insert( typename storageMap::value_type( item->id(), item) );
00052 }
00058 void add(KoID id, T item)
00059 {
00060 m_storage.insert(typename storageMap::value_type(id, item));
00061 }
00066 T remove(const KoID& name)
00067 {
00068 T p = 0;
00069 typename storageMap::iterator it = m_storage.find(name);
00070 if (it != m_storage.end()) {
00071 m_storage.erase(it);
00072 p = it->second;
00073 }
00074 return p;
00075 }
00081 T remove(const QString& id)
00082 {
00083 return remove(KoID(id,""));
00084 }
00090 T get(const KoID& name) const
00091 {
00092 T p = T(0);
00093 typename storageMap::const_iterator it = m_storage.find(name);
00094 if (it != m_storage.end()) {
00095 p = it->second;
00096 }
00097 return p;
00098 }
00099
00104 T get(const QString& id) const
00105 {
00106 return get(KoID(id, ""));
00107 }
00108
00113 bool exists(const KoID& id) const
00114 {
00115 typename storageMap::const_iterator it = m_storage.find(id);
00116 return (it != m_storage.end());
00117 }
00118
00119 bool exists(const QString& id) const
00120 {
00121 return exists(KoID(id, ""));
00122 }
00129 bool search(const QString& t, KoID& result) const
00130 {
00131 for(typename storageMap::const_iterator it = m_storage.begin();
00132 it != m_storage.end(); ++it)
00133 {
00134 if(it->first.name() == t)
00135 {
00136 result = it->first;
00137 return true;
00138 }
00139 }
00140 return false;
00141 }
00142
00145 QList<KoID> listKeys() const
00146 {
00147 QList<KoID> list;
00148 typename storageMap::const_iterator it = m_storage.begin();
00149 typename storageMap::const_iterator endit = m_storage.end();
00150 while( it != endit )
00151 {
00152 list.append(it->first);
00153 ++it;
00154 }
00155 return list;
00156 }
00157
00162 QList<QString> keys() const {
00163 QList<QString> answer;
00164 typename storageMap::const_iterator it = m_storage.begin();
00165 typename storageMap::const_iterator endit = m_storage.end();
00166 while( it != endit )
00167 {
00168 answer.append(it->first.id());
00169 ++it;
00170 }
00171 return answer;
00172 }
00173
00174 protected:
00175 KoGenericRegistry(const KoGenericRegistry&) { }
00176 KoGenericRegistry operator=(const KoGenericRegistry&) { }
00177 storageMap m_storage;
00178 };
00179
00180 #endif