1/****************************************************************************
2**
3** Copyright (C) 2016 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
47
48class QBitRef;
49class Q_CORE_EXPORT QBitArray
50{
51 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
52 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
53 friend Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed) Q_DECL_NOTHROW;
54 QByteArray d;
55
56public:
57 inline QBitArray() Q_DECL_NOTHROW {}
58 explicit QBitArray(int size, bool val = false);
59 QBitArray(const QBitArray &other) : d(other.d) {}
60 inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; }
61#ifdef Q_COMPILER_RVALUE_REFS
62 inline QBitArray(QBitArray &&other) Q_DECL_NOTHROW : d(std::move(other.d)) {}
63 inline QBitArray &operator=(QBitArray &&other) Q_DECL_NOTHROW
64 { qSwap(d, other.d); return *this; }
65#endif
66
67 inline void swap(QBitArray &other) Q_DECL_NOTHROW { qSwap(d, other.d); }
68
69 inline int size() const { return (d.size() << 3) - *d.constData(); }
70 inline int count() const { return (d.size() << 3) - *d.constData(); }
71 int count(bool on) const;
72
73 inline bool isEmpty() const { return d.isEmpty(); }
74 inline bool isNull() const { return d.isNull(); }
75
76 void resize(int size);
77
78 inline void detach() { d.detach(); }
79 inline bool isDetached() const { return d.isDetached(); }
80 inline void clear() { d.clear(); }
81
82 bool testBit(int i) const;
83 void setBit(int i);
84 void setBit(int i, bool val);
85 void clearBit(int i);
86 bool toggleBit(int i);
87
88 bool at(int i) const;
89 QBitRef operator[](int i);
90 bool operator[](int i) const;
91 QBitRef operator[](uint i);
92 bool operator[](uint i) const;
93
94 QBitArray& operator&=(const QBitArray &);
95 QBitArray& operator|=(const QBitArray &);
96 QBitArray& operator^=(const QBitArray &);
97 QBitArray operator~() const;
98
99 inline bool operator==(const QBitArray& other) const { return d == other.d; }
100 inline bool operator!=(const QBitArray& other) const { return d != other.d; }
101
102 inline bool fill(bool val, int size = -1);
103 void fill(bool val, int first, int last);
104
105 inline void truncate(int pos) { if (pos < size()) resize(pos); }
106
107 const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
108 static QBitArray fromBits(const char *data, qsizetype len);
109
110public:
111 typedef QByteArray::DataPtr DataPtr;
112 inline DataPtr &data_ptr() { return d.data_ptr(); }
113};
114
115inline bool QBitArray::fill(bool aval, int asize)
116{ *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
117
118Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
119Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
120Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
121
122inline bool QBitArray::testBit(int i) const
123{ Q_ASSERT(uint(i) < uint(size()));
124 return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
125
126inline void QBitArray::setBit(int i)
127{ Q_ASSERT(uint(i) < uint(size()));
128 *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
129
130inline void QBitArray::clearBit(int i)
131{ Q_ASSERT(uint(i) < uint(size()));
132 *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
133
134inline void QBitArray::setBit(int i, bool val)
135{ if (val) setBit(i); else clearBit(i); }
136
137inline bool QBitArray::toggleBit(int i)
138{ Q_ASSERT(uint(i) < uint(size()));
139 uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
140 uchar c = uchar(*p&b); *p^=b; return c!=0; }
141
142inline bool QBitArray::operator[](int i) const { return testBit(i); }
143inline bool QBitArray::operator[](uint i) const { return testBit(i); }
144inline bool QBitArray::at(int i) const { return testBit(i); }
145
146class Q_CORE_EXPORT QBitRef
147{
148private:
149 QBitArray& a;
150 int i;
151 inline QBitRef(QBitArray& array, int idx) : a(array), i(idx) {}
152 friend class QBitArray;
153public:
154 inline operator bool() const { return a.testBit(i); }
155 inline bool operator!() const { return !a.testBit(i); }
156 QBitRef& operator=(const QBitRef& val) { a.setBit(i, val); return *this; }
157 QBitRef& operator=(bool val) { a.setBit(i, val); return *this; }
158};
159
160inline QBitRef QBitArray::operator[](int i)
161{ Q_ASSERT(i >= 0); return QBitRef(*this, i); }
162inline QBitRef QBitArray::operator[](uint i)
163{ return QBitRef(*this, i); }
164
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
175Q_DECLARE_SHARED(QBitArray)
176
177QT_END_NAMESPACE
178
179#endif // QBITARRAY_H
180