Qt 6.x
The Qt SDK
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#ifndef QT_NO_DATASTREAM
15 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
16 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
17#endif
18 friend Q_CORE_EXPORT size_t qHash(const QBitArray &key, size_t seed) noexcept;
20
21public:
22 inline QBitArray() noexcept {}
23 explicit QBitArray(qsizetype size, bool val = false);
24 QBitArray(const QBitArray &other) noexcept : d(other.d) {}
25 inline QBitArray &operator=(const QBitArray &other) noexcept { d = other.d; return *this; }
26 inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
27 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBitArray)
28
29 void swap(QBitArray &other) noexcept { d.swap(other.d); }
30
31 inline qsizetype size() const { return (d.size() << 3) - *d.constData(); }
32 inline qsizetype count() const { return (d.size() << 3) - *d.constData(); }
33 qsizetype count(bool on) const;
34
35 inline bool isEmpty() const { return d.isEmpty(); }
36 inline bool isNull() const { return d.isNull(); }
37
38 void resize(qsizetype size);
39
40 inline void detach() { d.detach(); }
41 inline bool isDetached() const { return d.isDetached(); }
42 inline void clear() { d.clear(); }
43
44 bool testBit(qsizetype i) const;
45 void setBit(qsizetype i);
46 void setBit(qsizetype i, bool val);
47 void clearBit(qsizetype i);
48 bool toggleBit(qsizetype i);
49
50 bool at(qsizetype i) const;
51 QBitRef operator[](qsizetype i);
52 bool operator[](qsizetype i) const;
53
54 QBitArray &operator&=(const QBitArray &);
56 QBitArray &operator^=(const QBitArray &);
57 QBitArray operator~() const;
58
59 inline bool operator==(const QBitArray &other) const { return d == other.d; }
60 inline bool operator!=(const QBitArray &other) const { return d != other.d; }
61
62 inline bool fill(bool val, qsizetype size = -1);
63 void fill(bool val, qsizetype first, qsizetype last);
64
65 inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); }
66
67 const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
68 static QBitArray fromBits(const char *data, qsizetype len);
69
70 quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
71
72public:
73 typedef QByteArray::DataPointer DataPtr;
74 inline DataPtr &data_ptr() { return d.data_ptr(); }
75};
76
77inline bool QBitArray::fill(bool aval, qsizetype asize)
78{ *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
79
80Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
81Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
82Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
83
84inline bool QBitArray::testBit(qsizetype i) const
85{ Q_ASSERT(size_t(i) < size_t(size()));
86 return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
87
89{ Q_ASSERT(size_t(i) < size_t(size()));
90 *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
91
93{ Q_ASSERT(size_t(i) < size_t(size()));
94 *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
95
96inline void QBitArray::setBit(qsizetype i, bool val)
97{ if (val) setBit(i); else clearBit(i); }
98
100{ Q_ASSERT(size_t(i) < size_t(size()));
101 uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
102 uchar c = uchar(*p&b); *p^=b; return c!=0; }
103
104inline bool QBitArray::operator[](qsizetype i) const { return testBit(i); }
105inline bool QBitArray::at(qsizetype i) const { return testBit(i); }
106
107class Q_CORE_EXPORT QBitRef
108{
109private:
110 QBitArray &a;
111 qsizetype i;
112 inline QBitRef(QBitArray &array, qsizetype idx) : a(array), i(idx) { }
113 friend class QBitArray;
114
115public:
116 inline operator bool() const { return a.testBit(i); }
117 inline bool operator!() const { return !a.testBit(i); }
118 QBitRef &operator=(const QBitRef &val) { a.setBit(i, val); return *this; }
119 QBitRef &operator=(bool val) { a.setBit(i, val); return *this; }
120};
121
123{ Q_ASSERT(i >= 0); return QBitRef(*this, i); }
124
125#ifndef QT_NO_DATASTREAM
126Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
128#endif
129
130#ifndef QT_NO_DEBUG_STREAM
131Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &);
132#endif
133
134Q_DECLARE_SHARED(QBitArray)
135
137
138#endif // QBITARRAY_H
\inmodule QtCore
Definition qbitarray.h:13
void truncate(qsizetype pos)
Truncates the bit array at index position pos.
Definition qbitarray.h:65
void clear()
Clears the contents of the bit array and makes it empty.
Definition qbitarray.h:42
bool testBit(qsizetype i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition qbitarray.h:84
const char * bits() const
Definition qbitarray.h:67
bool at(qsizetype i) const
Returns the value of the bit at index position i.
Definition qbitarray.h:105
QBitArray & operator=(const QBitArray &other) noexcept
Assigns other to this bit array and returns a reference to this bit array.
Definition qbitarray.h:25
QBitArray(QBitArray &&other) noexcept
Move-constructs a QBitArray instance, making it point at the same object that other was pointing to.
Definition qbitarray.h:26
bool operator==(const QBitArray &other) const
Returns true if other is equal to this bit array; otherwise returns false.
Definition qbitarray.h:59
bool operator!=(const QBitArray &other) const
Returns true if other is not equal to this bit array; otherwise returns false.
Definition qbitarray.h:60
qsizetype count() const
Same as size().
Definition qbitarray.h:32
bool isEmpty() const
Returns true if this bit array has size 0; otherwise returns false.
Definition qbitarray.h:35
void detach()
Definition qbitarray.h:40
void setBit(qsizetype i)
Sets the bit at index position i to 1.
Definition qbitarray.h:88
QBitArray(const QBitArray &other) noexcept
Constructs a copy of other.
Definition qbitarray.h:24
void clearBit(qsizetype i)
Sets the bit at index position i to 0.
Definition qbitarray.h:92
bool isDetached() const
Definition qbitarray.h:41
bool fill(bool val, qsizetype size=-1)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false.
Definition qbitarray.h:77
bool toggleBit(qsizetype i)
Inverts the value of the bit at index position i, returning the previous value of that bit as either ...
Definition qbitarray.h:99
qsizetype size() const
Returns the number of bits stored in the bit array.
Definition qbitarray.h:31
bool isNull() const
Returns true if this bit array is null; otherwise returns false.
Definition qbitarray.h:36
QBitRef operator[](qsizetype i)
Returns the bit at index position i as a modifiable reference.
Definition qbitarray.h:122
QBitArray() noexcept
Constructs an empty bit array.
Definition qbitarray.h:22
\inmodule QtCore \reentrant
Definition qbitarray.h:108
QBitRef & operator=(const QBitRef &val)
Sets the value referenced by the QBitRef to that referenced by QBitRef v.
Definition qbitarray.h:118
bool operator!() const
Definition qbitarray.h:117
QBitRef & operator=(bool val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbitarray.h:119
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
Endian
\value BigEndian Big-endian byte order (also called Network byte order) \value LittleEndian Little-en...
Definition qsysinfo.h:28
a resize(100000)
Combined button and popup list for selecting options.
Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QBitArray &)
Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &)
Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &)
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QBitArray &)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
static bool testBit(long bit, const long *field)
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
GLboolean GLboolean GLboolean b
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
const GLubyte * c
GLuint GLfloat * val
GLenum array
GLenum GLsizei len
GLfloat GLfloat p
[1]
const void * data_ptr(const QTransform &t)
Definition qpainter_p.h:48
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int quint32
Definition qtypes.h:45
unsigned char uchar
Definition qtypes.h:27
ptrdiff_t qsizetype
Definition qtypes.h:70
QUrl::FormattingOptions & operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
Definition qurl.h:294
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QDataStream & operator>>(QDataStream &in, MyClass &myObj)
QObject::connect nullptr
ba setBit(0, true)
ba fill(true)
QSharedPointer< T > other(t)
[5]
QAction * at