forked from qt-creator/qt-creator
Core: Make enough room in an Id to store a pointer directly.
This switches internal storage from an (signed) int to an quintptr, making it possible for client code to use Ids directly based on pointers using Id::fromUniqueIdentifier(). Ids generated by existing code will keep using the low 32 bits only for now, so the change effectively only affects 64 bit systems. The patch also moves the rarely used fromUniqueIdentifier code path out-of-line and replaces the Id::IdsPerPlugin and Id::ReservedPlugins enums in the only place where they have been used (setting a lower limit for ids). The limit itself not really useful, but unchanged by this patch. Change-Id: Ieabc7d9d72f3c5dd749fb49b7193a0c8629c186e Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
@@ -53,16 +53,9 @@ namespace Core {
|
|||||||
in a more typesafe and faster manner than a plain \c QString or
|
in a more typesafe and faster manner than a plain \c QString or
|
||||||
\c QByteArray would provide.
|
\c QByteArray would provide.
|
||||||
|
|
||||||
An id is internally represented as a 32 bit integer (its \c UID)
|
An id is associated with a plain 7-bit-clean ASCII name used
|
||||||
and associated with a plain 7-bit-clean ASCII name used
|
|
||||||
for display and persistency.
|
for display and persistency.
|
||||||
|
|
||||||
Each plugin that is distributed as part of \QC 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.
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class StringHolder
|
class StringHolder
|
||||||
@@ -85,8 +78,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
int n;
|
int n;
|
||||||
uint h;
|
|
||||||
const char *str;
|
const char *str;
|
||||||
|
quintptr h;
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
|
static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
|
||||||
@@ -98,10 +91,10 @@ static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
|
|||||||
|
|
||||||
static uint qHash(const StringHolder &sh)
|
static uint qHash(const StringHolder &sh)
|
||||||
{
|
{
|
||||||
return sh.h;
|
return QT_PREPEND_NAMESPACE(qHash)(sh.h, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IdCache : public QHash<StringHolder, int>
|
struct IdCache : public QHash<StringHolder, quintptr>
|
||||||
{
|
{
|
||||||
#ifndef QTC_ALLOW_STATIC_LEAKS
|
#ifndef QTC_ALLOW_STATIC_LEAKS
|
||||||
~IdCache()
|
~IdCache()
|
||||||
@@ -113,13 +106,12 @@ struct IdCache : public QHash<StringHolder, int>
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static int firstUnusedId = Id::IdsPerPlugin * Id::ReservedPlugins;
|
static QHash<quintptr, StringHolder> stringFromId;
|
||||||
|
|
||||||
static QHash<int, StringHolder> stringFromId;
|
|
||||||
static IdCache idFromString;
|
static IdCache idFromString;
|
||||||
|
|
||||||
static int theId(const char *str, int n = 0)
|
static quintptr theId(const char *str, int n = 0)
|
||||||
{
|
{
|
||||||
|
static quintptr firstUnusedId = 10 * 1000 * 1000;
|
||||||
QTC_ASSERT(str && *str, return 0);
|
QTC_ASSERT(str && *str, return 0);
|
||||||
StringHolder sh(str, n);
|
StringHolder sh(str, n);
|
||||||
int res = idFromString.value(sh, 0);
|
int res = idFromString.value(sh, 0);
|
||||||
@@ -132,26 +124,20 @@ static int theId(const char *str, int n = 0)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int theId(const QByteArray &ba)
|
static quintptr theId(const QByteArray &ba)
|
||||||
{
|
{
|
||||||
return theId(ba.constData(), ba.size());
|
return theId(ba.constData(), ba.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn Core::Id::Id(int uid)
|
\fn Core::Id::Id(quintptr uid)
|
||||||
|
\internal
|
||||||
|
|
||||||
Constructs an id given \a UID.
|
Constructs an id given \a UID.
|
||||||
|
|
||||||
The UID is an integer value that is unique within the running
|
The UID is an integer value that is unique within the running
|
||||||
\QC process.
|
\QC 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()
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -333,7 +319,7 @@ bool Id::operator==(const char *name) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For debugging purposes
|
// For debugging purposes
|
||||||
CORE_EXPORT const char *nameForId(int id)
|
CORE_EXPORT const char *nameForId(quintptr id)
|
||||||
{
|
{
|
||||||
return stringFromId.value(id).str;
|
return stringFromId.value(id).str;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,10 +46,8 @@ namespace Core {
|
|||||||
class CORE_EXPORT Id
|
class CORE_EXPORT Id
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum { IdsPerPlugin = 10000, ReservedPlugins = 1000 };
|
|
||||||
|
|
||||||
Id() : m_id(0) {}
|
Id() : m_id(0) {}
|
||||||
Id(const char *name);
|
Id(const char *name); // Good to use.
|
||||||
|
|
||||||
Id withSuffix(int suffix) const;
|
Id withSuffix(int suffix) const;
|
||||||
Id withSuffix(const char *suffix) const;
|
Id withSuffix(const char *suffix) const;
|
||||||
@@ -68,8 +66,8 @@ public:
|
|||||||
bool operator<(Id id) const { return m_id < id.m_id; }
|
bool operator<(Id id) const { return m_id < id.m_id; }
|
||||||
bool operator>(Id id) const { return m_id > id.m_id; }
|
bool operator>(Id id) const { return m_id > id.m_id; }
|
||||||
bool alphabeticallyBefore(Id other) const;
|
bool alphabeticallyBefore(Id other) const;
|
||||||
int uniqueIdentifier() const { return m_id; }
|
|
||||||
static Id fromUniqueIdentifier(int uid) { return Id(uid); }
|
quintptr uniqueIdentifier() const { return m_id; } // Avoid.
|
||||||
static Id fromString(const QString &str); // FIXME: avoid.
|
static Id fromString(const QString &str); // FIXME: avoid.
|
||||||
static Id fromName(const QByteArray &ba); // FIXME: avoid.
|
static Id fromName(const QByteArray &ba); // FIXME: avoid.
|
||||||
static Id fromSetting(const QVariant &variant); // Good to use.
|
static Id fromSetting(const QVariant &variant); // Good to use.
|
||||||
@@ -81,14 +79,13 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
// Intentionally unimplemented
|
// Intentionally unimplemented
|
||||||
Id(const QLatin1String &);
|
Id(const QLatin1String &) = delete;
|
||||||
// Force explicit use of fromUniqueIdentifier().
|
explicit Id(quintptr uid) : m_id(uid) {}
|
||||||
explicit Id(int uid) : m_id(uid) {}
|
|
||||||
|
|
||||||
int m_id;
|
quintptr m_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline uint qHash(Id id) { return id.uniqueIdentifier(); }
|
inline uint qHash(Id id) { return static_cast<uint>(id.uniqueIdentifier()); }
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user