Move QSmallByteArray out of qcryptographichash.cpp

Its definition triggers QTCREATORBUG-28838, which has been marked as
Won't-Do.

By moving the class into its own header file, we limit the fallout of
said bug, which otherwise makes working in qcryptographichash.cpp a
nightmare with QtCreator.

Task-number: QTCREATORBUG-28838
Pick-to: 6.5
Change-Id: I12b8069ab77bfaee33b5df1b09757d617b142a44
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit d36835f0e3cb58a9858dc6cc13fbdec9ded7c227)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-10-28 09:02:54 +01:00 committed by Qt Cherry-pick Bot
parent c851050fae
commit bf8bdf4564
3 changed files with 106 additions and 69 deletions

View File

@ -304,6 +304,7 @@ qt_internal_add_module(Core
tools/qsharedpointer.cpp tools/qsharedpointer.h
tools/qsharedpointer_impl.h
tools/qsize.cpp tools/qsize.h
tools/qsmallbytearray_p.h
tools/qspan.h
tools/qspan_p.h
tools/qstack.h

View File

@ -6,6 +6,7 @@
#include <qcryptographichash.h>
#include <qmessageauthenticationcode.h>
#include <QtCore/private/qsmallbytearray_p.h>
#include <qiodevice.h>
#include <qmutex.h>
#include <qvarlengtharray.h>
@ -124,75 +125,6 @@ static inline int SHA384_512AddLength(SHA512Context *context, unsigned int lengt
QT_BEGIN_NAMESPACE
template <size_t N>
class QSmallByteArray
{
std::array<quint8, N> m_data;
static_assert(N <= (std::numeric_limits<std::uint8_t>::max)());
quint8 m_size = 0;
public:
QSmallByteArray() = default;
// all compiler-generated SMFs are ok!
template <std::size_t M, std::enable_if_t<M < N, bool> = true> // M == N is for copy ctor!
constexpr QSmallByteArray(const QSmallByteArray<M> &other) noexcept
{
assign(other);
}
template <std::size_t M, std::enable_if_t<M < N, bool> = true> // M == N is for copy-assignment op!
constexpr QSmallByteArray &operator=(const QSmallByteArray<M> &other) noexcept
{
assign(other);
return *this;
}
template <typename Container> // ### underconstrained
constexpr void assign(const Container &c)
{
const size_t otherSize = size_t(std::size(c));
Q_ASSERT(otherSize < N);
memcpy(data(), std::data(c), otherSize);
m_size = quint8(otherSize);
}
constexpr quint8 *data() noexcept { return m_data.data(); }
constexpr const quint8 *data() const noexcept { return m_data.data(); }
constexpr qsizetype size() const noexcept { return qsizetype{m_size}; }
constexpr quint8 &operator[](qsizetype n)
{
Q_ASSERT(n < size());
return data()[n];
}
constexpr const quint8 &operator[](qsizetype n) const
{
Q_ASSERT(n < size());
return data()[n];
}
constexpr bool isEmpty() const noexcept { return size() == 0; }
constexpr void clear() noexcept { m_size = 0; }
constexpr void resizeForOverwrite(qsizetype s)
{
Q_ASSERT(s >= 0);
Q_ASSERT(size_t(s) <= N);
m_size = std::uint8_t(s);
}
constexpr void resize(qsizetype s, quint8 v)
{
const auto oldSize = size();
resizeForOverwrite(s);
if (s > oldSize)
memset(data() + oldSize, v, size() - oldSize);
}
constexpr QByteArrayView toByteArrayView() const noexcept
{ return *this; }
constexpr auto begin() noexcept { return data(); }
constexpr auto begin() const noexcept { return data(); }
constexpr auto cbegin() const noexcept { return begin(); }
constexpr auto end() noexcept { return data() + size(); }
constexpr auto end() const noexcept { return data() + size(); }
constexpr auto cend() const noexcept { return end(); }
};
static constexpr int hashLengthInternal(QCryptographicHash::Algorithm method) noexcept
{
switch (method) {

View File

@ -0,0 +1,104 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QTCORE_QSMALLBYTEARRAY_P_H
#define QTCORE_QSMALLBYTEARRAY_P_H
#include <QtCore/qbytearrayview.h>
#include <QtCore/qtconfigmacros.h>
#include <QtCore/qtypes.h>
#include <array>
#include <limits>
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
QT_BEGIN_NAMESPACE
//
// A fixed-max-size version of QByteArray. Since it's fixed-max-size, it's
// never going to the heap. Can contain a maximum of 256 octets. Never
// NUL-terminates on its own.
//
template <size_t N>
class QSmallByteArray
{
std::array<quint8, N> m_data;
static_assert(N <= (std::numeric_limits<std::uint8_t>::max)());
quint8 m_size = 0;
public:
QSmallByteArray() = default;
// all compiler-generated SMFs are ok!
template <std::size_t M, std::enable_if_t<M < N, bool> = true> // M == N is for copy ctor!
constexpr QSmallByteArray(const QSmallByteArray<M> &other) noexcept
{
assign(other);
}
template <std::size_t M, std::enable_if_t<M < N, bool> = true> // M == N is for copy-assignment op!
constexpr QSmallByteArray &operator=(const QSmallByteArray<M> &other) noexcept
{
assign(other);
return *this;
}
template <typename Container> // ### underconstrained
constexpr void assign(const Container &c)
{
const size_t otherSize = size_t(std::size(c));
Q_ASSERT(otherSize < N);
memcpy(data(), std::data(c), otherSize);
m_size = quint8(otherSize);
}
constexpr quint8 *data() noexcept { return m_data.data(); }
constexpr const quint8 *data() const noexcept { return m_data.data(); }
constexpr qsizetype size() const noexcept { return qsizetype{m_size}; }
constexpr quint8 &operator[](qsizetype n)
{
Q_ASSERT(n < size());
return data()[n];
}
constexpr const quint8 &operator[](qsizetype n) const
{
Q_ASSERT(n < size());
return data()[n];
}
constexpr bool isEmpty() const noexcept { return size() == 0; }
constexpr void clear() noexcept { m_size = 0; }
constexpr void resizeForOverwrite(qsizetype s)
{
Q_ASSERT(s >= 0);
Q_ASSERT(size_t(s) <= N);
m_size = std::uint8_t(s);
}
constexpr void resize(qsizetype s, quint8 v)
{
const auto oldSize = size();
resizeForOverwrite(s);
if (s > oldSize)
memset(data() + oldSize, v, size() - oldSize);
}
constexpr QByteArrayView toByteArrayView() const noexcept
{ return *this; }
constexpr auto begin() noexcept { return data(); }
constexpr auto begin() const noexcept { return data(); }
constexpr auto cbegin() const noexcept { return begin(); }
constexpr auto end() noexcept { return data() + size(); }
constexpr auto end() const noexcept { return data() + size(); }
constexpr auto cend() const noexcept { return end(); }
};
QT_END_NAMESPACE
#endif // QTCORE_QSMALLBYTEARRAY_P_H