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