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 <eike.ziller@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2024-05-28 12:11:48 +02:00
parent 72df0a9ced
commit ec2642e362
9 changed files with 28 additions and 35 deletions

View File

@@ -9,6 +9,7 @@
#include <utils/smallstring.h> #include <utils/smallstring.h>
#include <utils/span.h> #include <utils/span.h>
#include <utils/utility.h>
#include <QByteArrayView> #include <QByteArrayView>
#include <QList> #include <QList>
@@ -144,17 +145,10 @@ void convertToString(String &string, Number number)
string.append(number); string.append(number);
} }
template<typename Enumeration>
constexpr std::underlying_type_t<Enumeration> to_underlying(Enumeration enumeration) noexcept
{
static_assert(std::is_enum_v<Enumeration>, "to_underlying expect an enumeration");
return static_cast<std::underlying_type_t<Enumeration>>(enumeration);
}
template<typename String, typename Enumeration, typename std::enable_if_t<std::is_enum_v<Enumeration>, bool> = true> template<typename String, typename Enumeration, typename std::enable_if_t<std::is_enum_v<Enumeration>, bool> = true>
void convertToString(String &string, Enumeration enumeration) void convertToString(String &string, Enumeration enumeration)
{ {
string.append(to_underlying(enumeration)); string.append(Utils::to_underlying(enumeration));
} }
template<typename String> template<typename String>

View File

@@ -3,6 +3,8 @@
#include "view3dactioncommand.h" #include "view3dactioncommand.h"
#include <utils/utility.h>
#include <QDebug> #include <QDebug>
#include <QDataStream> #include <QDataStream>
@@ -64,16 +66,9 @@ QDebug operator<<(QDebug debug, const View3DActionCommand &command)
<< command.m_value << ")\n"; << command.m_value << ")\n";
} }
template<typename Enumeration>
constexpr std::underlying_type_t<Enumeration> to_underlying(Enumeration enumeration) noexcept
{
static_assert(std::is_enum_v<Enumeration>, "to_underlying expect an enumeration");
return static_cast<std::underlying_type_t<Enumeration>>(enumeration);
}
QDebug operator<<(QDebug debug, View3DActionType type) QDebug operator<<(QDebug debug, View3DActionType type)
{ {
return debug.nospace() << to_underlying(type); return debug.nospace() << Utils::to_underlying(type);
} }
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -89,7 +89,7 @@ public:
template<typename Enumeration, std::enable_if_t<std::is_enum_v<Enumeration>, bool> = true> template<typename Enumeration, std::enable_if_t<std::is_enum_v<Enumeration>, bool> = true>
void bind(int index, Enumeration enumeration) 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<long long>(value)); } void bind(int index, uint value) { bind(index, static_cast<long long>(value)); }

View File

@@ -4,6 +4,7 @@
#pragma once #pragma once
#include <utils/span.h> #include <utils/span.h>
#include <utils/utility.h>
#include <nanotrace/nanotracehr.h> #include <nanotrace/nanotracehr.h>
#include <type_traits> #include <type_traits>
@@ -11,13 +12,6 @@
namespace Sqlite { namespace Sqlite {
template<typename Enumeration>
static constexpr std::underlying_type_t<Enumeration> to_underlying(Enumeration enumeration) noexcept
{
static_assert(std::is_enum_v<Enumeration>, "to_underlying expect an enumeration");
return static_cast<std::underlying_type_t<Enumeration>>(enumeration);
}
template<auto Type, typename InternalIntegerType = long long> template<auto Type, typename InternalIntegerType = long long>
class BasicId class BasicId
{ {
@@ -38,7 +32,7 @@ public:
static constexpr BasicId createSpecialState(Enumeration specialState) static constexpr BasicId createSpecialState(Enumeration specialState)
{ {
BasicId id; BasicId id;
id.id = ::Sqlite::to_underlying(specialState); id.id = ::Utils::to_underlying(specialState);
return id; return id;
} }
@@ -77,7 +71,7 @@ public:
template<typename Enumeration> template<typename Enumeration>
constexpr bool hasSpecialState(Enumeration specialState) const constexpr bool hasSpecialState(Enumeration specialState) const
{ {
return id == ::Sqlite::to_underlying(specialState); return id == ::Utils::to_underlying(specialState);
} }
explicit operator bool() const { return isValid(); } explicit operator bool() const { return isValid(); }

View File

@@ -197,6 +197,7 @@ add_qtc_library(Utils
utilstr.h utilstr.h
utilsicons.cpp utilsicons.h utilsicons.cpp utilsicons.h
utiltypes.h utiltypes.h
utility.h
variablechooser.cpp variablechooser.h variablechooser.cpp variablechooser.h
winutils.cpp winutils.h winutils.cpp winutils.h
wizard.cpp wizard.h wizard.cpp wizard.h

14
src/libs/utils/utility.h Normal file
View File

@@ -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<typename Enumeration>
[[nodiscard]] constexpr std::underlying_type_t<Enumeration> to_underlying(Enumeration enumeration) noexcept
{
return static_cast<std::underlying_type_t<Enumeration>>(enumeration);
}
} // namespace Utils

View File

@@ -3894,7 +3894,7 @@ Utils::PathString ProjectStorage::createJson(const Storage::Synchronization::Par
json.append("\"}"); json.append("\"}");
} else { } else {
json.append(R"(","tr":)"); 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("}"); json.append("}");
} }
} }

View File

@@ -7,6 +7,7 @@
#include <sqlite/sqlitevalue.h> #include <sqlite/sqlitevalue.h>
#include <utils/smallstring.h> #include <utils/smallstring.h>
#include <utils/utility.h>
#include <QVarLengthArray> #include <QVarLengthArray>
@@ -20,13 +21,6 @@ namespace QmlDesigner {
template<std::size_t size> template<std::size_t size>
using SmallPathStrings = QVarLengthArray<Utils::PathString, size>; using SmallPathStrings = QVarLengthArray<Utils::PathString, size>;
template<typename Enumeration>
constexpr std::underlying_type_t<Enumeration> to_underlying(Enumeration enumeration) noexcept
{
static_assert(std::is_enum_v<Enumeration>, "to_underlying expect an enumeration");
return static_cast<std::underlying_type_t<Enumeration>>(enumeration);
}
enum class FlagIs : unsigned int { False, Set, True }; enum class FlagIs : unsigned int { False, Set, True };
template<typename String> template<typename String>

View File

@@ -10,6 +10,7 @@
#include <nanotrace/nanotracehr.h> #include <nanotrace/nanotracehr.h>
#include <sqlite/sqlitevalue.h> #include <sqlite/sqlitevalue.h>
#include <utils/smallstring.h> #include <utils/smallstring.h>
#include <utils/utility.h>
#include <tuple> #include <tuple>
#include <variant> #include <variant>
@@ -207,7 +208,7 @@ void convertToString(String &string, const IsAutoVersion &isAutoVersion)
constexpr bool operator<(IsAutoVersion first, IsAutoVersion second) 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 class ModuleExportedImport