forked from qt-creator/qt-creator
QmlDesigner: Add compare factory
Instead of writing a complicated lambda a compare factory can be used to generate them. Instead of: std::ranges::sort(foo, [] (const Foo &first, const Foo &second) { return std::tie(first.name, first.version) < std::tie(second.name, second.version); }); you can write: std::ranges::sort(foos, makeLess(&Foo::name, &Foo::version)); Change-Id: I4daa5639f007a006a152d7b17688c189b3249cd8 Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -30,6 +30,7 @@ extend_qtc_library(QmlDesignerCore
|
||||
PUBLIC_INCLUDES designercoreutils
|
||||
SOURCES_PREFIX designercoreutils
|
||||
SOURCES
|
||||
functional.h
|
||||
generatedcomponentutils.cpp
|
||||
generatedcomponentutils.h
|
||||
modelmerger.cpp
|
||||
|
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
inline constexpr auto makeCompare = [](auto... projections) {
|
||||
return [=](auto compare) {
|
||||
return [=](const auto &first, const auto &second) {
|
||||
return compare(std::forward_as_tuple(std::invoke(projections, first)...),
|
||||
std::forward_as_tuple(std::invoke(projections, second)...));
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
inline constexpr auto makeLess = [](auto... projections) {
|
||||
return [=](const auto &first, const auto &second) {
|
||||
return std::ranges::less(std::forward_as_tuple(std::invoke(projections, first)...),
|
||||
std::forward_as_tuple(std::invoke(projections, second)...));
|
||||
};
|
||||
};
|
||||
|
||||
inline constexpr auto makeEqual = [](auto... projections) {
|
||||
return [=](const auto &first, const auto &second) {
|
||||
return std::ranges::equal_to(std::forward_as_tuple(std::invoke(projections, first)...),
|
||||
std::forward_as_tuple(std::invoke(projections, second)...));
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace QmlDesigner
|
@@ -13,6 +13,7 @@
|
||||
#include "sourcepathstorage/sourcepathcache.h"
|
||||
#include "typeannotationreader.h"
|
||||
|
||||
#include <functional.h>
|
||||
#include <sqlitedatabase.h>
|
||||
#include <tracing/qmldesignertracing.h>
|
||||
#include <utils/algorithm.h>
|
||||
@@ -1173,16 +1174,7 @@ void removeDuplicates(Storage::Synchronization::ExportedTypes &exportedTypes)
|
||||
{
|
||||
using Storage::Synchronization::ExportedType;
|
||||
|
||||
auto factory = [](auto... projections) {
|
||||
return [=](auto compare) {
|
||||
return [=](const auto &first, const auto &second) {
|
||||
return compare(std::forward_as_tuple(std::invoke(projections, first)...),
|
||||
std::forward_as_tuple(std::invoke(projections, second)...));
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
auto compare = factory(&ExportedType::name, &ExportedType::version);
|
||||
auto compare = makeCompare(&ExportedType::name, &ExportedType::version);
|
||||
auto less = compare(std::ranges::less{});
|
||||
auto equal = compare(std::ranges::equal_to{});
|
||||
|
||||
|
Reference in New Issue
Block a user