2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2011-09-05 16:10:37 +02:00
|
|
|
#include "id.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-11-30 11:47:39 +01:00
|
|
|
#include <utils/algorithm.h>
|
2011-11-09 08:18:00 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-05-03 11:33:08 +02:00
|
|
|
#include <QByteArray>
|
2013-06-06 17:34:06 +02:00
|
|
|
#include <QDataStream>
|
2015-06-16 11:30:34 +02:00
|
|
|
#include <QDebug>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QHash>
|
2013-03-27 08:29:38 +01:00
|
|
|
#include <QVariant>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-06-07 14:04:07 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2011-09-02 12:45:36 +02:00
|
|
|
namespace Core {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-09-02 12:45:36 +02:00
|
|
|
/*!
|
|
|
|
|
\class Core::Id
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The Id class encapsulates an identifier that is unique
|
|
|
|
|
within a specific running \QC process.
|
2013-01-14 16:05:36 +01:00
|
|
|
|
|
|
|
|
\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.
|
|
|
|
|
|
2015-06-19 14:01:31 +02:00
|
|
|
An id is associated with a plain 7-bit-clean ASCII name used
|
2013-01-14 16:05:36 +01:00
|
|
|
for display and persistency.
|
|
|
|
|
|
2011-09-02 12:45:36 +02:00
|
|
|
*/
|
|
|
|
|
|
2012-01-31 13:14:14 +01:00
|
|
|
class StringHolder
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-07-21 21:11:46 +02:00
|
|
|
StringHolder() = default;
|
2012-09-25 16:20:38 +02:00
|
|
|
|
2012-11-20 07:18:01 +02:00
|
|
|
StringHolder(const char *s, int length)
|
|
|
|
|
: n(length), str(s)
|
2012-01-31 13:14:14 +01:00
|
|
|
{
|
2012-11-20 07:18:01 +02:00
|
|
|
if (!n)
|
2013-02-17 09:00:54 +08:00
|
|
|
length = n = static_cast<int>(strlen(s));
|
2012-01-31 13:14:14 +01:00
|
|
|
h = 0;
|
2012-11-20 07:18:01 +02:00
|
|
|
while (length--) {
|
2012-01-31 13:14:14 +01:00
|
|
|
h = (h << 4) + *s++;
|
|
|
|
|
h ^= (h & 0xf0000000) >> 23;
|
|
|
|
|
h &= 0x0fffffff;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-21 21:11:46 +02:00
|
|
|
int n = 0;
|
|
|
|
|
const char *str = nullptr;
|
2015-06-19 14:01:31 +02:00
|
|
|
quintptr h;
|
2012-01-31 13:14:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
|
|
|
|
|
{
|
|
|
|
|
// sh.n is unlikely to discriminate better than the hash.
|
2013-01-23 15:51:52 +01:00
|
|
|
return sh1.h == sh2.h && sh1.str && sh2.str && strcmp(sh1.str, sh2.str) == 0;
|
2012-01-31 13:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint qHash(const StringHolder &sh)
|
|
|
|
|
{
|
2015-06-19 14:01:31 +02:00
|
|
|
return QT_PREPEND_NAMESPACE(qHash)(sh.h, 0);
|
2012-01-31 13:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 14:01:31 +02:00
|
|
|
struct IdCache : public QHash<StringHolder, quintptr>
|
2012-01-31 13:14:14 +01:00
|
|
|
{
|
|
|
|
|
#ifndef QTC_ALLOW_STATIC_LEAKS
|
|
|
|
|
~IdCache()
|
|
|
|
|
{
|
|
|
|
|
for (IdCache::iterator it = begin(); it != end(); ++it)
|
2012-09-12 18:01:12 +02:00
|
|
|
delete[](const_cast<char *>(it.key().str));
|
2012-01-31 13:14:14 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-06-19 14:01:31 +02:00
|
|
|
static QHash<quintptr, StringHolder> stringFromId;
|
2012-01-31 13:14:14 +01:00
|
|
|
static IdCache idFromString;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-06-19 14:01:31 +02:00
|
|
|
static quintptr theId(const char *str, int n = 0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-06-19 14:01:31 +02:00
|
|
|
static quintptr firstUnusedId = 10 * 1000 * 1000;
|
2012-01-31 13:14:14 +01:00
|
|
|
QTC_ASSERT(str && *str, return 0);
|
2012-11-20 07:18:01 +02:00
|
|
|
StringHolder sh(str, n);
|
2012-01-31 13:14:14 +01:00
|
|
|
int res = idFromString.value(sh, 0);
|
2011-11-09 08:18:00 +01:00
|
|
|
if (res == 0) {
|
2013-01-14 16:05:36 +01:00
|
|
|
res = firstUnusedId++;
|
2012-08-27 12:48:51 +02:00
|
|
|
sh.str = qstrdup(sh.str);
|
2012-01-31 13:14:14 +01:00
|
|
|
idFromString[sh] = res;
|
2012-09-25 16:20:38 +02:00
|
|
|
stringFromId[res] = sh;
|
2011-11-09 08:18:00 +01:00
|
|
|
}
|
|
|
|
|
return res;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 14:01:31 +02:00
|
|
|
static quintptr theId(const QByteArray &ba)
|
2012-11-20 07:18:01 +02:00
|
|
|
{
|
|
|
|
|
return theId(ba.constData(), ba.size());
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 16:05:36 +01:00
|
|
|
/*!
|
2015-06-19 14:01:31 +02:00
|
|
|
\fn Core::Id::Id(quintptr uid)
|
|
|
|
|
\internal
|
2013-01-14 16:05:36 +01:00
|
|
|
|
2013-09-06 16:38:53 +02:00
|
|
|
Constructs an id given \a UID.
|
2013-01-14 16:05:36 +01:00
|
|
|
|
|
|
|
|
The UID is an integer value that is unique within the running
|
2013-09-06 16:38:53 +02:00
|
|
|
\QC process.
|
2013-01-14 16:05:36 +01:00
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2013-09-06 16:38:53 +02:00
|
|
|
Constructs an id given its associated \a name. The internal
|
2013-01-14 16:05:36 +01:00
|
|
|
representation will be unspecified, but consistent within a
|
2013-09-06 16:38:53 +02:00
|
|
|
\QC process.
|
2013-01-14 16:05:36 +01:00
|
|
|
|
|
|
|
|
*/
|
2011-11-09 08:18:00 +01:00
|
|
|
Id::Id(const char *name)
|
2012-11-20 07:18:01 +02:00
|
|
|
: m_id(theId(name, 0))
|
|
|
|
|
{}
|
|
|
|
|
|
2013-01-14 16:05:36 +01:00
|
|
|
/*!
|
|
|
|
|
Returns an internal representation of the id.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-09 08:18:00 +01:00
|
|
|
QByteArray Id::name() const
|
|
|
|
|
{
|
2012-09-25 16:20:38 +02:00
|
|
|
return stringFromId.value(m_id).str;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-14 16:05:36 +01:00
|
|
|
/*!
|
|
|
|
|
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()
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-09 08:18:00 +01:00
|
|
|
QString Id::toString() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2012-09-25 16:20:38 +02:00
|
|
|
return QString::fromUtf8(stringFromId.value(m_id).str);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 16:05:36 +01:00
|
|
|
/*!
|
|
|
|
|
Creates an id from a string representation.
|
|
|
|
|
|
|
|
|
|
This should not be used to handle a persistent version
|
|
|
|
|
of the Id, use \c{fromSetting()} instead.
|
|
|
|
|
|
2013-01-15 13:28:17 +01:00
|
|
|
\deprecated
|
|
|
|
|
|
2013-01-14 16:05:36 +01:00
|
|
|
\sa toString(), fromSetting()
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
Id Id::fromString(const QString &name)
|
|
|
|
|
{
|
2015-12-04 12:36:54 +01:00
|
|
|
if (name.isEmpty())
|
|
|
|
|
return Id();
|
2013-01-14 16:05:36 +01:00
|
|
|
return Id(theId(name.toUtf8()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 13:28:17 +01:00
|
|
|
/*!
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 16:05:36 +01:00
|
|
|
/*!
|
|
|
|
|
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();
|
2013-01-25 17:45:18 +01:00
|
|
|
if (ba.isEmpty())
|
|
|
|
|
return Id();
|
2013-01-14 16:05:36 +01:00
|
|
|
return Id(theId(ba));
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 11:47:39 +01:00
|
|
|
Id Id::versionedId(const QByteArray &prefix, int major, int minor)
|
|
|
|
|
{
|
2015-11-30 12:00:14 +01:00
|
|
|
QTC_ASSERT(major >= 0, return fromName(prefix));
|
2015-11-30 11:47:39 +01:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-05-28 11:05:37 +02:00
|
|
|
return QSet<Id>::fromList(Utils::transform(list, &Id::fromString));
|
2015-11-30 11:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Id::toStringList(const QSet<Id> &ids)
|
|
|
|
|
{
|
2015-11-27 11:31:35 +01:00
|
|
|
QList<Id> idList = ids.toList();
|
|
|
|
|
Utils::sort(idList);
|
2018-05-28 11:05:37 +02:00
|
|
|
return Utils::transform(idList, &Id::toString);
|
2015-11-30 11:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-15 13:28:17 +01:00
|
|
|
/*!
|
|
|
|
|
Constructs a derived id.
|
|
|
|
|
|
|
|
|
|
This can be used to construct groups of ids logically
|
|
|
|
|
belonging together. The associated internal name
|
2013-09-06 16:38:53 +02:00
|
|
|
will be generated by appending \a suffix.
|
2013-01-15 13:28:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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
|
2013-01-14 16:05:36 +01:00
|
|
|
{
|
2013-01-15 13:28:17 +01:00
|
|
|
const QByteArray ba = name() + suffix;
|
2013-01-14 16:05:36 +01:00
|
|
|
return Id(ba.constData());
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-21 16:19:09 +01:00
|
|
|
/*!
|
|
|
|
|
\overload
|
|
|
|
|
|
|
|
|
|
\sa stringSuffix()
|
|
|
|
|
*/
|
|
|
|
|
|
2013-03-19 12:16:51 +01:00
|
|
|
Id Id::withSuffix(const QString &suffix) const
|
|
|
|
|
{
|
|
|
|
|
const QByteArray ba = name() + suffix.toUtf8();
|
|
|
|
|
return Id(ba.constData());
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 13:28:17 +01:00
|
|
|
/*!
|
|
|
|
|
Constructs a derived id.
|
|
|
|
|
|
|
|
|
|
This can be used to construct groups of ids logically
|
|
|
|
|
belonging together. The associated internal name
|
2013-09-06 16:38:53 +02:00
|
|
|
will be generated by prepending \a prefix.
|
2013-01-15 13:28:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
Id Id::withPrefix(const char *prefix) const
|
2013-01-14 16:05:36 +01:00
|
|
|
{
|
2013-01-15 13:28:17 +01:00
|
|
|
const QByteArray ba = prefix + name();
|
2013-01-14 16:05:36 +01:00
|
|
|
return Id(ba.constData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-03 14:33:10 +02:00
|
|
|
bool Id::operator==(const char *name) const
|
|
|
|
|
{
|
2013-01-23 15:51:52 +01:00
|
|
|
const char *string = stringFromId.value(m_id).str;
|
|
|
|
|
if (string && name)
|
|
|
|
|
return strcmp(string, name) == 0;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2010-06-25 17:37:59 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-17 17:15:24 +02:00
|
|
|
// For debugging purposes
|
2015-06-19 14:01:31 +02:00
|
|
|
CORE_EXPORT const char *nameForId(quintptr id)
|
2012-07-17 17:15:24 +02:00
|
|
|
{
|
2012-09-25 16:20:38 +02:00
|
|
|
return stringFromId.value(id).str;
|
2012-07-17 17:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
2013-01-18 11:16:53 +01:00
|
|
|
bool Id::alphabeticallyBefore(Id other) const
|
|
|
|
|
{
|
|
|
|
|
return toString().compare(other.toString(), Qt::CaseInsensitive) < 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-21 16:19:09 +01:00
|
|
|
|
|
|
|
|
/*!
|
2013-09-06 16:38:53 +02:00
|
|
|
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()}.
|
2013-03-21 16:19:09 +01:00
|
|
|
|
|
|
|
|
\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();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-02 12:45:36 +02:00
|
|
|
} // namespace Core
|
2013-03-19 19:01:30 +01:00
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
2014-07-01 11:08:26 +02:00
|
|
|
QDataStream &operator<<(QDataStream &ds, Core::Id id)
|
2013-03-19 19:01:30 +01:00
|
|
|
{
|
|
|
|
|
return ds << id.name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDataStream &operator>>(QDataStream &ds, Core::Id &id)
|
|
|
|
|
{
|
|
|
|
|
QByteArray ba;
|
|
|
|
|
ds >> ba;
|
|
|
|
|
id = Core::Id::fromName(ba);
|
|
|
|
|
return ds;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 10:49:46 +02:00
|
|
|
QDebug operator<<(QDebug dbg, const Core::Id &id)
|
|
|
|
|
{
|
|
|
|
|
return dbg << id.name();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-19 19:01:30 +01:00
|
|
|
QT_END_NAMESPACE
|