id.cpp 8.67 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
** 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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

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

33
#include <utils/algorithm.h>
34
35
#include <utils/qtcassert.h>

Tobias Hunger's avatar
Tobias Hunger committed
36
#include <QByteArray>
Christian Kandeler's avatar
Christian Kandeler committed
37
#include <QDataStream>
Nikolai Kosjar's avatar
Nikolai Kosjar committed
38
#include <QDebug>
39
#include <QHash>
40
#include <QVariant>
con's avatar
con committed
41

Christian Kandeler's avatar
Christian Kandeler committed
42
43
#include <string.h>

44
namespace Core {
con's avatar
con committed
45

46
47
/*!
    \class Core::Id
con's avatar
con committed
48

49
50
    \brief The Id class encapsulates an identifier that is unique
    within a specific running \QC process.
51
52
53
54
55
56

    \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)
57
    and associated with a plain 7-bit-clean ASCII name used
58
59
    for display and persistency.

Leena Miettinen's avatar
Leena Miettinen committed
60
    Each plugin that is distributed as part of \QC has a
61
62
63
64
    private range of 10000 UIDs that are guaranteed to be unique.

    Third party plugins are advised to construct ids from their
    string representation.
65
66
67

*/

68
69
70
class StringHolder
{
public:
71
72
73
    StringHolder()
        : n(0), str(0)
    {}
hjk's avatar
hjk committed
74

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

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


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)
110
            delete[](const_cast<char *>(it.key().str));
111
112
113
114
115
    }
#endif
};


116
117
static int firstUnusedId = Id::IdsPerPlugin * Id::ReservedPlugins;

hjk's avatar
hjk committed
118
static QHash<int, StringHolder> stringFromId;
119
static IdCache idFromString;
con's avatar
con committed
120

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

Orgad Shaneh's avatar
Orgad Shaneh committed
135
136
137
138
139
static int theId(const QByteArray &ba)
{
    return theId(ba.constData(), ba.size());
}

140
/*!
141
    \fn Core::Id::Id(int uid)
142

Leena Miettinen's avatar
Leena Miettinen committed
143
    Constructs an id given \a UID.
144
145

    The UID is an integer value that is unique within the running
Leena Miettinen's avatar
Leena Miettinen committed
146
    \QC process.
147
148
149
150
151
152
153
154
155
156
157

    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()

*/

/*!
Leena Miettinen's avatar
Leena Miettinen committed
158
    Constructs an id given its associated \a name. The internal
159
    representation will be unspecified, but consistent within a
Leena Miettinen's avatar
Leena Miettinen committed
160
    \QC process.
161
162

*/
163
Id::Id(const char *name)
Orgad Shaneh's avatar
Orgad Shaneh committed
164
165
166
    : m_id(theId(name, 0))
{}

167
168
169
170
/*!
  Returns an internal representation of the id.
*/

171
172
QByteArray Id::name() const
{
hjk's avatar
hjk committed
173
    return stringFromId.value(m_id).str;
con's avatar
con committed
174
175
}

176
177
178
179
180
181
182
183
184
185
/*!
  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()
*/

186
QString Id::toString() const
con's avatar
con committed
187
{
hjk's avatar
hjk committed
188
189
190
    return QString::fromUtf8(stringFromId.value(m_id).str);
}

191
192
193
194
195
196
/*!
  Creates an id from a string representation.

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

197
198
  \deprecated

199
200
201
202
203
204
205
206
  \sa toString(), fromSetting()
*/

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

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*!
  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));
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*!
  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();
244
245
    if (ba.isEmpty())
        return Id();
246
247
248
    return Id(theId(ba));
}

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Id Id::versionedId(const QByteArray &prefix, int major, int minor)
{
    if (major < 0)
        return fromName(prefix);

    QByteArray result = prefix + '.';
    result += QString::number(major).toLatin1();

    if (minor < 0)
        return fromName(result);
    return fromName(result + '.' + QString::number(minor).toLatin1());
}

QSet<Id> Id::fromStringList(const QStringList &list)
{
    return QSet<Id>::fromList(Utils::transform(list, [](const QString &s) { return Id::fromString(s); }));
}

QStringList Id::toStringList(const QSet<Id> &ids)
{
269
270
271
    QList<Id> idList = ids.toList();
    Utils::sort(idList);
    return Utils::transform(idList, [](Id i) { return i.toString(); });
272
273
}

274
275
276
277
278
/*!
  Constructs a derived id.

  This can be used to construct groups of ids logically
  belonging together. The associated internal name
Leena Miettinen's avatar
Leena Miettinen committed
279
  will be generated by appending \a suffix.
280
281
282
283
284
285
286
287
288
289
290
291
292
*/

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
293
{
294
    const QByteArray ba = name() + suffix;
295
296
297
    return Id(ba.constData());
}

hjk's avatar
hjk committed
298
299
300
301
302
303
/*!
  \overload

  \sa stringSuffix()
*/

304
305
306
307
308
309
Id Id::withSuffix(const QString &suffix) const
{
    const QByteArray ba = name() + suffix.toUtf8();
    return Id(ba.constData());
}

310
311
312
313
314
/*!
  Constructs a derived id.

  This can be used to construct groups of ids logically
  belonging together. The associated internal name
Leena Miettinen's avatar
Leena Miettinen committed
315
  will be generated by prepending \a prefix.
316
317
318
*/

Id Id::withPrefix(const char *prefix) const
319
{
320
    const QByteArray ba = prefix + name();
321
322
323
324
    return Id(ba.constData());
}


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

hjk's avatar
hjk committed
334
335
336
// For debugging purposes
CORE_EXPORT const char *nameForId(int id)
{
hjk's avatar
hjk committed
337
    return stringFromId.value(id).str;
hjk's avatar
hjk committed
338
339
}

hjk's avatar
hjk committed
340
341
342
343
344
bool Id::alphabeticallyBefore(Id other) const
{
    return toString().compare(other.toString(), Qt::CaseInsensitive) < 0;
}

hjk's avatar
hjk committed
345
346

/*!
Leena Miettinen's avatar
Leena Miettinen committed
347
348
349
  Extracts a part of the id string
  representation. This function can be used to split off the base
  part specified by \a baseId used when generating an id with \c{withSuffix()}.
hjk's avatar
hjk committed
350
351
352
353
354
355
356
357
358
359
360

  \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();
}

361
} // namespace Core
362
363
364

QT_BEGIN_NAMESPACE

365
QDataStream &operator<<(QDataStream &ds, Core::Id id)
366
367
368
369
370
371
372
373
374
375
376
377
{
    return ds << id.name();
}

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

378
379
380
381
382
QDebug operator<<(QDebug dbg, const Core::Id &id)
{
    return dbg << id.name();
}

383
QT_END_NAMESPACE