Create a way to pre-register Core::Ids

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

Change-Id: I0d03441eab4172b31f813d478bdd2946d7056117
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
This commit is contained in:
hjk
2012-09-25 16:20:38 +02:00
parent 6b67f24865
commit cfde02f60a
2 changed files with 19 additions and 11 deletions

View File

@@ -52,6 +52,8 @@ namespace Core {
class StringHolder class StringHolder
{ {
public: public:
StringHolder() {}
StringHolder(const char *s, int length) StringHolder(const char *s, int length)
: n(length), str(s) : n(length), str(s)
{ {
@@ -93,8 +95,8 @@ struct IdCache : public QHash<StringHolder, int>
}; };
static int lastUid = 0; static int lastUid = 1000 * 1000;
static QVector<QByteArray> stringFromId; static QHash<int, StringHolder> stringFromId;
static IdCache idFromString; static IdCache idFromString;
static int theId(const char *str, int n = 0) 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); StringHolder sh(str, n);
int res = idFromString.value(sh, 0); int res = idFromString.value(sh, 0);
if (res == 0) { if (res == 0) {
if (lastUid == 0)
stringFromId.append(QByteArray());
res = ++lastUid; res = ++lastUid;
sh.str = qstrdup(sh.str); sh.str = qstrdup(sh.str);
idFromString[sh] = res; idFromString[sh] = res;
stringFromId.append(QByteArray::fromRawData(sh.str, sh.n)); stringFromId[res] = sh;
} }
return res; return res;
} }
@@ -132,23 +132,30 @@ Id::Id(const QString &name)
QByteArray Id::name() const QByteArray Id::name() const
{ {
return stringFromId.at(m_id); return stringFromId.value(m_id).str;
} }
QString Id::toString() const 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 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 // For debugging purposes
CORE_EXPORT const char *nameForId(int id) CORE_EXPORT const char *nameForId(int id)
{ {
return (stringFromId.constData() + id)->constData(); return stringFromId.value(id).str;
} }
} // namespace Core } // namespace Core

View File

@@ -42,6 +42,7 @@ class CORE_EXPORT Id
{ {
public: public:
Id() : m_id(0) {} Id() : m_id(0) {}
Id(int uid) : m_id(uid) {}
Id(const char *name); Id(const char *name);
explicit Id(const QByteArray &name); explicit Id(const QByteArray &name);
// FIXME: Remove // 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; }
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; } 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: private:
Id(int uid, int) : m_id(uid) {}
// Intentionally unimplemented // Intentionally unimplemented
Id(const QLatin1String &); Id(const QLatin1String &);
int m_id; int m_id;