1 /**************************************************************************** 2 ** 3 ** DQt - D bindings for the Qt Toolkit 4 ** 5 ** GNU Lesser General Public License Usage 6 ** This file may be used under the terms of the GNU Lesser 7 ** General Public License version 3 as published by the Free Software 8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 9 ** packaging of this file. Please review the following information to 10 ** ensure the GNU Lesser General Public License version 3 requirements 11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 12 ** 13 ****************************************************************************/ 14 module qt.core.bitarray; 15 extern(C++): 16 17 import qt.config; 18 import qt.core.bytearray; 19 import qt.core.global; 20 import qt.core.metatype; 21 import qt.core.typeinfo; 22 import qt.helpers; 23 24 /+ class QBitRef; +/ 25 @Q_MOVABLE_TYPE @(QMetaType.Type.QBitArray) extern(C++, class) struct /+ Q_CORE_EXPORT +/ QBitArray 26 { 27 private: 28 /+ friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &); +/ 29 /+ friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &); +/ 30 /+ friend Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed) noexcept; +/ 31 QByteArray d; 32 33 public: 34 @disable this(); 35 /+pragma(inline, true) this()/+ noexcept+/ {}+/ 36 /+ explicit +/this(int size, bool val = false); 37 @disable this(this); 38 this(ref const(QBitArray) other) 39 { 40 this.d = other.d; 41 } 42 /+pragma(inline, true) ref QBitArray operator =(ref const(QBitArray) other) { d = other.d; return this; }+/ 43 /+ inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {} +/ 44 /+ inline QBitArray &operator=(QBitArray &&other) noexcept 45 { qSwap(d, other.d); return *this; } +/ 46 47 /+ inline void swap(QBitArray &other) noexcept { qSwap(d, other.d); } +/ 48 49 pragma(inline, true) int size() const { return (d.size() << 3) - *d.constData(); } 50 pragma(inline, true) int count() const { return (d.size() << 3) - *d.constData(); } 51 int count(bool on) const; 52 53 pragma(inline, true) bool isEmpty() const { return d.isEmpty(); } 54 pragma(inline, true) bool isNull() const { return d.isNull(); } 55 56 void resize(int size); 57 58 pragma(inline, true) void detach() { d.detach(); } 59 pragma(inline, true) bool isDetached() const { return d.isDetached(); } 60 pragma(inline, true) void clear() { d.clear(); } 61 62 pragma(inline, true) bool testBit(int i) const 63 { (mixin(Q_ASSERT(q{uint(i) < uint(QBitArray.size())}))); 64 return (*(reinterpret_cast!(const(uchar)*)(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; } 65 pragma(inline, true) void setBit(int i) 66 { (mixin(Q_ASSERT(q{uint(i) < uint(QBitArray.size())}))); 67 *(reinterpret_cast!(uchar*)(d.data())+1+(i>>3)) |= cast(uchar)(1 << (i & 7)); } 68 pragma(inline, true) void setBit(int i, bool val) 69 { if (val) setBit(i); else clearBit(i); } 70 pragma(inline, true) void clearBit(int i) 71 { (mixin(Q_ASSERT(q{uint(i) < uint(QBitArray.size())}))); 72 *(reinterpret_cast!(uchar*)(d.data())+1+(i>>3)) &= ~uint(uchar(1 << (i & 7))); } 73 pragma(inline, true) bool toggleBit(int i) 74 { (mixin(Q_ASSERT(q{uint(i) < uint(QBitArray.size())}))); 75 uchar b = cast(uchar)(1<<(i&7)); uchar* p = reinterpret_cast!(uchar*)(d.data())+1+(i>>3); 76 uchar c = cast(uchar)(*p&b); *p^=b; return c!=0; } 77 78 pragma(inline, true) bool at(int i) const { return testBit(i); } 79 pragma(inline, true) QBitRef opIndex(int i) 80 { (mixin(Q_ASSERT(q{i >= 0}))); return QBitRef(this, i); } 81 pragma(inline, true) bool opIndex(int i) const { return testBit(i); } 82 pragma(inline, true) QBitRef opIndex(uint i) 83 { return QBitRef(this, i); } 84 pragma(inline, true) bool opIndex(uint i) const { return testBit(i); } 85 86 ref QBitArray opOpAssign(string op)(ref const(QBitArray) ) if(op == "&"); 87 ref QBitArray opOpAssign(string op)(ref const(QBitArray) ) if(op == "|"); 88 /+ref QBitArray operator ^=(ref const(QBitArray) );+/ 89 /+QBitArray operator ~() const;+/ 90 91 /+pragma(inline, true) bool operator ==(ref const(QBitArray) other) const { return d == other.d; }+/ 92 /+pragma(inline, true) bool operator !=(ref const(QBitArray) other) const { return d != other.d; }+/ 93 94 pragma(inline, true) bool fill(bool aval, int asize = -1) 95 { this = QBitArray((asize < 0 ? this.size() : asize), aval); return true; } 96 void fill(bool val, int first, int last); 97 98 pragma(inline, true) void truncate(int pos) { if (pos < size()) resize(pos); } 99 100 const(char)* bits() const { return isEmpty() ? null : d.constData() + 1; } 101 static QBitArray fromBits(const(char)* data, qsizetype len); 102 103 public: 104 alias DataPtr = QByteArray.DataPtr; 105 pragma(inline, true) ref DataPtr data_ptr() return { return d.data_ptr(); } 106 } 107 108 /+/+ Q_CORE_EXPORT +/ QBitArray operator &(ref const(QBitArray) , ref const(QBitArray) );+/ 109 /+/+ Q_CORE_EXPORT +/ QBitArray operator |(ref const(QBitArray) , ref const(QBitArray) );+/ 110 /+/+ Q_CORE_EXPORT +/ QBitArray operator ^(ref const(QBitArray) , ref const(QBitArray) );+/ 111 112 extern(C++, class) struct /+ Q_CORE_EXPORT +/ QBitRef 113 { 114 private: 115 QBitArray *a; 116 int i; 117 pragma(inline, true) this(ref QBitArray array, int idx) 118 { 119 this.a = &array; 120 this.i = idx; 121 } 122 /+ friend class QBitArray; +/ 123 public: 124 /+ QBitRef(const QBitRef &) = default; +/ 125 /+pragma(inline, true) auto opCast(T : bool)() const { return a.testBit(i); }+/ 126 /+pragma(inline, true) bool operator !() const { return !a.testBit(i); }+/ 127 /+ref QBitRef operator =(ref const(QBitRef) val) { a.setBit(i, val); return this; }+/ 128 /+ref QBitRef operator =(bool val) { a.setBit(i, val); return this; }+/ 129 } 130 131 132 /+ #ifndef QT_NO_DATASTREAM 133 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &); 134 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &); 135 #endif 136 137 #ifndef QT_NO_DEBUG_STREAM 138 Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &); 139 #endif 140 141 Q_DECLARE_SHARED(QBitArray) +/ 142