Commit cfc9b71e authored by hjk's avatar hjk Committed by hjk
Browse files

coreplugin: replace QByteArray by int for ids



Change-Id: I3135cb745b4e8adc6d7a7145bac51317544f9844
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent 3a8c0f28
...@@ -34,7 +34,10 @@ ...@@ -34,7 +34,10 @@
#include "coreconstants.h" #include "coreconstants.h"
#include "icontext.h" #include "icontext.h"
#include <utils/qtcassert.h>
#include <QtCore/QHash> #include <QtCore/QHash>
#include <QtCore/QDebug>
namespace Core { namespace Core {
...@@ -47,27 +50,40 @@ namespace Core { ...@@ -47,27 +50,40 @@ namespace Core {
*/ */
uint qHash(const Core::Id &id) { return qHash(id.name()); } static int lastUid = 0;
static QVector<QByteArray> stringFromId;
static QHash<QByteArray, int> idFromString;
static QHash<Core::Id, int> &theUniqueIdentifiers() static int theId(const QByteArray &ba)
{ {
static QHash<Core::Id, int> data; QTC_ASSERT(!ba.isEmpty(), /**/);
return data; int res = idFromString.value(ba);
if (res == 0) {
if (lastUid == 0)
stringFromId.append(QByteArray());
res = ++lastUid;
idFromString[ba] = res;
stringFromId.append(ba);
}
return res;
} }
int Id::uniqueIdentifier() const Id::Id(const char *name)
{ : m_id(theId(name))
if (theUniqueIdentifiers().contains(*this)) {}
return theUniqueIdentifiers().value(*this);
const int uid = theUniqueIdentifiers().count() + 1; Id::Id(const QString &name)
theUniqueIdentifiers().insert(*this, uid); : m_id(theId(name.toLatin1()))
return uid; {}
QByteArray Id::name() const
{
return stringFromId.at(m_id);
} }
Id Id::fromUniqueIdentifier(int uid) QString Id::toString() const
{ {
return theUniqueIdentifiers().key(uid); return QString::fromLatin1(stringFromId[m_id]);
} }
Context::Context(const char *id, int offset) Context::Context(const char *id, int offset)
......
...@@ -44,25 +44,26 @@ namespace Core { ...@@ -44,25 +44,26 @@ namespace Core {
class CORE_EXPORT Id class CORE_EXPORT Id
{ {
public: public:
Id() {} Id() : m_id(0) {}
Id(const char *name) : m_name(name) {} Id(const char *name);
// FIXME: Replace with QByteArray // FIXME: Replace with QByteArray
Id(const QString &name) : m_name(name.toLatin1()) {} Id(const QString &name);
QByteArray name() const { return m_name; } QByteArray name() const;
QString toString() const { return QString::fromLatin1(m_name); } QString toString() const;
bool isValid() const { return !m_name.isEmpty(); } bool isValid() const { return m_id; }
bool operator==(const Id &id) const { return m_name == id.m_name; } bool operator==(const Id &id) const { return m_id == id.m_id; }
bool operator!=(const Id &id) const { return m_name != id.m_name; } bool operator!=(const Id &id) const { return m_id != id.m_id; }
int uniqueIdentifier() const; int uniqueIdentifier() const { return m_id; }
static Id fromUniqueIdentifier(int uid); static Id fromUniqueIdentifier(int uid) { return Id(uid, uid); }
private: private:
Id(int uid, int) : m_id(uid) {}
// Intentionally unimplemented // Intentionally unimplemented
Id(const QLatin1String &); Id(const QLatin1String &);
QByteArray m_name; int m_id;
}; };
CORE_EXPORT uint qHash(const Id &id); CORE_EXPORT inline uint qHash(const Id &id) { return id.uniqueIdentifier(); }
} // namespace Core } // namespace Core
......
Markdown is supported
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