id.cpp 8.17 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

30
#include "id.h"
con's avatar
con committed
31

32
33
#include <utils/qtcassert.h>

Tobias Hunger's avatar
Tobias Hunger committed
34
#include <QByteArray>
35
#include <QHash>
36
#include <QVariant>
con's avatar
con committed
37

Christian Kandeler's avatar
Christian Kandeler committed
38
39
#include <string.h>

40
namespace Core {
con's avatar
con committed
41

42
43
/*!
    \class Core::Id
con's avatar
con committed
44

45
46
47
48
49
50
51
52
    \brief The class Id encapsulates an identifier that is unique
    within a specific running Qt Creator process.

    \c{Core::Id} is used as facility to identify objects of interest
    in a more typesafe and faster manner than a plain \c QString or
    \c QByteArray would provide.

    An id is internally represented as a 32 bit integer (its \c UID)
53
    and associated with a plain 7-bit-clean ASCII name used
54
55
56
57
58
59
60
    for display and persistency.

    Each plugin that is distributed as part of Qt Creator has a
    private range of 10000 UIDs that are guaranteed to be unique.

    Third party plugins are advised to construct ids from their
    string representation.
61
62
63

*/

64
65
66
class StringHolder
{
public:
67
68
69
    StringHolder()
        : n(0), str(0)
    {}
hjk's avatar
hjk committed
70

Orgad Shaneh's avatar
Orgad Shaneh committed
71
72
    StringHolder(const char *s, int length)
        : n(length), str(s)
73
    {
Orgad Shaneh's avatar
Orgad Shaneh committed
74
        if (!n)
75
            length = n = static_cast<int>(strlen(s));
76
        h = 0;
Orgad Shaneh's avatar
Orgad Shaneh committed
77
        while (length--) {
78
79
80
81
82
83
84
85
86
87
88
89
90
            h = (h << 4) + *s++;
            h ^= (h & 0xf0000000) >> 23;
            h &= 0x0fffffff;
        }
    }
    int n;
    const char *str;
    uint h;
};

static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
{
    // sh.n is unlikely to discriminate better than the hash.
91
    return sh1.h == sh2.h && sh1.str && sh2.str && strcmp(sh1.str, sh2.str) == 0;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}


static uint qHash(const StringHolder &sh)
{
    return sh.h;
}

struct IdCache : public QHash<StringHolder, int>
{
#ifndef QTC_ALLOW_STATIC_LEAKS
    ~IdCache()
    {
        for (IdCache::iterator it = begin(); it != end(); ++it)
106
            delete[](const_cast<char *>(it.key().str));
107
108
109
110
111
    }
#endif
};


112
113
static int firstUnusedId = Id::IdsPerPlugin * Id::ReservedPlugins;

hjk's avatar
hjk committed
114
static QHash<int, StringHolder> stringFromId;
115
static IdCache idFromString;
con's avatar
con committed
116

Orgad Shaneh's avatar
Orgad Shaneh committed
117
static int theId(const char *str, int n = 0)
con's avatar
con committed
118
{
119
    QTC_ASSERT(str && *str, return 0);
Orgad Shaneh's avatar
Orgad Shaneh committed
120
    StringHolder sh(str, n);
121
    int res = idFromString.value(sh, 0);
122
    if (res == 0) {
123
        res = firstUnusedId++;
124
        sh.str = qstrdup(sh.str);
125
        idFromString[sh] = res;
hjk's avatar
hjk committed
126
        stringFromId[res] = sh;
127
128
    }
    return res;
con's avatar
con committed
129
130
}

Orgad Shaneh's avatar
Orgad Shaneh committed
131
132
133
134
135
static int theId(const QByteArray &ba)
{
    return theId(ba.constData(), ba.size());
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*!
    \fn Core::Id(int uid)

    \brief Constructs an id given a UID.

    The UID is an integer value that is unique within the running
    Qt Creator process.

    It is the callers responsibility to ensure the uniqueness of
    the passed integer. The recommended approach is to use
    \c{registerId()} with an value taken from the plugin's
    private range.

    \sa registerId()

*/

/*!
    Constructs an id given its associated name. The internal
    representation will be unspecified, but consistent within a
    Qt Creator process.

*/
159
Id::Id(const char *name)
Orgad Shaneh's avatar
Orgad Shaneh committed
160
161
162
    : m_id(theId(name, 0))
{}

163
164
165
166
/*!
    \overload

*/
Orgad Shaneh's avatar
Orgad Shaneh committed
167
168
Id::Id(const QByteArray &name)
   : m_id(theId(name))
169
{}
con's avatar
con committed
170

171
172
173
174
/*!
  Returns an internal representation of the id.
*/

175
176
QByteArray Id::name() const
{
hjk's avatar
hjk committed
177
    return stringFromId.value(m_id).str;
con's avatar
con committed
178
179
}

180
181
182
183
184
185
186
187
188
189
/*!
  Returns a string representation of the id suitable
  for UI display.

  This should not be used to create a persistent version
  of the Id, use \c{toSetting()} instead.

  \sa fromString(), toSetting()
*/

190
QString Id::toString() const
con's avatar
con committed
191
{
hjk's avatar
hjk committed
192
193
194
    return QString::fromUtf8(stringFromId.value(m_id).str);
}

195
196
197
198
199
200
/*!
  Creates an id from a string representation.

  This should not be used to handle a persistent version
  of the Id, use \c{fromSetting()} instead.

201
202
  \deprecated

203
204
205
206
207
208
209
210
  \sa toString(), fromSetting()
*/

Id Id::fromString(const QString &name)
{
    return Id(theId(name.toUtf8()));
}

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*!
  Creates an id from a string representation.

  This should not be used to handle a persistent version
  of the Id, use \c{fromSetting()} instead.

  \deprecated

  \sa toString(), fromSetting()
*/

Id Id::fromName(const QByteArray &name)
{
    return Id(theId(name));
}

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*!
  Returns a persistent value representing the id which is
  suitable to be stored in QSettings.

  \sa fromSetting()
*/

QVariant Id::toSetting() const
{
    return QVariant(QString::fromUtf8(stringFromId.value(m_id).str));
}

/*!
  Reconstructs an id from a persistent value.

  \sa toSetting()
*/

Id Id::fromSetting(const QVariant &variant)
{
    const QByteArray ba = variant.toString().toUtf8();
248
249
    if (ba.isEmpty())
        return Id();
250
251
252
    return Id(theId(ba));
}

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*!
  Constructs a derived id.

  This can be used to construct groups of ids logically
  belonging together. The associated internal name
  will be generated by appending \c{suffix}.
*/

Id Id::withSuffix(int suffix) const
{
    const QByteArray ba = name() + QByteArray::number(suffix);
    return Id(ba.constData());
}

/*!
  \overload
*/

Id Id::withSuffix(const char *suffix) const
272
{
273
    const QByteArray ba = name() + suffix;
274
275
276
    return Id(ba.constData());
}

hjk's avatar
hjk committed
277
278
279
280
281
282
/*!
  \overload

  \sa stringSuffix()
*/

283
284
285
286
287
288
Id Id::withSuffix(const QString &suffix) const
{
    const QByteArray ba = name() + suffix.toUtf8();
    return Id(ba.constData());
}

289
290
291
292
293
294
295
296
297
/*!
  Constructs a derived id.

  This can be used to construct groups of ids logically
  belonging together. The associated internal name
  will be generated by prepending \c{prefix}.
*/

Id Id::withPrefix(const char *prefix) const
298
{
299
    const QByteArray ba = prefix + name();
300
301
302
303
304
305
306
307
308
309
310
311
312
    return Id(ba.constData());
}


/*!
  Associates a id with its uid and its string
  representation.

  The uid should be taken from the plugin's private range.

  \sa fromSetting()
*/

hjk's avatar
hjk committed
313
314
315
316
317
void Id::registerId(int uid, const char *name)
{
    StringHolder sh(name, 0);
    idFromString[sh] = uid;
    stringFromId[uid] = sh;
318
319
320
321
}

bool Id::operator==(const char *name) const
{
322
323
324
325
326
    const char *string = stringFromId.value(m_id).str;
    if (string && name)
        return strcmp(string, name) == 0;
    else
        return false;
327
328
}

hjk's avatar
hjk committed
329
330
331
// For debugging purposes
CORE_EXPORT const char *nameForId(int id)
{
hjk's avatar
hjk committed
332
    return stringFromId.value(id).str;
hjk's avatar
hjk committed
333
334
}

hjk's avatar
hjk committed
335
336
337
338
339
bool Id::alphabeticallyBefore(Id other) const
{
    return toString().compare(other.toString(), Qt::CaseInsensitive) < 0;
}

hjk's avatar
hjk committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

/*!
  Convenience function to extract a part of the id string
  representation. This can be used to split off the base
  part used when generating an id with \c{withSuffix()}.

  \sa withSuffix()
*/

QString Id::suffixAfter(Id baseId) const
{
    const QByteArray b = baseId.name();
    const QByteArray n = name();
    return n.startsWith(b) ? QString::fromUtf8(n.mid(b.size())) : QString();
}

356
} // namespace Core
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374


QT_BEGIN_NAMESPACE

QDataStream &operator<<(QDataStream &ds, const Core::Id &id)
{
    return ds << id.name();
}

QDataStream &operator>>(QDataStream &ds, Core::Id &id)
{
    QByteArray ba;
    ds >> ba;
    id = Core::Id::fromName(ba);
    return ds;
}

QT_END_NAMESPACE