File indexing completed on 2024-05-12 13:00:15

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 /**
0016  * Base class for registry objects.
0017  *
0018  * Registered objects are owned by the registry.
0019  *
0020  * Items are mapped by QString as a unique Id.
0021  *
0022  * Example of use:
0023  * @code
0024  * class KoMyClassRegistry : public KoGenericRegistry<MyClass*> {
0025  * public:
0026  *   static KoMyClassRegistry * instance();
0027  * private:
0028  *  static KoMyClassRegistry* s_instance;
0029  * };
0030  *
0031  * KoMyClassRegistry *KoMyClassRegistry::s_instance = 0;
0032  * KoMyClassRegistry * KoMyClassRegistry::instance()
0033  * {
0034  *    if(s_instance == 0)
0035  *    {
0036  *      s_instance = new KoMyClassRegistry;
0037  *    }
0038  *    return s_instance;
0039  * }
0040  *
0041  * @endcode
0042  */
0043 template<typename T>
0044 class KoGenericRegistry
0045 {
0046 public:
0047     KoGenericRegistry() { }
0048     virtual ~KoGenericRegistry() { m_hash.clear(); }
0049 
0050 public:
0051     /**
0052      * Add an object to the registry. If it is a QObject, make sure it isn't in the
0053      * QObject ownership hierarchy, since the registry itself is responsible for
0054      * deleting it.
0055      *
0056      * @param item the item to add (NOTE: T must have an QString id() const   function)
0057      */
0058     void add(T item) {
0059         Q_ASSERT( item );
0060         QString id = item->id();
0061         if(m_hash.contains(id)) {
0062             m_doubleEntries << value(id);
0063             remove(id);
0064         }
0065         m_hash.insert(id, item);
0066     }
0067 
0068     /**
0069      * add an object to the registry
0070      * @param id the id of the object
0071      * @param item the item to add
0072      */
0073     void add(const QString &id, T item) {
0074         Q_ASSERT( item );
0075         if(m_hash.contains(id)) {
0076             m_doubleEntries << value(id);
0077             remove(id);
0078         }
0079         m_hash.insert(id, item);
0080     }
0081 
0082     /**
0083      * This function removes an item from the registry
0084      */
0085     void remove(const QString &id) {
0086         m_hash.remove(id);
0087     }
0088 
0089     /**
0090      * Retrieve the object from the registry based on the unique
0091      * identifier string.
0092      *
0093      * @param id the id
0094      */
0095     T get(const QString& id) const {
0096         return value(id);
0097     }
0098 
0099     /**
0100      * @return if there is an object stored in the registry identified
0101      * by the id.
0102      * @param id the unique identifier string
0103      */
0104     bool contains(const QString &id) const {
0105         return m_hash.contains(id);
0106     }
0107 
0108     /**
0109      * Retrieve the object from the registry based on the unique identifier string
0110      * @param id the id
0111      */
0112     const T value(const QString &id) const {
0113         return m_hash.value(id);
0114     }
0115 
0116     /**
0117      * @return a list of all keys
0118      */
0119     QList<QString> keys() const {
0120         return m_hash.keys();
0121     }
0122 
0123     int count() const {
0124         return m_hash.count();
0125     }
0126 
0127     QList<T> values() const {
0128         return m_hash.values();
0129     }
0130 
0131     QList<T> doubleEntries() const {
0132         return m_doubleEntries;
0133     }
0134 
0135 private:
0136 
0137     QList<T> m_doubleEntries;
0138 
0139 private:
0140 
0141     QHash<QString, T> m_hash;
0142 };
0143 
0144 #endif