krita

kis_generic_registry.h

00001 /*
00002  *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #ifndef _KIS_GENERIC_REGISTRY_H_
00020 #define _KIS_GENERIC_REGISTRY_H_
00021 
00022 #include <map>
00023 
00024 #include <qstring.h>
00025 #include <kdebug.h>
00026 
00027 #include <kis_id.h>
00028 
00038 template<typename _T>
00039 class KisGenericRegistry {
00040 protected:
00041     typedef std::map<KisID, _T> storageMap;
00042 public:
00043     KisGenericRegistry() { };
00044     virtual ~KisGenericRegistry() { };
00045 public:
00046 
00051     void add(_T item)
00052     {
00053         m_storage.insert( typename storageMap::value_type( item->id(), item) );
00054     }
00060     void add(KisID id, _T item)
00061     {
00062         m_storage.insert(typename storageMap::value_type(id, item));
00063     }
00068     _T remove(const KisID& name)
00069     {
00070         _T p = 0;
00071         typename storageMap::iterator it = m_storage.find(name);
00072         if (it != m_storage.end()) {
00073             m_storage.erase(it);
00074             p = it->second;
00075         }
00076         return p;
00077     }
00083     _T remove(const QString& id)
00084     {
00085         return remove(KisID(id,""));
00086     }
00092     _T get(const KisID& name) const
00093     {
00094         _T p = 0;
00095         typename storageMap::const_iterator it = m_storage.find(name);
00096         if (it != m_storage.end()) {
00097             p = it->second;
00098         }
00099         return p;
00100     }
00101 
00106     _T get(const QString& id) const
00107     {
00108         return get(KisID(id, ""));
00109     }
00110 
00115     bool exists(const KisID& id) const
00116     {
00117         typename storageMap::const_iterator it = m_storage.find(id);
00118         return (it != m_storage.end());
00119     }
00120 
00121     bool exists(const QString& id) const
00122     {
00123         return exists(KisID(id, ""));
00124     }
00131     bool search(const QString& t, KisID& result) const
00132     {
00133         for(typename storageMap::const_iterator it = m_storage.begin();
00134             it != m_storage.end(); ++it)
00135         {
00136             if(it->first.name() == t)
00137             {
00138                 result = it->first;
00139                 return true;
00140             }
00141         }
00142         return false;
00143     }
00144 
00147     KisIDList listKeys() const
00148     {
00149         KisIDList list;
00150         typename storageMap::const_iterator it = m_storage.begin();
00151         typename storageMap::const_iterator endit = m_storage.end();
00152         while( it != endit )
00153         {
00154             list.append(it->first);
00155             ++it;
00156         }
00157         return list;
00158     }
00159 
00160 protected:
00161     KisGenericRegistry(const KisGenericRegistry&) { };
00162     KisGenericRegistry operator=(const KisGenericRegistry&) { };
00163     storageMap m_storage;
00164 };
00165 
00166 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys