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 QtQml 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 QBITFIELD_P_H
41#define QBITFIELD_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/qglobal.h>
55#include <QtCore/qtypeinfo.h>
56
57QT_BEGIN_NAMESPACE
58
59class QBitField
60{
61public:
62 inline QBitField();
63 inline QBitField(const quint32 *, int bits);
64 inline QBitField(const QBitField &);
65 inline ~QBitField();
66
67 inline QBitField &operator=(const QBitField &);
68
69 inline quint32 size() const;
70 inline QBitField united(const QBitField &);
71 inline bool testBit(int) const;
72
73private:
74 quint32 bits:31;
75 quint32 *ownData;
76 const quint32 *data;
77};
78
79QBitField::QBitField()
80: bits(0), ownData(nullptr), data(nullptr)
81{
82}
83
84QBitField::QBitField(const quint32 *bitData, int bitCount)
85: bits((quint32)bitCount), ownData(nullptr), data(bitData)
86{
87}
88
89QBitField::QBitField(const QBitField &other)
90: bits(other.bits), ownData(other.ownData), data(other.data)
91{
92 if (ownData)
93 ++(*ownData);
94}
95
96QBitField::~QBitField()
97{
98 if (ownData)
99 if(0 == --(*ownData)) delete [] ownData;
100}
101
102QBitField &QBitField::operator=(const QBitField &other)
103{
104 if (other.data == data)
105 return *this;
106
107 if (ownData)
108 if(0 == --(*ownData)) delete [] ownData;
109
110 bits = other.bits;
111 ownData = other.ownData;
112 data = other.data;
113
114 if (ownData)
115 ++(*ownData);
116
117 return *this;
118}
119
120inline quint32 QBitField::size() const
121{
122 return bits;
123}
124
125QBitField QBitField::united(const QBitField &o)
126{
127 if (o.bits == 0) {
128 return *this;
129 } else if (bits == 0) {
130 return o;
131 } else {
132 int max = (bits > o.bits)?bits:o.bits;
133 int length = (max + 31) / 32;
134 QBitField rv;
135 rv.bits = max;
136 rv.ownData = new quint32[length + 1];
137 *(rv.ownData) = 1;
138 quint32 *rvdata;
139 rv.data = rvdata = rv.ownData + 1;
140 if (bits > o.bits) {
141 ::memcpy(dest: rvdata, src: data, n: length * sizeof(quint32));
142 for (quint32 ii = 0; ii < (o.bits + quint32(31)) / 32; ++ii)
143 (rvdata)[ii] |= o.data[ii];
144 } else {
145 ::memcpy(dest: rvdata, src: o.data, n: length * sizeof(quint32));
146 for (quint32 ii = 0; ii < (bits + quint32(31)) / 32; ++ii)
147 (rvdata)[ii] |= data[ii];
148 }
149 return rv;
150 }
151}
152
153bool QBitField::testBit(int b) const
154{
155 Q_ASSERT(b >= 0);
156 if ((quint32)b < bits) {
157 return data[b / 32] & (1 << (b % 32));
158 } else {
159 return false;
160 }
161}
162
163Q_DECLARE_TYPEINFO(QBitField, Q_MOVABLE_TYPE);
164
165QT_END_NAMESPACE
166
167#endif // QBITFIELD_P_H
168

source code of qtdeclarative/src/qml/qml/ftw/qbitfield_p.h