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
qbitarray.h
Go to the documentation of this file.
1// Copyright (C) 2020 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 QBITARRAY_H
6#define QBITARRAY_H
7
8#include <QtCore/qbytearray.h>
9
11
12class QBitRef;
13class Q_CORE_EXPORT QBitArray
14{
15 Q_CORE_EXPORT friend QBitArray operator&(const QBitArray &a1, const QBitArray &a2);
16 friend QBitArray operator&(QBitArray &&a1, const QBitArray &a2)
17 { return a1 &= a2; }
18 friend QBitArray operator&(const QBitArray &a1, QBitArray &&a2)
19 { return a2 &= a1; }
20 friend QBitArray operator&(QBitArray &&a1, QBitArray &&a2)
21 { return a1 &= a2; }
22
23 Q_CORE_EXPORT friend QBitArray operator|(const QBitArray &a1, const QBitArray &a2);
24 friend QBitArray operator|(QBitArray &&a1, const QBitArray &a2)
25 { return a1 |= a2; }
26 friend QBitArray operator|(const QBitArray &a1, QBitArray &&a2)
27 { return a2 |= a1; }
28 friend QBitArray operator|(QBitArray &&a1, QBitArray &&a2)
29 { return a1 |= a2; }
30
31 Q_CORE_EXPORT friend QBitArray operator^(const QBitArray &a1, const QBitArray &a2);
32 friend QBitArray operator^(QBitArray &&a1, const QBitArray &a2)
33 { return a1 ^= a2; }
34 friend QBitArray operator^(const QBitArray &a1, QBitArray &&a2)
35 { return a2 ^= a1; }
36 friend QBitArray operator^(QBitArray &&a1, QBitArray &&a2)
37 { return a1 ^= a2; }
38
39#ifndef QT_NO_DATASTREAM
40 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
41 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
42#endif
43 friend Q_CORE_EXPORT size_t qHash(const QBitArray &key, size_t seed) noexcept;
44 friend QBitArray operator~(QBitArray a)
45 { return std::move(a).inverted_inplace(); }
46 QByteArray d;
47
48 QBitArray(QByteArrayData &&dd) : d(std::move(dd)) {}
49
50 template <typename BitArray> static auto bitLocation(BitArray &ba, qsizetype i)
51 {
52 Q_ASSERT(size_t(i) < size_t(ba.size()));
53 struct R {
54 decltype(ba.d[1]) byte;
55 uchar bitMask;
56 };
57 qsizetype byteIdx = i >> 3;
58 qsizetype bitIdx = i & 7;
59 return R{ ba.d[1 + byteIdx], uchar(1U << bitIdx) };
60 }
61
62 QBitArray inverted_inplace() &&;
63
64public:
65 inline QBitArray() noexcept {}
66 explicit QBitArray(qsizetype size, bool val = false);
67 // Rule Of Zero applies
68#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
69 QBitArray(const QBitArray &other) noexcept : d(other.d) {}
70 inline QBitArray &operator=(const QBitArray &other) noexcept { d = other.d; return *this; }
71 inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
72 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBitArray)
73#endif // Qt 6
74
75 void swap(QBitArray &other) noexcept { d.swap(other.d); }
76
77 qsizetype size() const { return qsizetype((size_t(d.size()) << 3) - *d.constData()); }
78 qsizetype count() const { return size(); }
79 qsizetype count(bool on) const;
80
81 inline bool isEmpty() const { return d.isEmpty(); }
82 inline bool isNull() const { return d.isNull(); }
83
84 void resize(qsizetype size);
85
86 inline void detach() { d.detach(); }
87 inline bool isDetached() const { return d.isDetached(); }
88 inline void clear() { d.clear(); }
89
90 bool testBit(qsizetype i) const
91 { auto r = bitLocation(*this, i); return r.byte & r.bitMask; }
92 void setBit(qsizetype i)
93 { auto r = bitLocation(*this, i); r.byte |= r.bitMask; }
94 void setBit(qsizetype i, bool val)
95 { if (val) setBit(i); else clearBit(i); }
96 void clearBit(qsizetype i)
97 { auto r = bitLocation(*this, i); r.byte &= ~r.bitMask; }
98 bool toggleBit(qsizetype i)
99 {
100 auto r = bitLocation(*this, i);
101 bool cl = r.byte & r.bitMask;
102 r.byte ^= r.bitMask;
103 return cl;
104 }
105
106 bool at(qsizetype i) const { return testBit(i); }
107 inline QBitRef operator[](qsizetype i);
108 bool operator[](qsizetype i) const { return testBit(i); }
109
110 QBitArray &operator&=(QBitArray &&);
111 QBitArray &operator|=(QBitArray &&);
112 QBitArray &operator^=(QBitArray &&);
113 QBitArray &operator&=(const QBitArray &);
114 QBitArray &operator|=(const QBitArray &);
115 QBitArray &operator^=(const QBitArray &);
116#if QT_CORE_REMOVED_SINCE(6, 7)
117 QBitArray operator~() const;
118#endif
119
120#if QT_CORE_REMOVED_SINCE(6, 8)
121 inline bool operator==(const QBitArray &other) const { return comparesEqual(d, other.d); }
122 inline bool operator!=(const QBitArray &other) const { return !operator==(other); }
123#endif
124
125 bool fill(bool aval, qsizetype asize = -1)
126 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
127 void fill(bool val, qsizetype first, qsizetype last);
128
129 inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); }
130
131 const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
132 static QBitArray fromBits(const char *data, qsizetype len);
133
134 quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
135
136public:
137 typedef QByteArray::DataPointer DataPtr;
138 inline DataPtr &data_ptr() { return d.data_ptr(); }
139 inline const DataPtr &data_ptr() const { return d.data_ptr(); }
140
141private:
142 friend bool comparesEqual(const QBitArray &lhs, const QBitArray &rhs) noexcept
143 {
144 return lhs.d == rhs.d;
145 }
146 Q_DECLARE_EQUALITY_COMPARABLE(QBitArray)
147};
148
149class QT6_ONLY(Q_CORE_EXPORT) QBitRef
150{
151private:
152 QBitArray &a;
153 qsizetype i;
154 inline QBitRef(QBitArray &array, qsizetype idx) : a(array), i(idx) { }
155 friend class QBitArray;
156
157public:
158 inline operator bool() const { return a.testBit(i); }
159 inline bool operator!() const { return !a.testBit(i); }
160 QBitRef &operator=(const QBitRef &val) { a.setBit(i, val); return *this; }
161 QBitRef &operator=(bool val) { a.setBit(i, val); return *this; }
162};
163
164QBitRef QBitArray::operator[](qsizetype i)
165{ Q_ASSERT(i >= 0); return QBitRef(*this, i); }
166
167#ifndef QT_NO_DATASTREAM
168Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
169Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
170#endif
171
172#ifndef QT_NO_DEBUG_STREAM
173Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &);
174#endif
175
177
178QT_END_NAMESPACE
179
180#endif // QBITARRAY_H
\inmodule QtCore
Definition qbitarray.h:14
\inmodule QtCore\reentrant
Definition qdatastream.h:50
Definition qmap.h:190
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
QModelIndex siblingAtColumn(int column) const
Returns the sibling at column for the current row.
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index, or a default-constructed Q...
constexpr int row() const noexcept
Returns the row this model index refers to.
friend constexpr bool comparesEqual(const QModelIndex &lhs, const QModelIndex &rhs) noexcept
QModelIndex siblingAtRow(int row) const
Returns the sibling at row for the current column.
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
constexpr const QAbstractItemModel * model() const noexcept
Returns a pointer to the model containing the item that this index refers to.
Qt::ItemFlags flags() const
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
void * internalPointer() const noexcept
Returns a {void} {*} pointer used by the model to associate the index with the internal data structur...
friend constexpr Qt::strong_ordering compareThreeWay(const QModelIndex &lhs, const QModelIndex &rhs) noexcept
void multiData(QModelRoleDataSpan roleDataSpan) const
constexpr QModelIndex() noexcept
Creates a new empty model index.
constexpr int column() const noexcept
Returns the column this model index refers to.
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
constexpr quintptr internalId() const noexcept
Returns a {quintptr} used by the model to associate the index with the internal data structure.
const void * constInternalPointer() const noexcept
Returns a {const void} {*} pointer used by the model to associate the index with the internal data st...
constexpr qsizetype size() const noexcept
Returns the length of the span represented by this object.
constexpr QModelRoleData * end() const noexcept
Returns a pointer to the imaginary element one past the end of the span represented by this object.
constexpr QModelRoleData * begin() const noexcept
Returns a pointer to the beginning of the span represented by this object.
constexpr QModelRoleDataSpan() noexcept
Constructs an empty QModelRoleDataSpan.
constexpr qsizetype length() const noexcept
Returns the length of the span represented by this object.
constexpr QModelRoleDataSpan(Container &c) noexcept(noexcept(std::data(c)) &&noexcept(std::size(c)))
Constructs an QModelRoleDataSpan spanning over the container c, which can be any contiguous container...
constexpr QModelRoleData & operator[](qsizetype index) const
Returns a modifiable reference to the QModelRoleData at position index in the span.
constexpr QModelRoleData * data() const noexcept
Returns a pointer to the beginning of the span represented by this object.
constexpr QModelRoleDataSpan(QModelRoleData *modelRoleData, qsizetype len)
Constructs an QModelRoleDataSpan spanning over the array beginning at modelRoleData and with length l...
constexpr QModelRoleDataSpan(QModelRoleData &modelRoleData) noexcept
Constructs an QModelRoleDataSpan spanning over modelRoleData, seen as a 1-element array.
constexpr QVariant * dataForRole(int role) const
Returns the data associated with the first QModelRoleData in the span that has its role equal to role...
Combined button and popup list for selecting options.
bool comparesEqual(const QPersistentModelIndex &lhs, const QModelIndex &rhs) noexcept
Qt::strong_ordering compareThreeWay(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) noexcept
static uint typeOfVariant(const QVariant &value)
QDebug operator<<(QDebug dbg, const QModelIndex &idx)
Qt::strong_ordering compareThreeWay(const QPersistentModelIndex &lhs, const QModelIndex &rhs) noexcept
QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)
bool comparesEqual(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) noexcept
Q_GLOBAL_STATIC(DefaultRoleNames, qDefaultRoleNames, { { Qt::DisplayRole, "display" }, { Qt::DecorationRole, "decoration" }, { Qt::EditRole, "edit" }, { Qt::ToolTipRole, "toolTip" }, { Qt::StatusTipRole, "statusTip" }, { Qt::WhatsThisRole, "whatsThis" }, }) const QHash< int
size_t qHash(const QModelIndex &index, size_t seed=0) noexcept
Q_DECLARE_TYPEINFO(QModelRoleDataSpan, Q_RELOCATABLE_TYPE)
QT_REQUIRE_CONFIG(itemmodel)
QList< QModelIndex > QModelIndexList
size_t qHash(const QPersistentModelIndex &index, size_t seed=0) noexcept
Q_DECLARE_TYPEINFO(QModelIndex, Q_RELOCATABLE_TYPE)
Q_DECLARE_TYPEINFO(QModelRoleData, Q_RELOCATABLE_TYPE)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)