From 10b52c7dba39f22cfcc973347bef8c07f4df236c Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 25 Jul 2024 17:46:27 +0200 Subject: [PATCH] Utils: Replace Id ctor from char * with char(&)[] ... and move the strlen() call to the header, where it can be replaced at compile time with a constant for true literals. Change-Id: I1c4b2b1c87b68bc218a235c95c90e4c8fa8e7a21 Reviewed-by: Orgad Shaneh --- src/libs/utils/id.cpp | 7 ++++--- src/libs/utils/id.h | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/libs/utils/id.cpp b/src/libs/utils/id.cpp index b9476c61b8b..68642cb34f9 100644 --- a/src/libs/utils/id.cpp +++ b/src/libs/utils/id.cpp @@ -124,13 +124,14 @@ static quintptr theId(const char *str, int n) \QC process. */ -Id::Id(const char *name) - : m_id(theId(name, 0)) +Id::Id(const char *s, size_t len) + : m_id(theId(s, len)) {} Id Id::generate() { - return Id{QUuid::createUuid().toByteArray()}; + const QByteArray ba = QUuid::createUuid().toByteArray(); + return Id(theId(ba.data(), ba.size())); } /*! diff --git a/src/libs/utils/id.h b/src/libs/utils/id.h index 9e80af45352..b40f8eecadb 100644 --- a/src/libs/utils/id.h +++ b/src/libs/utils/id.h @@ -22,7 +22,10 @@ class QTCREATOR_UTILS_EXPORT Id { public: Id() = default; - Id(const char *name); // Good to use. + + template + Id(const char (&s)[N]) : Id(s, N - 1) {} + Id(const QLatin1String &) = delete; static Id generate(); @@ -62,6 +65,7 @@ public: friend QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug dbg, const Id &id); private: + Id(const char *s, size_t len); explicit Id(quintptr uid) : m_id(uid) {} quintptr m_id = 0;