Commit cfde02f6 authored by hjk's avatar hjk
Browse files

Create a way to pre-register Core::Ids



This enables using integral values in addition to strings as key.

Change-Id: I0d03441eab4172b31f813d478bdd2946d7056117
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 6b67f248
......@@ -52,6 +52,8 @@ namespace Core {
class StringHolder
{
public:
StringHolder() {}
StringHolder(const char *s, int length)
: n(length), str(s)
{
......@@ -93,8 +95,8 @@ struct IdCache : public QHash<StringHolder, int>
};
static int lastUid = 0;
static QVector<QByteArray> stringFromId;
static int lastUid = 1000 * 1000;
static QHash<int, StringHolder> stringFromId;
static IdCache idFromString;
static int theId(const char *str, int n = 0)
......@@ -103,12 +105,10 @@ static int theId(const char *str, int n = 0)
StringHolder sh(str, n);
int res = idFromString.value(sh, 0);
if (res == 0) {
if (lastUid == 0)
stringFromId.append(QByteArray());
res = ++lastUid;
sh.str = qstrdup(sh.str);
idFromString[sh] = res;
stringFromId.append(QByteArray::fromRawData(sh.str, sh.n));
stringFromId[res] = sh;
}
return res;
}
......@@ -132,23 +132,30 @@ Id::Id(const QString &name)
QByteArray Id::name() const
{
return stringFromId.at(m_id);
return stringFromId.value(m_id).str;
}
QString Id::toString() const
{
return QString::fromUtf8(stringFromId.at(m_id));
return QString::fromUtf8(stringFromId.value(m_id).str);
}
void Id::registerId(int uid, const char *name)
{
StringHolder sh(name, 0);
idFromString[sh] = uid;
stringFromId[uid] = sh;
}
bool Id::operator==(const char *name) const
{
return strcmp(stringFromId.at(m_id).constData(), name) == 0;
return strcmp(stringFromId.value(m_id).str, name) == 0;
}
// For debugging purposes
CORE_EXPORT const char *nameForId(int id)
{
return (stringFromId.constData() + id)->constData();
return stringFromId.value(id).str;
}
} // namespace Core
......@@ -42,6 +42,7 @@ class CORE_EXPORT Id
{
public:
Id() : m_id(0) {}
Id(int uid) : m_id(uid) {}
Id(const char *name);
explicit Id(const QByteArray &name);
// FIXME: Remove
......@@ -56,10 +57,10 @@ public:
bool operator<(Id id) const { return m_id < id.m_id; }
bool operator>(Id id) const { return m_id > id.m_id; }
int uniqueIdentifier() const { return m_id; }
static Id fromUniqueIdentifier(int uid) { return Id(uid, uid); }
static Id fromUniqueIdentifier(int uid) { return Id(uid); }
static void registerId(int uid, const char *name);
private:
Id(int uid, int) : m_id(uid) {}
// Intentionally unimplemented
Id(const QLatin1String &);
int m_id;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment