From ec2642e362f43d1275650a118df0b6a05ce6aa43 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Tue, 28 May 2024 12:11:48 +0200 Subject: [PATCH] Utils: Add to_underlying to_underlying was added to C++23 to get the underlying integer type for an enumeration. Change-Id: Ib7262882a47cf4b060cff428bb10a6a65c089fc5 Reviewed-by: Eike Ziller Reviewed-by: Tim Jenssen --- src/libs/nanotrace/nanotracehr.h | 10 ++-------- .../commands/view3dactioncommand.cpp | 11 +++-------- src/libs/sqlite/sqlitebasestatement.h | 2 +- src/libs/sqlite/sqliteids.h | 12 +++--------- src/libs/utils/CMakeLists.txt | 1 + src/libs/utils/utility.h | 14 ++++++++++++++ .../designercore/projectstorage/projectstorage.cpp | 2 +- .../projectstorage/projectstorageinfotypes.h | 8 +------- .../projectstorage/projectstoragetypes.h | 3 ++- 9 files changed, 28 insertions(+), 35 deletions(-) create mode 100644 src/libs/utils/utility.h diff --git a/src/libs/nanotrace/nanotracehr.h b/src/libs/nanotrace/nanotracehr.h index 810e4e3c5d7..15f8109fe91 100644 --- a/src/libs/nanotrace/nanotracehr.h +++ b/src/libs/nanotrace/nanotracehr.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -144,17 +145,10 @@ void convertToString(String &string, Number number) string.append(number); } -template -constexpr std::underlying_type_t to_underlying(Enumeration enumeration) noexcept -{ - static_assert(std::is_enum_v, "to_underlying expect an enumeration"); - return static_cast>(enumeration); -} - template, bool> = true> void convertToString(String &string, Enumeration enumeration) { - string.append(to_underlying(enumeration)); + string.append(Utils::to_underlying(enumeration)); } template diff --git a/src/libs/qmlpuppetcommunication/commands/view3dactioncommand.cpp b/src/libs/qmlpuppetcommunication/commands/view3dactioncommand.cpp index eac0961393a..a522499ffa7 100644 --- a/src/libs/qmlpuppetcommunication/commands/view3dactioncommand.cpp +++ b/src/libs/qmlpuppetcommunication/commands/view3dactioncommand.cpp @@ -3,6 +3,8 @@ #include "view3dactioncommand.h" +#include + #include #include @@ -64,16 +66,9 @@ QDebug operator<<(QDebug debug, const View3DActionCommand &command) << command.m_value << ")\n"; } -template -constexpr std::underlying_type_t to_underlying(Enumeration enumeration) noexcept -{ - static_assert(std::is_enum_v, "to_underlying expect an enumeration"); - return static_cast>(enumeration); -} - QDebug operator<<(QDebug debug, View3DActionType type) { - return debug.nospace() << to_underlying(type); + return debug.nospace() << Utils::to_underlying(type); } } // namespace QmlDesigner diff --git a/src/libs/sqlite/sqlitebasestatement.h b/src/libs/sqlite/sqlitebasestatement.h index 9a4bb0a2a2f..64acba0a27a 100644 --- a/src/libs/sqlite/sqlitebasestatement.h +++ b/src/libs/sqlite/sqlitebasestatement.h @@ -89,7 +89,7 @@ public: template, bool> = true> void bind(int index, Enumeration enumeration) { - bind(index, to_underlying(enumeration)); + bind(index, Utils::to_underlying(enumeration)); } void bind(int index, uint value) { bind(index, static_cast(value)); } diff --git a/src/libs/sqlite/sqliteids.h b/src/libs/sqlite/sqliteids.h index 460cd91415a..abbb13d7c16 100644 --- a/src/libs/sqlite/sqliteids.h +++ b/src/libs/sqlite/sqliteids.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include @@ -11,13 +12,6 @@ namespace Sqlite { -template -static constexpr std::underlying_type_t to_underlying(Enumeration enumeration) noexcept -{ - static_assert(std::is_enum_v, "to_underlying expect an enumeration"); - return static_cast>(enumeration); -} - template class BasicId { @@ -38,7 +32,7 @@ public: static constexpr BasicId createSpecialState(Enumeration specialState) { BasicId id; - id.id = ::Sqlite::to_underlying(specialState); + id.id = ::Utils::to_underlying(specialState); return id; } @@ -77,7 +71,7 @@ public: template constexpr bool hasSpecialState(Enumeration specialState) const { - return id == ::Sqlite::to_underlying(specialState); + return id == ::Utils::to_underlying(specialState); } explicit operator bool() const { return isValid(); } diff --git a/src/libs/utils/CMakeLists.txt b/src/libs/utils/CMakeLists.txt index a9912b3575f..30e4be2eaa9 100644 --- a/src/libs/utils/CMakeLists.txt +++ b/src/libs/utils/CMakeLists.txt @@ -197,6 +197,7 @@ add_qtc_library(Utils utilstr.h utilsicons.cpp utilsicons.h utiltypes.h + utility.h variablechooser.cpp variablechooser.h winutils.cpp winutils.h wizard.cpp wizard.h diff --git a/src/libs/utils/utility.h b/src/libs/utils/utility.h new file mode 100644 index 00000000000..27de88d3398 --- /dev/null +++ b/src/libs/utils/utility.h @@ -0,0 +1,14 @@ +// Copyright (C) 2024 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 { + +template +[[nodiscard]] constexpr std::underlying_type_t to_underlying(Enumeration enumeration) noexcept +{ + return static_cast>(enumeration); +} + +} // namespace Utils diff --git a/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.cpp b/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.cpp index 2f56d569087..2283b64945a 100644 --- a/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.cpp +++ b/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.cpp @@ -3894,7 +3894,7 @@ Utils::PathString ProjectStorage::createJson(const Storage::Synchronization::Par json.append("\"}"); } else { json.append(R"(","tr":)"); - json.append(Utils::SmallString::number(to_underlying(parameter.traits))); + json.append(Utils::SmallString::number(Utils::to_underlying(parameter.traits))); json.append("}"); } } diff --git a/src/plugins/qmldesigner/designercore/projectstorage/projectstorageinfotypes.h b/src/plugins/qmldesigner/designercore/projectstorage/projectstorageinfotypes.h index 75ab0d1f5a5..1d630344aec 100644 --- a/src/plugins/qmldesigner/designercore/projectstorage/projectstorageinfotypes.h +++ b/src/plugins/qmldesigner/designercore/projectstorage/projectstorageinfotypes.h @@ -7,6 +7,7 @@ #include #include +#include #include @@ -20,13 +21,6 @@ namespace QmlDesigner { template using SmallPathStrings = QVarLengthArray; -template -constexpr std::underlying_type_t to_underlying(Enumeration enumeration) noexcept -{ - static_assert(std::is_enum_v, "to_underlying expect an enumeration"); - return static_cast>(enumeration); -} - enum class FlagIs : unsigned int { False, Set, True }; template diff --git a/src/plugins/qmldesigner/designercore/projectstorage/projectstoragetypes.h b/src/plugins/qmldesigner/designercore/projectstorage/projectstoragetypes.h index 5f51a9acf61..1592628af53 100644 --- a/src/plugins/qmldesigner/designercore/projectstorage/projectstoragetypes.h +++ b/src/plugins/qmldesigner/designercore/projectstorage/projectstoragetypes.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -207,7 +208,7 @@ void convertToString(String &string, const IsAutoVersion &isAutoVersion) constexpr bool operator<(IsAutoVersion first, IsAutoVersion second) { - return to_underlying(first) < to_underlying(second); + return Utils::to_underlying(first) < Utils::to_underlying(second); } class ModuleExportedImport