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
4#ifndef QBITARRAY_H
5#define QBITARRAY_H
6
7#include <QtCore/qbytearray.h>
8
10
11class QBitRef;
12class Q_CORE_EXPORT QBitArray
13{
14 Q_CORE_EXPORT friend QBitArray operator&(const QBitArray &a1, const QBitArray &a2);
15 friend QBitArray operator&(QBitArray &&a1, const QBitArray &a2)
16 { return a1 &= a2; }
17 friend QBitArray operator&(const QBitArray &a1, QBitArray &&a2)
18 { return a2 &= a1; }
19 friend QBitArray operator&(QBitArray &&a1, QBitArray &&a2)
20 { return a1 &= a2; }
21
22 Q_CORE_EXPORT friend QBitArray operator|(const QBitArray &a1, const QBitArray &a2);
23 friend QBitArray operator|(QBitArray &&a1, const QBitArray &a2)
24 { return a1 |= a2; }
25 friend QBitArray operator|(const QBitArray &a1, QBitArray &&a2)
26 { return a2 |= a1; }
27 friend QBitArray operator|(QBitArray &&a1, QBitArray &&a2)
28 { return a1 |= a2; }
29
30 Q_CORE_EXPORT friend QBitArray operator^(const QBitArray &a1, const QBitArray &a2);
31 friend QBitArray operator^(QBitArray &&a1, const QBitArray &a2)
32 { return a1 ^= a2; }
33 friend QBitArray operator^(const QBitArray &a1, QBitArray &&a2)
34 { return a2 ^= a1; }
35 friend QBitArray operator^(QBitArray &&a1, QBitArray &&a2)
36 { return a1 ^= a2; }
37
38#ifndef QT_NO_DATASTREAM
39 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
40 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
41#endif
42 friend Q_CORE_EXPORT size_t qHash(const QBitArray &key, size_t seed) noexcept;
43 friend QBitArray operator~(QBitArray a)
44 { return std::move(a).inverted_inplace(); }
45 QByteArray d;
46
47 QBitArray(QByteArrayData &&dd) : d(std::move(dd)) {}
48
49 template <typename BitArray> static auto bitLocation(BitArray &ba, qsizetype i)
50 {
51 Q_ASSERT(size_t(i) < size_t(ba.size()));
52 struct R {
53 decltype(ba.d[1]) byte;
54 uchar bitMask;
55 };
56 qsizetype byteIdx = i >> 3;
57 qsizetype bitIdx = i & 7;
58 return R{ ba.d[1 + byteIdx], uchar(1U << bitIdx) };
59 }
60
61 QBitArray inverted_inplace() &&;
62
63public:
64 inline QBitArray() noexcept {}
65 explicit QBitArray(qsizetype size, bool val = false);
66 // Rule Of Zero applies
67#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
68 QBitArray(const QBitArray &other) noexcept : d(other.d) {}
69 inline QBitArray &operator=(const QBitArray &other) noexcept { d = other.d; return *this; }
70 inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
71 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBitArray)
72#endif // Qt 6
73
74 void swap(QBitArray &other) noexcept { d.swap(other.d); }
75
76 qsizetype size() const { return qsizetype((size_t(d.size()) << 3) - *d.constData()); }
77 qsizetype count() const { return size(); }
78 qsizetype count(bool on) const;
79
80 inline bool isEmpty() const { return d.isEmpty(); }
81 inline bool isNull() const { return d.isNull(); }
82
83 void resize(qsizetype size);
84
85 inline void detach() { d.detach(); }
86 inline bool isDetached() const { return d.isDetached(); }
87 inline void clear() { d.clear(); }
88
89 bool testBit(qsizetype i) const
90 { auto r = bitLocation(*this, i); return r.byte & r.bitMask; }
91 void setBit(qsizetype i)
92 { auto r = bitLocation(*this, i); r.byte |= r.bitMask; }
93 void setBit(qsizetype i, bool val)
94 { if (val) setBit(i); else clearBit(i); }
95 void clearBit(qsizetype i)
96 { auto r = bitLocation(*this, i); r.byte &= ~r.bitMask; }
97 bool toggleBit(qsizetype i)
98 {
99 auto r = bitLocation(*this, i);
100 bool cl = r.byte & r.bitMask;
101 r.byte ^= r.bitMask;
102 return cl;
103 }
104
105 bool at(qsizetype i) const { return testBit(i); }
106 inline QBitRef operator[](qsizetype i);
107 bool operator[](qsizetype i) const { return testBit(i); }
108
109 QBitArray &operator&=(QBitArray &&);
110 QBitArray &operator|=(QBitArray &&);
111 QBitArray &operator^=(QBitArray &&);
112 QBitArray &operator&=(const QBitArray &);
113 QBitArray &operator|=(const QBitArray &);
114 QBitArray &operator^=(const QBitArray &);
115#if QT_CORE_REMOVED_SINCE(6, 7)
116 QBitArray operator~() const;
117#endif
118
119#if QT_CORE_REMOVED_SINCE(6, 8)
120 inline bool operator==(const QBitArray &other) const { return comparesEqual(d, other.d); }
121 inline bool operator!=(const QBitArray &other) const { return !operator==(other); }
122#endif
123
124 bool fill(bool aval, qsizetype asize = -1)
125 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
126 void fill(bool val, qsizetype first, qsizetype last);
127
128 inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); }
129
130 const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
131 static QBitArray fromBits(const char *data, qsizetype len);
132
133 quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
134
135public:
136 typedef QByteArray::DataPointer DataPtr;
137 inline DataPtr &data_ptr() { return d.data_ptr(); }
138 inline const DataPtr &data_ptr() const { return d.data_ptr(); }
139
140private:
141 friend bool comparesEqual(const QBitArray &lhs, const QBitArray &rhs) noexcept
142 {
143 return lhs.d == rhs.d;
144 }
145 Q_DECLARE_EQUALITY_COMPARABLE(QBitArray)
146};
147
148class QT6_ONLY(Q_CORE_EXPORT) QBitRef
149{
150private:
151 QBitArray &a;
152 qsizetype i;
153 inline QBitRef(QBitArray &array, qsizetype idx) : a(array), i(idx) { }
154 friend class QBitArray;
155
156public:
157 inline operator bool() const { return a.testBit(i); }
158 inline bool operator!() const { return !a.testBit(i); }
159 QBitRef &operator=(const QBitRef &val) { a.setBit(i, val); return *this; }
160 QBitRef &operator=(bool val) { a.setBit(i, val); return *this; }
161};
162
163QBitRef QBitArray::operator[](qsizetype i)
164{ Q_ASSERT(i >= 0); return QBitRef(*this, i); }
165
166#ifndef QT_NO_DATASTREAM
167Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
168Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
169#endif
170
171#ifndef QT_NO_DEBUG_STREAM
172Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &);
173#endif
174
176
177QT_END_NAMESPACE
178
179#endif // QBITARRAY_H
\inmodule QtCore
Definition qbitarray.h:13
\inmodule QtCore\reentrant
Definition qdatastream.h:49
Definition qmap.h:189
\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:2462
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)