Warning, file /graphics/krita/interfaces/KoGenericRegistry.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  *  SPDX-FileCopyrightText: 2004 Cyrille Berger <cberger@cberger.net>
0003  *  SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-or-later
0006  */
0007 
0008 #ifndef _KO_GENERIC_REGISTRY_H_
0009 #define _KO_GENERIC_REGISTRY_H_
0010 
0011 #include <QList>
0012 #include <QString>
0013 #include <QHash>
0014 
0015 #include "kis_assert.h"
0016 
0017 /**
0018  * Base class for registry objects.
0019  *
0020  * Registered objects are owned by the registry.
0021  *
0022  * Items are mapped by QString as a unique Id.
0023  *
0024  * Example of use:
0025  * @code
0026  * class KoMyClassRegistry : public KoGenericRegistry<MyClass*> {
0027  * public:
0028  *   static KoMyClassRegistry * instance();
0029  * private:
0030  *  static KoMyClassRegistry* s_instance;
0031  * };
0032  *
0033  * KoMyClassRegistry *KoMyClassRegistry::s_instance = 0;
0034  * KoMyClassRegistry * KoMyClassRegistry::instance()
0035  * {
0036  *    if(s_instance == 0)
0037  *    {
0038  *      s_instance = new KoMyClassRegistry;
0039  *    }
0040  *    return s_instance;
0041  * }
0042  *
0043  * @endcode
0044  */
0045 template<typename T>
0046 class KoGenericRegistry
0047 {
0048 public:
0049     KoGenericRegistry() { }
0050     virtual ~KoGenericRegistry()
0051     {
0052         m_doubleEntries.clear();
0053         m_hash.clear();
0054     }
0055 
0056 public:
0057     /**
0058      * Add an object to the registry. If it is a QObject, make sure it isn't in the
0059      * QObject ownership hierarchy, since the registry itself is responsbile for
0060      * deleting it.
0061      *
0062      * @param item the item to add (NOTE: T must have an QString id() const   function)
0063      */
0064     void add(T item)
0065     {
0066         KIS_SAFE_ASSERT_RECOVER_RETURN(item);
0067 
0068         const QString id = item->id();
0069         KIS_SAFE_ASSERT_RECOVER_NOOP(!m_aliases.contains(id));
0070 
0071         if (m_hash.contains(id)) {
0072             m_doubleEntries << value(id);
0073             remove(id);
0074         }
0075         m_hash.insert(id, item);
0076     }
0077 
0078     /**
0079      * add an object to the registry
0080      * @param id the id of the object
0081      * @param item the item to add
0082      */
0083     void add(const QString &id, T item)
0084     {
0085         KIS_SAFE_ASSERT_RECOVER_RETURN(item);
0086         KIS_SAFE_ASSERT_RECOVER_NOOP(!m_aliases.contains(id));
0087 
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     {
0100         m_hash.remove(id);
0101     }
0102 
0103     void addAlias(const QString &alias, const QString &id)
0104     {
0105         KIS_SAFE_ASSERT_RECOVER_NOOP(!m_hash.contains(alias));
0106         m_aliases[alias] = id;
0107     }
0108 
0109     void removeAlias(const QString &alias)
0110     {
0111         m_aliases.remove(alias);
0112     }
0113 
0114     /**
0115      * Retrieve the object from the registry based on the unique
0116      * identifier string.
0117      *
0118      * @param id the id
0119      */
0120     T get(const QString &id) const
0121     {
0122         return value(id);
0123     }
0124 
0125     /**
0126      * @return if there is an object stored in the registry identified
0127      * by the id.
0128      * @param id the unique identifier string
0129      */
0130     bool contains(const QString &id) const
0131     {
0132         bool result = m_hash.contains(id);
0133 
0134         if (!result && m_aliases.contains(id)) {
0135             result = m_hash.contains(m_aliases.value(id));
0136         }
0137 
0138         return result;
0139     }
0140 
0141     /**
0142      * Retrieve the object from the registry based on the unique identifier string
0143      * @param id the id
0144      */
0145     const T value(const QString &id) const
0146     {
0147         T result = m_hash.value(id);
0148 
0149         if (!result && m_aliases.contains(id)) {
0150             result = m_hash.value(m_aliases.value(id));
0151         }
0152 
0153         return result;
0154     }
0155 
0156     /**
0157      * @return a list of all keys
0158      */
0159     QList<QString> keys() const
0160     {
0161         return m_hash.keys();
0162     }
0163 
0164     int count() const
0165     {
0166         return m_hash.count();
0167     }
0168 
0169     QList<T> values() const
0170     {
0171         return m_hash.values();
0172     }
0173 
0174     QList<T> doubleEntries() const
0175     {
0176         return m_doubleEntries;
0177     }
0178 
0179     typename QHash<QString, T>::const_iterator constBegin() const {
0180         return m_hash.constBegin();
0181     }
0182 
0183     typename QHash<QString, T>::const_iterator constEnd() const {
0184         return m_hash.constEnd();
0185     }
0186 
0187 private:
0188 
0189     QList<T> m_doubleEntries;
0190 
0191 private:
0192 
0193     QHash<QString, T> m_hash;
0194     QHash<QString, QString> m_aliases;
0195 };
0196 
0197 #endif