KoGenericRegistry.h 4.63 KB
Newer Older
1 2 3 4 5 6 7
/* This file is part of the KDE project
 *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
 *  Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef _KO_GENERIC_REGISTRY_H_
#define _KO_GENERIC_REGISTRY_H_

24 25 26
#include <QList>
#include <QString>
#include <QHash>
27

28 29
#include "kis_assert.h"

30 31 32
/**
 * Base class for registry objects.
 *
33 34
 * Registered objects are owned by the registry.
 *
35 36
 * Items are mapped by QString as a unique Id.
 *
luz paz's avatar
luz paz committed
37
 * Example of use:
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * @code
 * class KoMyClassRegistry : public KoGenericRegistry<MyClass*> {
 * public:
 *   static KoMyClassRegistry * instance();
 * private:
 *  static KoMyClassRegistry* s_instance;
 * };
 *
 * KoMyClassRegistry *KoMyClassRegistry::s_instance = 0;
 * KoMyClassRegistry * KoMyClassRegistry::instance()
 * {
 *    if(s_instance == 0)
 *    {
 *      s_instance = new KoMyClassRegistry;
 *    }
 *    return s_instance;
 * }
 *
 * @endcode
 */
template<typename T>
class KoGenericRegistry
{
public:
    KoGenericRegistry() { }
63 64 65 66
    virtual ~KoGenericRegistry()
    {
        m_hash.clear();
    }
67 68 69

public:
    /**
70 71 72 73
     * Add an object to the registry. If it is a QObject, make sure it isn't in the
     * QObject ownership hierarchy, since the registry itself is responsbile for
     * deleting it.
     *
74 75
     * @param item the item to add (NOTE: T must have an QString id() const   function)
     */
76 77
    void add(T item)
    {
78 79 80 81 82
        KIS_SAFE_ASSERT_RECOVER_RETURN(item);

        const QString id = item->id();
        KIS_SAFE_ASSERT_RECOVER_NOOP(!m_aliases.contains(id));

83
        if (m_hash.contains(id)) {
84 85 86 87
            m_doubleEntries << value(id);
            remove(id);
        }
        m_hash.insert(id, item);
88 89 90 91 92 93 94
    }

    /**
     * add an object to the registry
     * @param id the id of the object
     * @param item the item to add
     */
95 96
    void add(const QString &id, T item)
    {
97 98 99
        KIS_SAFE_ASSERT_RECOVER_RETURN(item);
        KIS_SAFE_ASSERT_RECOVER_NOOP(!m_aliases.contains(id));

100
        if (m_hash.contains(id)) {
101 102 103
            m_doubleEntries << value(id);
            remove(id);
        }
104 105 106 107 108 109
        m_hash.insert(id, item);
    }

    /**
     * This function removes an item from the registry
     */
110 111
    void remove(const QString &id)
    {
112 113 114
        m_hash.remove(id);
    }

115 116 117 118 119 120 121 122 123 124 125
    void addAlias(const QString &alias, const QString &id)
    {
        KIS_SAFE_ASSERT_RECOVER_NOOP(!m_hash.contains(alias));
        m_aliases[alias] = id;
    }

    void removeAlias(const QString &alias)
    {
        m_aliases.remove(alias);
    }

126 127 128 129 130 131
    /**
     * Retrieve the object from the registry based on the unique
     * identifier string.
     *
     * @param id the id
     */
132 133
    T get(const QString &id) const
    {
134 135 136 137 138 139 140 141
        return value(id);
    }

    /**
     * @return if there is an object stored in the registry identified
     * by the id.
     * @param id the unique identifier string
     */
142 143
    bool contains(const QString &id) const
    {
144 145 146 147 148 149 150
        bool result = m_hash.contains(id);

        if (!result && m_aliases.contains(id)) {
            result = m_hash.contains(m_aliases.value(id));
        }

        return result;
151 152 153 154 155 156
    }

    /**
     * Retrieve the object from the registry based on the unique identifier string
     * @param id the id
     */
157 158
    const T value(const QString &id) const
    {
159 160 161 162 163 164 165
        T result = m_hash.value(id);

        if (!result && m_aliases.contains(id)) {
            result = m_hash.value(m_aliases.value(id));
        }

        return result;
166 167 168 169 170
    }

    /**
     * @return a list of all keys
     */
171 172
    QList<QString> keys() const
    {
173 174 175
        return m_hash.keys();
    }

176 177
    int count() const
    {
178 179 180
        return m_hash.count();
    }

181 182
    QList<T> values() const
    {
183 184 185
        return m_hash.values();
    }

186 187
    QList<T> doubleEntries() const
    {
188 189 190
        return m_doubleEntries;
    }

191
private:
192 193 194 195 196

    QList<T> m_doubleEntries;

private:

197
    QHash<QString, T> m_hash;
198
    QHash<QString, QString> m_aliases;
199 200 201
};

#endif