1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QBITARRAY_H
41#define QBITARRAY_H
42
43#include <QtCore/qbytearray.h>
44
45QT_BEGIN_NAMESPACE
46
47class QBitRef;
48class Q_CORE_EXPORT QBitArray
49{
50#ifndef QT_NO_DATASTREAM
51 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
52 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
53#endif
54 friend Q_CORE_EXPORT size_t qHash(const QBitArray &key, size_t seed) noexcept;
55 QByteArray d;
56
57public:
58 inline QBitArray() noexcept {}
59 explicit QBitArray(qsizetype size, bool val = false);
60 QBitArray(const QBitArray &other) : d(other.d) {}
61 inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; }
62 inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
63 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBitArray)
64
65 inline void swap(QBitArray &other) noexcept { qSwap(d, other.d); }
66
67 inline qsizetype size() const { return (d.size() << 3) - *d.constData(); }
68 inline qsizetype count() const { return (d.size() << 3) - *d.constData(); }
69 qsizetype count(bool on) const;
70
71 inline bool isEmpty() const { return d.isEmpty(); }
72 inline bool isNull() const { return d.isNull(); }
73
74 void resize(qsizetype size);
75
76 inline void detach() { d.detach(); }
77 inline bool isDetached() const { return d.isDetached(); }
78 inline void clear() { d.clear(); }
79
80 bool testBit(qsizetype i) const;
81 void setBit(qsizetype i);
82 void setBit(qsizetype i, bool val);
83 void clearBit(qsizetype i);
84 bool toggleBit(qsizetype i);
85
86 bool at(qsizetype i) const;
87 QBitRef operator[](qsizetype i);
88 bool operator[](qsizetype i) const;
89
90 QBitArray &operator&=(const QBitArray &);
91 QBitArray &operator|=(const QBitArray &);
92 QBitArray &operator^=(const QBitArray &);
93 QBitArray operator~() const;
94
95 inline bool operator==(const QBitArray &other) const { return d == other.d; }
96 inline bool operator!=(const QBitArray &other) const { return d != other.d; }
97
98 inline bool fill(bool val, qsizetype size = -1);
99 void fill(bool val, qsizetype first, qsizetype last);
100
101 inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); }
102
103 const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
104 static QBitArray fromBits(const char *data, qsizetype len);
105
106 quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
107
108public:
109 typedef QByteArray::DataPointer DataPtr;
110 inline DataPtr &data_ptr() { return d.data_ptr(); }
111};
112
113inline bool QBitArray::fill(bool aval, qsizetype asize)
114{ *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
115
116Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
117Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
118Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
119
120inline bool QBitArray::testBit(qsizetype i) const
121{ Q_ASSERT(size_t(i) < size_t(size()));
122 return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
123
124inline void QBitArray::setBit(qsizetype i)
125{ Q_ASSERT(size_t(i) < size_t(size()));
126 *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
127
128inline void QBitArray::clearBit(qsizetype i)
129{ Q_ASSERT(size_t(i) < size_t(size()));
130 *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
131
132inline void QBitArray::setBit(qsizetype i, bool val)
133{ if (val) setBit(i); else clearBit(i); }
134
135inline bool QBitArray::toggleBit(qsizetype i)
136{ Q_ASSERT(size_t(i) < size_t(size()));
137 uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
138 uchar c = uchar(*p&b); *p^=b; return c!=0; }
139
140inline bool QBitArray::operator[](qsizetype i) const { return testBit(i); }
141inline bool QBitArray::at(qsizetype i) const { return testBit(i); }
142
143class Q_CORE_EXPORT QBitRef
144{
145private:
146 QBitArray &a;
147 qsizetype i;
148 inline QBitRef(QBitArray &array, qsizetype idx) : a(array), i(idx) { }
149 friend class QBitArray;
150
151public:
152 inline operator bool() const { return a.testBit(i); }
153 inline bool operator!() const { return !a.testBit(i); }
154 QBitRef &operator=(const QBitRef &val) { a.setBit(i, val); return *this; }
155 QBitRef &operator=(bool val) { a.setBit(i, val); return *this; }
156};
157
158inline QBitRef QBitArray::operator[](qsizetype i)
159{ Q_ASSERT(i >= 0); return QBitRef(*this, i); }
160
161#ifndef QT_NO_DATASTREAM
162Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
163Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
164#endif
165
166#ifndef QT_NO_DEBUG_STREAM
167Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &);
168#endif
169
170Q_DECLARE_SHARED(QBitArray)
171
172QT_END_NAMESPACE
173
174#endif // QBITARRAY_H
175