Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
26
QT_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
34
template
<size_t N>
35
class
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;
40
41
void
assign_impl(
const
void
*p, size_t n)
42
{
43
Q_PRE(n <= N);
44
if
(n)
45
memcpy(data(), p, n);
46
m_size = quint8(n);
47
}
48
public
:
49
QSmallByteArray() =
default
;
50
// all compiler-generated SMFs are ok!
51
template
<std::size_t M, std::enable_if_t<M < N,
bool
> =
true
>
// M == N is for copy ctor!
52
constexpr
QSmallByteArray(
const
QSmallByteArray<M> &other)
noexcept
53
{
54
assign(other);
55
}
56
template
<std::size_t M, std::enable_if_t<M < N,
bool
> =
true
>
// M == N is for copy-assignment op!
57
constexpr
QSmallByteArray &operator=(
const
QSmallByteArray<M> &other)
noexcept
58
{
59
assign(other);
60
return
*
this
;
61
}
62
63
template
<
typename
Container>
// ### underconstrained
64
constexpr
void
assign(
const
Container &c)
65
{
66
assign_impl(std::data(c), size_t(std::size(c)));
67
}
68
69
constexpr
quint8 *data()
noexcept
{
return
m_data.data(); }
70
constexpr
const
quint8 *data()
const
noexcept
{
return
m_data.data(); }
71
constexpr
qsizetype size()
const
noexcept
{
return
qsizetype{m_size}; }
72
constexpr
quint8 &operator[](qsizetype n)
73
{
74
Q_ASSERT(n < size());
75
return
data()[n];
76
}
77
constexpr
const
quint8 &operator[](qsizetype n)
const
78
{
79
Q_ASSERT(n < size());
80
return
data()[n];
81
}
82
constexpr
bool
isEmpty()
const
noexcept
{
return
size() == 0; }
83
constexpr
void
clear()
noexcept
{ m_size = 0; }
84
constexpr
void
resizeForOverwrite(qsizetype s)
85
{
86
Q_ASSERT(s >= 0);
87
Q_ASSERT(size_t(s) <= N);
88
m_size = std::uint8_t(s);
89
}
90
constexpr
void
resize(qsizetype s, quint8 v)
91
{
92
const
auto
oldSize = size();
93
resizeForOverwrite(s);
94
if
(s > oldSize)
95
memset(data() + oldSize, v, size() - oldSize);
96
}
97
constexpr
QByteArrayView toByteArrayView()
const
noexcept
98
{
return
*
this
; }
99
100
constexpr
auto
begin()
noexcept
{
return
data(); }
101
constexpr
auto
begin()
const
noexcept
{
return
data(); }
102
constexpr
auto
cbegin()
const
noexcept
{
return
begin(); }
103
constexpr
auto
end()
noexcept
{
return
data() + size(); }
104
constexpr
auto
end()
const
noexcept
{
return
data() + size(); }
105
constexpr
auto
cend()
const
noexcept
{
return
end(); }
106
};
107
108
QT_END_NAMESPACE
109
110
#
endif
// QTCORE_QSMALLBYTEARRAY_P_H
qtbase
src
corelib
tools
qsmallbytearray_p.h
Generated on
for Qt by
1.18.0