forked from qt-creator/qt-creator
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:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <utils/smallstring.h>
|
||||
#include <utils/span.h>
|
||||
#include <utils/utility.h>
|
||||
|
||||
#include <QByteArrayView>
|
||||
#include <QList>
|
||||
@@ -144,17 +145,10 @@ void convertToString(String &string, Number 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>
|
||||
void convertToString(String &string, Enumeration enumeration)
|
||||
{
|
||||
string.append(to_underlying(enumeration));
|
||||
string.append(Utils::to_underlying(enumeration));
|
||||
}
|
||||
|
||||
template<typename String>
|
||||
|
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "view3dactioncommand.h"
|
||||
|
||||
#include <utils/utility.h>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDataStream>
|
||||
|
||||
@@ -64,16 +66,9 @@ QDebug operator<<(QDebug debug, const View3DActionCommand &command)
|
||||
<< 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)
|
||||
{
|
||||
return debug.nospace() << to_underlying(type);
|
||||
return debug.nospace() << Utils::to_underlying(type);
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner
|
||||
|
@@ -89,7 +89,7 @@ public:
|
||||
template<typename Enumeration, std::enable_if_t<std::is_enum_v<Enumeration>, 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<long long>(value)); }
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <utils/span.h>
|
||||
#include <utils/utility.h>
|
||||
|
||||
#include <nanotrace/nanotracehr.h>
|
||||
#include <type_traits>
|
||||
@@ -11,13 +12,6 @@
|
||||
|
||||
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>
|
||||
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<typename Enumeration>
|
||||
constexpr bool hasSpecialState(Enumeration specialState) const
|
||||
{
|
||||
return id == ::Sqlite::to_underlying(specialState);
|
||||
return id == ::Utils::to_underlying(specialState);
|
||||
}
|
||||
|
||||
explicit operator bool() const { return isValid(); }
|
||||
|
@@ -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
|
||||
|
14
src/libs/utils/utility.h
Normal file
14
src/libs/utils/utility.h
Normal 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
|
@@ -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("}");
|
||||
}
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <sqlite/sqlitevalue.h>
|
||||
#include <utils/smallstring.h>
|
||||
#include <utils/utility.h>
|
||||
|
||||
#include <QVarLengthArray>
|
||||
|
||||
@@ -20,13 +21,6 @@ namespace QmlDesigner {
|
||||
template<std::size_t 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 };
|
||||
|
||||
template<typename String>
|
||||
|
@@ -10,6 +10,7 @@
|
||||
#include <nanotrace/nanotracehr.h>
|
||||
#include <sqlite/sqlitevalue.h>
|
||||
#include <utils/smallstring.h>
|
||||
#include <utils/utility.h>
|
||||
|
||||
#include <tuple>
|
||||
#include <variant>
|
||||
@@ -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
|
||||
|
Reference in New Issue
Block a user