src/declarative/qml/qbitfield_p.h
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    58 QT_BEGIN_NAMESPACE
       
    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:
       
    75     quint32 bits:31;
       
    76     quint32 *ownData;
       
    77     const quint32 *data;
       
    78 };
       
    79 
       
    80 QBitField::QBitField()
       
    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 
       
    90 QBitField::QBitField(const QBitField &other)
       
    91 : bits(other.bits), ownData(other.ownData), data(other.data)
       
    92 {
       
    93     if (ownData) 
       
    94         ++(*ownData);
       
    95 }
       
    96 
       
    97 QBitField::~QBitField()
       
    98 {
       
    99     if (ownData) 
       
   100         if(0 == --(*ownData)) delete [] ownData;
       
   101 }
       
   102 
       
   103 QBitField &QBitField::operator=(const QBitField &other)
       
   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 
       
   126 QBitField QBitField::united(const QBitField &o)
       
   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 
       
   163 QT_END_NAMESPACE
       
   164 
       
   165 #endif // QBITFIELD_P_H