forked from qt-creator/qt-creator
QmlDesiger: add LazyUniquePtr
Creating a memory on heap is not cheap. To defer it LazyUniquePtr is allocating as the type is cast. Change-Id: Id3239da7136101040fcb47c9359c96d8ca236bb5 Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <designsystem/dsconstants.h>
|
||||
#include <designsystem/dsthememanager.h>
|
||||
#include <qmldesignerutils/memory.h>
|
||||
|
||||
#include <QQmlEngine>
|
||||
|
||||
namespace QmlDesigner {
|
||||
@@ -74,7 +76,7 @@ QStringList DesignSystemInterface::collections() const
|
||||
CollectionModel *DesignSystemInterface::createModel(const QString &typeName, DSThemeManager *collection)
|
||||
{
|
||||
auto [newItr, success] = m_models.try_emplace(typeName,
|
||||
std::make_unique<CollectionModel>(collection,
|
||||
makeLazyUniquePtr<CollectionModel>(collection,
|
||||
m_store));
|
||||
if (success) {
|
||||
// Otherwise the model will be deleted by the QML engine.
|
||||
|
@@ -263,9 +263,7 @@ void DSThemeManager::decorateThemeInterface(ModelNode rootNode) const
|
||||
|
||||
DSThemeGroup *DSThemeManager::propertyGroup(GroupType type)
|
||||
{
|
||||
auto itr = m_groups.find(type);
|
||||
if (itr == m_groups.end())
|
||||
itr = m_groups.try_emplace(type, std::make_unique<DSThemeGroup>(type)).first;
|
||||
auto itr = m_groups.try_emplace(type, makeLazySharedPtr<DSThemeGroup>(type)).first;
|
||||
|
||||
return itr->second.get();
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ add_qtc_library(QmlDesignerUtils STATIC
|
||||
asset.cpp asset.h
|
||||
designeralgorithm.h
|
||||
filedownloader.cpp filedownloader.h
|
||||
memory.h
|
||||
multifiledownloader.cpp multifiledownloader.h
|
||||
fileextractor.cpp fileextractor.h
|
||||
hdrimage.cpp hdrimage.h
|
||||
|
52
src/plugins/qmldesigner/libs/qmldesignerutils/memory.h
Normal file
52
src/plugins/qmldesigner/libs/qmldesignerutils/memory.h
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (C) 2023 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
template<typename Pointer, typename... Arguments>
|
||||
class LazyPtr
|
||||
{
|
||||
using ResultType = Pointer::element_type;
|
||||
|
||||
public:
|
||||
LazyPtr(Arguments &&...arguments)
|
||||
: m_arguments{std::forward_as_tuple(std::forward<Arguments>(arguments)...)}
|
||||
{}
|
||||
|
||||
operator Pointer() const
|
||||
{
|
||||
return std::apply(
|
||||
[](auto &&...arguments) {
|
||||
return Pointer(new ResultType(std::forward<decltype(arguments)>(arguments)...));
|
||||
},
|
||||
m_arguments);
|
||||
}
|
||||
|
||||
private:
|
||||
std::tuple<Arguments...> m_arguments;
|
||||
};
|
||||
|
||||
template<typename ResultType, typename... Arguments>
|
||||
using LazyUniquePtr = LazyPtr<std::unique_ptr<ResultType>, Arguments...>;
|
||||
|
||||
template<typename Result, typename... Arguments>
|
||||
LazyUniquePtr<Result, Arguments...> makeLazyUniquePtr(Arguments &&...arguments)
|
||||
{
|
||||
return LazyUniquePtr<Result, Arguments...>(std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
template<typename ResultType, typename... Arguments>
|
||||
using LazySharedPtr = LazyPtr<std::shared_ptr<ResultType>, Arguments...>;
|
||||
|
||||
template<typename ResultType, typename... Arguments>
|
||||
LazySharedPtr<ResultType, Arguments...> makeLazySharedPtr(Arguments &&...arguments)
|
||||
{
|
||||
return LazySharedPtr<ResultType, Arguments...>(std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner
|
Reference in New Issue
Block a user