QmlDesigner: Take advantage of stable references in std::map

References are stable for std::map for later insertions like
https://en.cppreference.com/w/cpp/container#Iterator_invalidation shows,
So there is no need to add an extra indirection.

The member has made to be mutable because the code is not const correct.

Change-Id: I9dd74aac80ddc2b9ae78000916d8a835cc9cb0cc
Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
This commit is contained in:
Marco Bubke
2025-02-14 15:55:31 +01:00
parent 3e343dda13
commit ca2fc0d02a
5 changed files with 19 additions and 22 deletions

View File

@@ -70,14 +70,12 @@ QStringList DesignSystemInterface::collections() const
CollectionModel *DesignSystemInterface::createModel(const QString &typeName, DSThemeManager *collection) CollectionModel *DesignSystemInterface::createModel(const QString &typeName, DSThemeManager *collection)
{ {
auto [iterator, inserted] = m_models.try_emplace(typeName, auto [iterator, inserted] = m_models.try_emplace(typeName, collection, m_store);
makeLazyUniquePtr<CollectionModel>(collection,
m_store));
if (inserted) { if (inserted) {
// Otherwise the model will be deleted by the QML engine. // Otherwise the model will be deleted by the QML engine.
QQmlEngine::setObjectOwnership(iterator->second.get(), QQmlEngine::CppOwnership); QQmlEngine::setObjectOwnership(&iterator->second, QQmlEngine::CppOwnership);
} }
return iterator->second.get(); return &iterator->second;
} }
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -42,7 +42,7 @@ private:
private: private:
class DSStore *m_store = nullptr; class DSStore *m_store = nullptr;
std::map<QString, std::unique_ptr<CollectionModel>> m_models; std::map<QString, CollectionModel> m_models;
}; };
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -190,7 +190,7 @@ DSThemeManager *DSStore::addCollection(const QString &qmlTypeName)
{ {
const QString componentType = uniqueCollectionName(qmlTypeName); const QString componentType = uniqueCollectionName(qmlTypeName);
auto [itr, success] = m_collections.try_emplace(componentType, DSThemeManager{}); auto [itr, success] = m_collections.try_emplace(componentType);
if (success) if (success)
return &itr->second; return &itr->second;

View File

@@ -116,7 +116,7 @@ void DSThemeManager::forAllGroups(std::function<void(GroupType, DSThemeGroup *)>
return; return;
for (auto &[gt, themeGroup] : m_groups) for (auto &[gt, themeGroup] : m_groups)
callback(gt, themeGroup.get()); callback(gt, &themeGroup);
} }
size_t DSThemeManager::themeCount() const size_t DSThemeManager::themeCount() const
@@ -126,9 +126,8 @@ size_t DSThemeManager::themeCount() const
size_t DSThemeManager::propertyCount() const size_t DSThemeManager::propertyCount() const
{ {
using groupPair = std::pair<const GroupType, std::shared_ptr<DSThemeGroup>>; return std::accumulate(m_groups.cbegin(), m_groups.cend(), 0ull, [](size_t c, const auto &g) {
return std::accumulate(m_groups.cbegin(), m_groups.cend(), 0ull, [](size_t c, const groupPair &g) { return c + g.second.count();
return c + g.second->count();
}); });
} }
@@ -138,7 +137,7 @@ void DSThemeManager::removeTheme(ThemeId id)
return; return;
for (auto &[gt, group] : m_groups) for (auto &[gt, group] : m_groups)
group->removeTheme(id); group.removeTheme(id);
if (m_themes.erase(id)) if (m_themes.erase(id))
reviewActiveTheme(); reviewActiveTheme();
@@ -147,13 +146,13 @@ void DSThemeManager::removeTheme(ThemeId id)
void DSThemeManager::duplicateTheme(ThemeId from, ThemeId to) void DSThemeManager::duplicateTheme(ThemeId from, ThemeId to)
{ {
for (auto &[gt, group] : m_groups) for (auto &[gt, group] : m_groups)
group->duplicateValues(from, to); group.duplicateValues(from, to);
} }
std::optional<GroupType> DSThemeManager::groupType(const PropertyName &name) const std::optional<GroupType> DSThemeManager::groupType(const PropertyName &name) const
{ {
for (const auto &[gt, group] : m_groups) { for (const auto &[gt, group] : m_groups) {
if (group->hasProperty(name)) if (group.hasProperty(name))
return gt; return gt;
} }
return {}; return {};
@@ -166,7 +165,7 @@ std::optional<ThemeProperty> DSThemeManager::property(ThemeId themeId,
if (m_themes.contains(themeId)) { if (m_themes.contains(themeId)) {
auto groupItr = m_groups.find(gType); auto groupItr = m_groups.find(gType);
if (groupItr != m_groups.end()) if (groupItr != m_groups.end())
return groupItr->second->propertyValue(themeId, name); return groupItr->second.propertyValue(themeId, name);
} }
qCDebug(dsLog) << "Error fetching property: {" << themeId << GroupId(gType) << name << "}"; qCDebug(dsLog) << "Error fetching property: {" << themeId << GroupId(gType) << name << "}";
@@ -248,7 +247,7 @@ void DSThemeManager::decorate(ModelNode rootNode, const QByteArray &nodeType, bo
// Add property groups // Add property groups
for (auto &[gt, group] : m_groups) for (auto &[gt, group] : m_groups)
group->decorate(themeId, themeNode, !isMCU); group.decorate(themeId, themeNode, !isMCU);
} }
} }
@@ -258,21 +257,21 @@ void DSThemeManager::decorateThemeInterface(ModelNode rootNode) const
return; return;
for (auto &[gt, group] : m_groups) for (auto &[gt, group] : m_groups)
group->decorateComponent(rootNode); group.decorateComponent(rootNode);
} }
DSThemeGroup *DSThemeManager::propertyGroup(GroupType type) DSThemeGroup *DSThemeManager::propertyGroup(GroupType type)
{ {
auto itr = m_groups.try_emplace(type, makeLazySharedPtr<DSThemeGroup>(type)).first; auto itr = m_groups.try_emplace(type, type).first;
return itr->second.get(); return &itr->second;
} }
void DSThemeManager::addGroupAliases(ModelNode rootNode) const void DSThemeManager::addGroupAliases(ModelNode rootNode) const
{ {
std::set<PropertyName> groupNames; std::set<PropertyName> groupNames;
for (auto &[groupType, group] : m_groups) { for (auto &[groupType, group] : m_groups) {
if (group->count()) if (group.count())
groupNames.emplace(GroupId(groupType)); groupNames.emplace(GroupId(groupType));
} }
@@ -396,7 +395,7 @@ PropertyName DSThemeManager::uniquePropertyName(const PropertyName &hint) const
{ {
auto isPropertyNameUsed = [this](const PropertyName &name) -> bool { auto isPropertyNameUsed = [this](const PropertyName &name) -> bool {
return std::any_of(m_groups.begin(), m_groups.end(), [&name](const auto &p) { return std::any_of(m_groups.begin(), m_groups.end(), [&name](const auto &p) {
return p.second->hasProperty(name); return p.second.hasProperty(name);
}); });
}; };

View File

@@ -79,7 +79,7 @@ private:
private: private:
std::map<ThemeId, ThemeName> m_themes; std::map<ThemeId, ThemeName> m_themes;
std::map<GroupType, std::shared_ptr<DSThemeGroup>> m_groups; mutable std::map<GroupType, DSThemeGroup> m_groups;
ThemeId m_activeTheme = static_cast<ThemeId>(0); ThemeId m_activeTheme = static_cast<ThemeId>(0);
}; };