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:
Marco Bubke
2024-10-24 19:27:32 +02:00
parent d0a8de5694
commit a58cf3784a
3 changed files with 35 additions and 10 deletions

View File

@@ -30,6 +30,7 @@ extend_qtc_library(QmlDesignerCore
PUBLIC_INCLUDES designercoreutils PUBLIC_INCLUDES designercoreutils
SOURCES_PREFIX designercoreutils SOURCES_PREFIX designercoreutils
SOURCES SOURCES
functional.h
generatedcomponentutils.cpp generatedcomponentutils.cpp
generatedcomponentutils.h generatedcomponentutils.h
modelmerger.cpp modelmerger.cpp

View File

@@ -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

View File

@@ -13,6 +13,7 @@
#include "sourcepathstorage/sourcepathcache.h" #include "sourcepathstorage/sourcepathcache.h"
#include "typeannotationreader.h" #include "typeannotationreader.h"
#include <functional.h>
#include <sqlitedatabase.h> #include <sqlitedatabase.h>
#include <tracing/qmldesignertracing.h> #include <tracing/qmldesignertracing.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
@@ -1173,16 +1174,7 @@ void removeDuplicates(Storage::Synchronization::ExportedTypes &exportedTypes)
{ {
using Storage::Synchronization::ExportedType; using Storage::Synchronization::ExportedType;
auto factory = [](auto... projections) { auto compare = makeCompare(&ExportedType::name, &ExportedType::version);
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 less = compare(std::ranges::less{}); auto less = compare(std::ranges::less{});
auto equal = compare(std::ranges::equal_to{}); auto equal = compare(std::ranges::equal_to{});