home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_qt.idb / usr / freeware / include / Qt / qbitarray.h.z / qbitarray.h
Encoding:
C/C++ Source or Header  |  1998-10-28  |  4.1 KB  |  149 lines

  1. /****************************************************************************
  2. ** $Id: qbitarray.h,v 2.6 1998/07/03 00:09:43 hanord Exp $
  3. **
  4. ** Definition of QBitArray class
  5. **
  6. ** Created : 940118
  7. **
  8. ** Copyright (C) 1992-1998 Troll Tech AS.  All rights reserved.
  9. **
  10. ** This file is part of Qt Free Edition, version 1.40.
  11. **
  12. ** See the file LICENSE included in the distribution for the usage
  13. ** and distribution terms, or http://www.troll.no/free-license.html.
  14. **
  15. ** IMPORTANT NOTE: You may NOT copy this file or any part of it into
  16. ** your own programs or libraries.
  17. **
  18. ** Please see http://www.troll.no/pricing.html for information about 
  19. ** Qt Professional Edition, which is this same library but with a
  20. ** license which allows creation of commercial/proprietary software.
  21. **
  22. *****************************************************************************/
  23.  
  24. #ifndef QBITARRAY_H
  25. #define QBITARRAY_H
  26.  
  27. #ifndef QT_H
  28. #include "qstring.h"
  29. #endif // QT_H
  30.  
  31.  
  32. /*****************************************************************************
  33.   QBitVal class; a context class for QBitArray::operator[]
  34.  *****************************************************************************/
  35.  
  36. class QBitArray;
  37.  
  38. class QBitVal
  39. {
  40. private:
  41.     QBitArray *array;
  42.     uint    index;
  43. public:
  44.     QBitVal( QBitArray *a, uint i ) : array(a), index(i) {}
  45.     operator int();
  46.     QBitVal &operator=( const QBitVal &v );
  47.     QBitVal &operator=( int v );
  48. };
  49.  
  50.  
  51. /*****************************************************************************
  52.   QBitArray class
  53.  *****************************************************************************/
  54.  
  55. class QBitArray : public QByteArray
  56. {
  57. public:
  58.     QBitArray();
  59.     QBitArray( uint size );
  60.     QBitArray( const QBitArray &a ) : QByteArray( a ) {}
  61.  
  62.     QBitArray &operator=( const QBitArray & );
  63.  
  64.     uint    size() const;
  65.     bool    resize( uint size );
  66.  
  67.     bool    fill( bool v, int size = -1 );
  68.  
  69.     void    detach();
  70.     QBitArray copy() const;
  71.  
  72.     bool    testBit( uint index ) const;
  73.     void    setBit( uint index );
  74.     void    setBit( uint index, bool value );
  75.     void    clearBit( uint index );
  76.     bool    toggleBit( uint index );
  77.  
  78.     bool    at( uint index ) const;
  79.     QBitVal operator[]( int index );
  80.  
  81.     QBitArray &operator&=( const QBitArray & );
  82.     QBitArray &operator|=( const QBitArray & );
  83.     QBitArray &operator^=( const QBitArray & );
  84.     QBitArray  operator~() const;
  85.  
  86. protected:
  87.     struct bitarr_data : public QGArray::array_data {
  88.     uint   nbits;
  89.     };
  90.     array_data *newData()            { return new bitarr_data; }
  91.     void    deleteData( array_data *d ) { delete (bitarr_data*)d; }
  92. private:
  93.     void    pad0();
  94. };
  95.  
  96.  
  97. inline QBitArray &QBitArray::operator=( const QBitArray &a )
  98. { return (QBitArray&)assign( a ); }
  99.  
  100. inline uint QBitArray::size() const
  101. { return ((bitarr_data*)sharedBlock())->nbits; }
  102.  
  103. inline void QBitArray::setBit( uint index, bool value )
  104. { if ( value ) setBit(index); else clearBit(index); }
  105.  
  106. inline bool QBitArray::at( uint index ) const
  107. { return testBit(index); }
  108.  
  109. inline QBitVal QBitArray::operator[]( int index )
  110. { return QBitVal( (QBitArray*)this, index ); }
  111.  
  112.  
  113. /*****************************************************************************
  114.   Misc. QBitArray operator functions
  115.  *****************************************************************************/
  116.  
  117. QBitArray operator&( const QBitArray &, const QBitArray & );
  118. QBitArray operator|( const QBitArray &, const QBitArray & );
  119. QBitArray operator^( const QBitArray &, const QBitArray & );
  120.  
  121.  
  122. inline QBitVal::operator int()
  123. {
  124.     return array->testBit( index );
  125. }
  126.  
  127. inline QBitVal &QBitVal::operator=( const QBitVal &v )
  128. {
  129.     array->setBit( index, v.array->testBit(v.index) );
  130.     return *this;
  131. }
  132.  
  133. inline QBitVal &QBitVal::operator=( int v )    // ### Qt 2.0: change to bool
  134. {
  135.     array->setBit( index, v );
  136.     return *this;
  137. }
  138.  
  139.  
  140. /*****************************************************************************
  141.   QBitArray stream functions
  142.  *****************************************************************************/
  143.  
  144. QDataStream &operator<<( QDataStream &, const QBitArray & );
  145. QDataStream &operator>>( QDataStream &, QBitArray & );
  146.  
  147.  
  148. #endif // QBITARRAY_H
  149.