Qt 4.8
qbitfield_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QBITFIELD_P_H
43 #define QBITFIELD_P_H
44 
45 //
46 // W A R N I N G
47 // -------------
48 //
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include <QtCore/qglobal.h>
57 
59 
60 class QBitField
61 {
62 public:
63  inline QBitField();
64  inline QBitField(const quint32 *, int bits);
65  inline QBitField(const QBitField &);
66  inline ~QBitField();
67 
68  inline QBitField &operator=(const QBitField &);
69 
70  inline quint32 size() const;
71  inline QBitField united(const QBitField &);
72  inline bool testBit(int) const;
73 
74 private:
77  const quint32 *data;
78 };
79 
81 : bits(0), ownData(0), data(0)
82 {
83 }
84 
85 QBitField::QBitField(const quint32 *bitData, int bitCount)
86 : bits((quint32)bitCount), ownData(0), data(bitData)
87 {
88 }
89 
91 : bits(other.bits), ownData(other.ownData), data(other.data)
92 {
93  if (ownData)
94  ++(*ownData);
95 }
96 
98 {
99  if (ownData)
100  if(0 == --(*ownData)) delete [] ownData;
101 }
102 
104 {
105  if (other.data == data)
106  return *this;
107 
108  if (ownData)
109  if(0 == --(*ownData)) delete [] ownData;
110 
111  bits = other.bits;
112  ownData = other.ownData;
113  data = other.data;
114 
115  if (ownData)
116  ++(*ownData);
117 
118  return *this;
119 }
120 
121 inline quint32 QBitField::size() const
122 {
123  return bits;
124 }
125 
127 {
128  if (o.bits == 0) {
129  return *this;
130  } else if (bits == 0) {
131  return o;
132  } else {
133  int max = (bits > o.bits)?bits:o.bits;
134  int length = (max + 31) / 32;
135  QBitField rv;
136  rv.bits = max;
137  rv.ownData = new quint32[length + 1];
138  *(rv.ownData) = 1;
139  rv.data = rv.ownData + 1;
140  if (bits > o.bits) {
141  ::memcpy((quint32 *)rv.data, data, length * sizeof(quint32));
142  for (quint32 ii = 0; ii < (o.bits + quint32(31)) / 32; ++ii)
143  ((quint32 *)rv.data)[ii] |= o.data[ii];
144  } else {
145  ::memcpy((quint32 *)rv.data, o.data, length * sizeof(quint32));
146  for (quint32 ii = 0; ii < (bits + quint32(31)) / 32; ++ii)
147  ((quint32 *)rv.data)[ii] |= data[ii];
148  }
149  return rv;
150  }
151 }
152 
153 bool 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 
164 
165 #endif // QBITFIELD_P_H
bool testBit(int) const
Definition: qbitfield_p.h:153
QBitField united(const QBitField &)
Definition: qbitfield_p.h:126
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
quint32 bits
Definition: qbitfield_p.h:75
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
const quint32 * data
Definition: qbitfield_p.h:77
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QBitField & operator=(const QBitField &)
Definition: qbitfield_p.h:103
unsigned int quint32
Definition: qglobal.h:938
quint32 * ownData
Definition: qbitfield_p.h:76
quint32 size() const
Definition: qbitfield_p.h:121