forked from qt-creator/qt-creator
Utils: Add C++ 20 converter function to_array
That can be quite useful to create arrays:
struct MyReallySpecialType
{
std::string_view name;
std::string_view expression;
};
std::string_view getExressionCArray(std::string_view name)
{
static constexpr MyReallySpecialType list[] = {
{"Left", "true"},
{"Center", "execute(%1)"},
{"Right", "false"}};
auto found =std::find_if(
std::begin(list),
std::end(list),
[&] (const auto &entry) { return entry.name == name; });
if (found != std::end(list))
return found->expression;
return {};
}
std::string_view getExressionStdArray(std::string_view name)
{
static constexpr auto list = Utils::to_array<MyReallySpecialType>({
{"Left", "true"},
{"Center", "execute(%1)"},
{"Right", "false"}});
auto found = std::find_if(
std::begin(list),
std::end(list),
[&] (const auto &entry) { return entry.name == name; });
if (found != std::end(list))
return found->expression;
return {};
}
Change-Id: Ifc737edb72d7a5f63b26203a5f22bfde19b5c2bc
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -11,6 +11,7 @@ add_qtc_library(Utils
|
||||
ansiescapecodehandler.cpp ansiescapecodehandler.h
|
||||
appmainwindow.cpp appmainwindow.h
|
||||
archive.cpp archive.h
|
||||
array.h
|
||||
aspects.cpp aspects.h
|
||||
async.cpp async.h
|
||||
basetreeview.cpp basetreeview.h
|
||||
|
||||
23
src/libs/utils/array.h
Normal file
23
src/libs/utils/array.h
Normal file
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
|
||||
namespace Utils {
|
||||
|
||||
namespace Internal {
|
||||
template<typename Type, std::size_t size, std::size_t... index>
|
||||
constexpr std::array<std::remove_cv_t<Type>, size> to_array_implementation(
|
||||
Type (&&array)[size], std::index_sequence<index...>)
|
||||
{
|
||||
return {{std::move(array[index])...}};
|
||||
}
|
||||
} // namespace Internal
|
||||
|
||||
template<typename Type, std::size_t size>
|
||||
constexpr std::array<std::remove_cv_t<Type>, size> to_array(Type (&&array)[size])
|
||||
{
|
||||
return Internal::to_array_implementation(std::move(array), std::make_index_sequence<size>{});
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
Reference in New Issue
Block a user