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 @@
#include "coreconstants.h"
#include "icontext.h"
#include <utils/qtcassert.h>
#include <QtCore/QHash>
#include <QtCore/QDebug>
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;
return data;
QTC_ASSERT(!ba.isEmpty(), /**/);
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
{
if (theUniqueIdentifiers().contains(*this))
return theUniqueIdentifiers().value(*this);
Id::Id(const char *name)
: m_id(theId(name))
{}
const int uid = theUniqueIdentifiers().count() + 1;
theUniqueIdentifiers().insert(*this, uid);
return uid;
Id::Id(const QString &name)
: m_id(theId(name.toLatin1()))
{}
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)
......
......@@ -44,25 +44,26 @@ namespace Core {
class CORE_EXPORT Id
{
public:
Id() {}
Id(const char *name) : m_name(name) {}
Id() : m_id(0) {}
Id(const char *name);
// FIXME: Replace with QByteArray
Id(const QString &name) : m_name(name.toLatin1()) {}
QByteArray name() const { return m_name; }
QString toString() const { return QString::fromLatin1(m_name); }
bool isValid() const { return !m_name.isEmpty(); }
bool operator==(const Id &id) const { return m_name == id.m_name; }
bool operator!=(const Id &id) const { return m_name != id.m_name; }
int uniqueIdentifier() const;
static Id fromUniqueIdentifier(int uid);
Id(const QString &name);
QByteArray name() const;
QString toString() const;
bool isValid() const { return m_id; }
bool operator==(const Id &id) const { return m_id == id.m_id; }
bool operator!=(const Id &id) const { return m_id != id.m_id; }
int uniqueIdentifier() const { return m_id; }
static Id fromUniqueIdentifier(int uid) { return Id(uid, uid); }
private:
Id(int uid, int) : m_id(uid) {}
// Intentionally unimplemented
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
......
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