1 /*
2  *  Copyright (c) 2008 Cyrille Berger <cberger@cberger.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "KoUniqueNumberForIdServer.h"
21 
22 #include <QHash>
23 #include <QGlobalStatic>
24 
25 Q_GLOBAL_STATIC(KoUniqueNumberForIdServer, s_instance)
26 
27 struct Q_DECL_HIDDEN KoUniqueNumberForIdServer::Private {
PrivateKoUniqueNumberForIdServer::Private28     Private()
29         : currentNumber(0) {}
30 
31     QHash<QString, quint32 > id2Number;
32     quint32 currentNumber;
33 };
34 
KoUniqueNumberForIdServer()35 KoUniqueNumberForIdServer::KoUniqueNumberForIdServer()
36     : d(new Private)
37 {
38 }
39 
~KoUniqueNumberForIdServer()40 KoUniqueNumberForIdServer::~KoUniqueNumberForIdServer()
41 {
42     delete d;
43 }
44 
instance()45 KoUniqueNumberForIdServer* KoUniqueNumberForIdServer::instance()
46 {
47     return s_instance;
48 }
49 
numberForId(const QString & _id)50 quint32 KoUniqueNumberForIdServer::numberForId(const QString& _id)
51 {
52     QHash<QString, quint32>::iterator it = d->id2Number.find(_id);
53     if (it != d->id2Number.end()) {
54         return it.value();
55     }
56     quint32 number = ++d->currentNumber;
57     d->id2Number[ _id ] = number;
58     return number;
59 }
60