forked from qt-creator/qt-creator
Utils: Make Id::name() return a QByteArrayView
As we keep all id name content in IdCache which stays until application shutdown this is as safe as returning a full QByteArray, but cheaper. Change-Id: Ic41feb0a648d1267bbfb57a6a18a724a0ab52d80 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -137,10 +137,17 @@ Id Id::generate()
|
|||||||
Returns an internal representation of the id.
|
Returns an internal representation of the id.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
QByteArray Id::name() const
|
QByteArrayView Id::name() const
|
||||||
{
|
{
|
||||||
QReadLocker lock(&s_cacheMutex);
|
QReadLocker lock(&s_cacheMutex);
|
||||||
return stringFromId.value(m_id).str;
|
StringHolder holder = stringFromId.value(m_id);
|
||||||
|
return QByteArrayView(holder.str, holder.n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \internal */
|
||||||
|
QByteArray Id::toByteArray() const
|
||||||
|
{
|
||||||
|
return name().toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -155,8 +162,7 @@ QByteArray Id::name() const
|
|||||||
|
|
||||||
QString Id::toString() const
|
QString Id::toString() const
|
||||||
{
|
{
|
||||||
QReadLocker lock(&s_cacheMutex);
|
return QString::fromUtf8(name());
|
||||||
return QString::fromUtf8(stringFromId.value(m_id).str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \internal */
|
/*! \internal */
|
||||||
@@ -335,14 +341,14 @@ bool Id::alphabeticallyBefore(Id other) const
|
|||||||
|
|
||||||
QString Id::suffixAfter(Id baseId) const
|
QString Id::suffixAfter(Id baseId) const
|
||||||
{
|
{
|
||||||
const QByteArray b = baseId.name();
|
const QByteArrayView b = baseId.name();
|
||||||
const QByteArray n = name();
|
const QByteArrayView n = name();
|
||||||
return n.startsWith(b) ? QString::fromUtf8(n.mid(b.size())) : QString();
|
return n.startsWith(b) ? QString::fromUtf8(n.mid(b.size())) : QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream &operator<<(QDataStream &ds, Id id)
|
QDataStream &operator<<(QDataStream &ds, Id id)
|
||||||
{
|
{
|
||||||
return ds << id.name();
|
return ds << id.name().toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream &operator>>(QDataStream &ds, Id &id)
|
QDataStream &operator>>(QDataStream &ds, Id &id)
|
||||||
|
@@ -34,7 +34,8 @@ public:
|
|||||||
Id withSuffix(const QStringView suffix) const;
|
Id withSuffix(const QStringView suffix) const;
|
||||||
Id withPrefix(const char *prefix) const;
|
Id withPrefix(const char *prefix) const;
|
||||||
|
|
||||||
QByteArray name() const;
|
QByteArrayView name() const;
|
||||||
|
QByteArray toByteArray() const; // Avoid
|
||||||
QString toString() const; // Avoid.
|
QString toString() const; // Avoid.
|
||||||
Key toKey() const; // FIXME: Replace uses with .name() after Store/key transition.
|
Key toKey() const; // FIXME: Replace uses with .name() after Store/key transition.
|
||||||
QVariant toSetting() const; // Good to use.
|
QVariant toSetting() const; // Good to use.
|
||||||
|
@@ -19,6 +19,7 @@ public:
|
|||||||
Key(Key &&) = default;
|
Key(Key &&) = default;
|
||||||
|
|
||||||
Key(const QByteArray &key) : data(key) {}
|
Key(const QByteArray &key) : data(key) {}
|
||||||
|
Key(const QByteArrayView key) : data(key.toByteArray()) {}
|
||||||
|
|
||||||
template <int N>
|
template <int N>
|
||||||
Key(const char (&key)[N]) : data(key) {}
|
Key(const char (&key)[N]) : data(key) {}
|
||||||
|
@@ -597,7 +597,7 @@ TouchBarActionContainer::TouchBarActionContainer(Id id,
|
|||||||
const QIcon &icon,
|
const QIcon &icon,
|
||||||
const QString &text)
|
const QString &text)
|
||||||
: ActionContainerPrivate(id, actionManagerPrivate)
|
: ActionContainerPrivate(id, actionManagerPrivate)
|
||||||
, m_touchBar(std::make_unique<TouchBar>(id.withPrefix(ID_PREFIX).name(), icon, text))
|
, m_touchBar(std::make_unique<TouchBar>(id.withPrefix(ID_PREFIX).toByteArray(), icon, text))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -623,7 +623,7 @@ QAction *TouchBarActionContainer::actionForItem(QObject *item) const
|
|||||||
void TouchBarActionContainer::insertAction(QAction *before, Command *command)
|
void TouchBarActionContainer::insertAction(QAction *before, Command *command)
|
||||||
{
|
{
|
||||||
m_touchBar->insertAction(before,
|
m_touchBar->insertAction(before,
|
||||||
command->id().withPrefix(ID_PREFIX).name(),
|
command->id().withPrefix(ID_PREFIX).toByteArray(),
|
||||||
command->touchBarAction());
|
command->touchBarAction());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -158,7 +158,7 @@ Environment ExternalTool::baseEnvironment() const
|
|||||||
{
|
{
|
||||||
if (m_baseEnvironmentProviderId.isValid()) {
|
if (m_baseEnvironmentProviderId.isValid()) {
|
||||||
const std::optional<EnvironmentProvider> provider = EnvironmentProvider::provider(
|
const std::optional<EnvironmentProvider> provider = EnvironmentProvider::provider(
|
||||||
m_baseEnvironmentProviderId.name());
|
m_baseEnvironmentProviderId.toByteArray());
|
||||||
if (provider && provider->environment)
|
if (provider && provider->environment)
|
||||||
return provider->environment();
|
return provider->environment();
|
||||||
}
|
}
|
||||||
|
@@ -52,7 +52,7 @@ void MacroEvent::load(QDataStream &stream)
|
|||||||
|
|
||||||
void MacroEvent::save(QDataStream &stream) const
|
void MacroEvent::save(QDataStream &stream) const
|
||||||
{
|
{
|
||||||
stream << m_id.name();
|
stream << m_id.toByteArray();
|
||||||
stream << int(m_values.count());
|
stream << int(m_values.count());
|
||||||
for (auto i = m_values.cbegin(), end = m_values.cend(); i != end; ++i)
|
for (auto i = m_values.cbegin(), end = m_values.cend(); i != end; ++i)
|
||||||
stream << i.key() << i.value();
|
stream << i.key() << i.value();
|
||||||
|
Reference in New Issue
Block a user