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>
2023-09-30 15:08:51 +02:00
|
|
|
// 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
|
|
|
|
|
|
2023-10-04 22:42:30 +02:00
|
|
|
#include <array>
|
|
|
|
|
|
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>
2023-09-30 15:08:51 +02:00
|
|
|
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
|