Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsmallbytearray_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTCORE_QSMALLBYTEARRAY_P_H
6#define QTCORE_QSMALLBYTEARRAY_P_H
7
8#include <QtCore/qbytearrayview.h>
9#include <QtCore/qtconfigmacros.h>
10#include <QtCore/qtypes.h>
11
12#include <array>
13#include <limits>
14
15//
16// W A R N I N G
17// -------------
18//
19// This file is not part of the Qt API. It exists purely as an
20// implementation detail. This header file may change from version to
21// version without notice, or even be removed.
22//
23// We mean it.
24//
25
26QT_BEGIN_NAMESPACE
27
28//
29// A fixed-max-size version of QByteArray. Since it's fixed-max-size, it's
30// never going to the heap. Can contain a maximum of 256 octets. Never
31// NUL-terminates on its own.
32//
33
34template <size_t N>
35class QSmallByteArray
36{
37 std::array<quint8, N> m_data;
38 static_assert(N <= (std::numeric_limits<std::uint8_t>::max)());
39 quint8 m_size = 0;
40public:
41 QSmallByteArray() = default;
42 // all compiler-generated SMFs are ok!
43 template <std::size_t M, std::enable_if_t<M < N, bool> = true> // M == N is for copy ctor!
44 constexpr QSmallByteArray(const QSmallByteArray<M> &other) noexcept
45 {
46 assign(other);
47 }
48 template <std::size_t M, std::enable_if_t<M < N, bool> = true> // M == N is for copy-assignment op!
49 constexpr QSmallByteArray &operator=(const QSmallByteArray<M> &other) noexcept
50 {
51 assign(other);
52 return *this;
53 }
54
55 template <typename Container> // ### underconstrained
56 constexpr void assign(const Container &c)
57 {
58 const size_t otherSize = size_t(std::size(c));
59 Q_ASSERT(otherSize < N);
60 memcpy(data(), std::data(c), otherSize);
61 m_size = quint8(otherSize);
62 }
63
64 constexpr quint8 *data() noexcept { return m_data.data(); }
65 constexpr const quint8 *data() const noexcept { return m_data.data(); }
66 constexpr qsizetype size() const noexcept { return qsizetype{m_size}; }
67 constexpr quint8 &operator[](qsizetype n)
68 {
69 Q_ASSERT(n < size());
70 return data()[n];
71 }
72 constexpr const quint8 &operator[](qsizetype n) const
73 {
74 Q_ASSERT(n < size());
75 return data()[n];
76 }
77 constexpr bool isEmpty() const noexcept { return size() == 0; }
78 constexpr void clear() noexcept { m_size = 0; }
79 constexpr void resizeForOverwrite(qsizetype s)
80 {
81 Q_ASSERT(s >= 0);
82 Q_ASSERT(size_t(s) <= N);
83 m_size = std::uint8_t(s);
84 }
85 constexpr void resize(qsizetype s, quint8 v)
86 {
87 const auto oldSize = size();
88 resizeForOverwrite(s);
89 if (s > oldSize)
90 memset(data() + oldSize, v, size() - oldSize);
91 }
92 constexpr QByteArrayView toByteArrayView() const noexcept
93 { return *this; }
94
95 constexpr auto begin() noexcept { return data(); }
96 constexpr auto begin() const noexcept { return data(); }
97 constexpr auto cbegin() const noexcept { return begin(); }
98 constexpr auto end() noexcept { return data() + size(); }
99 constexpr auto end() const noexcept { return data() + size(); }
100 constexpr auto cend() const noexcept { return end(); }
101};
102
103QT_END_NAMESPACE
104
105#endif // QTCORE_QSMALLBYTEARRAY_P_H