summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Bubke <marco.bubke@qt.io>2022-02-02 16:33:59 +0100
committerMarco Bubke <marco.bubke@qt.io>2022-02-15 17:17:57 +0000
commitf84e0bd3ec9f18d4246c34c4fae197745b963b9f (patch)
tree84195ad567c4417c39fed091e45edaa607a42e96
parent57256988f587f8aae44479d3b9d804fb1550e40e (diff)
downloadqt-creator-f84e0bd3ec9f18d4246c34c4fae197745b963b9f.tar.gz
QmlDesigner: Use to_underlying to get the underlying enum type
There is now std::to_underlying in C++ 23 to prevent cast bugs for enumerations. Because we cannot use it and it is easy to have our own implementation we can use that so far and use std::to_underlying if we can use C++ 23. Change-Id: Iafc9163d659a98422d7aa8605d69d741d44d1052 Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
-rw-r--r--src/plugins/qmldesigner/designercore/projectstorage/projectstorage.h33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.h b/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.h
index e2058b4a6a..54228248c2 100644
--- a/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.h
+++ b/src/plugins/qmldesigner/designercore/projectstorage/projectstorage.h
@@ -40,9 +40,18 @@
#include <algorithm>
#include <tuple>
+#include <type_traits>
+#include <utility>
namespace QmlDesigner {
+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 Database>
class ProjectStorage final : public ProjectStorageInterface
{
@@ -1073,7 +1082,7 @@ private:
updateAliasIdPropertyDeclarationStatement.write(&nextPropertyDeclarationId,
&propertyDeclarationId);
updatePropertyAliasDeclarationRecursivelyWithTypeAndTraitsStatement
- .write(&propertyDeclarationId, &propertyTypeId, static_cast<int>(value.traits));
+ .write(&propertyDeclarationId, &propertyTypeId, to_underlying(value.traits));
}
}
@@ -1109,10 +1118,10 @@ private:
updatePropertyDeclarationStatement.write(&view.id,
&propertyTypeId,
- static_cast<int>(value.traits),
+ to_underlying(value.traits),
&propertyImportedTypeNameId);
updatePropertyAliasDeclarationRecursivelyWithTypeAndTraitsStatement
- .write(&view.id, &propertyTypeId, static_cast<int>(value.traits));
+ .write(&view.id, &propertyTypeId, to_underlying(value.traits));
propertyDeclarationIds.push_back(view.id);
return Sqlite::UpdateChange::Update;
}
@@ -1280,14 +1289,18 @@ private:
if (import.version.minor) {
insertDocumentImportWithVersionStatement.write(&import.sourceId,
&import.moduleId,
+ to_underlying(importKind),
import.version.major.value,
import.version.minor.value);
} else if (import.version.major) {
insertDocumentImportWithMajorVersionStatement.write(&import.sourceId,
&import.moduleId,
+ to_underlying(importKind),
import.version.major.value);
} else {
- insertDocumentImportWithoutVersionStatement.write(&import.sourceId, &import.moduleId);
+ insertDocumentImportWithoutVersionStatement.write(&import.sourceId,
+ &import.moduleId,
+ to_underlying(importKind));
}
};
@@ -1320,7 +1333,7 @@ private:
json.append("\"}");
} else {
json.append("\",\"tr\":");
- json.append(Utils::SmallString::number(static_cast<int>(parameter.traits)));
+ json.append(Utils::SmallString::number(to_underlying(parameter.traits)));
json.append("}");
}
}
@@ -1499,8 +1512,7 @@ private:
type.typeId = upsertTypeStatement.template value<TypeId>(&type.sourceId,
type.typeName,
- static_cast<int>(
- type.accessSemantics));
+ to_underlying(type.accessSemantics));
if (!type.typeId)
type.typeId = selectTypeIdBySourceIdAndNameStatement.template value<TypeId>(&type.sourceId,
@@ -1675,13 +1687,14 @@ private:
Utils::SmallStringView typeName)
{
auto importedTypeNameId = selectImportedTypeNameIdStatement.template value<ImportedTypeNameId>(
- static_cast<int>(kind), id, typeName);
+ to_underlying(kind), id, typeName);
if (importedTypeNameId)
return importedTypeNameId;
- return insertImportedTypeNameIdStatement
- .template value<ImportedTypeNameId>(static_cast<int>(kind), id, typeName);
+ return insertImportedTypeNameIdStatement.template value<ImportedTypeNameId>(to_underlying(kind),
+ id,
+ typeName);
}
TypeId fetchTypeId(ImportedTypeNameId typeNameId) const